Using the terminal find command

I use the terminal find command to perform complex searches. I tried to do this, but it doesn't work.


mac $ find -x ~ -maxdepth 1  -iname ".bash*"  -type f -exec echo {} | hexdump -C   \;
hexdump: ;: find: No such file or directory
-exec: no terminating ";" or "+"hexdump:
;: Bad file descriptor


On occasion, I want to run multiple commands in the -exec portion. Is this possible? How?


This does something, but all the output ends up co-mingled. I want one line of output per hexdump output.

find -x ~ -maxdepth 1  -iname ".bash*"  -type f -exec echo {} \; | hexdump -C 
00000000  2f 55 73 65 72 73 2f 6d  61 63 2f 2e 62 61 73 68  |/Users/mac/.bash|
00000010  20 0a 2f 55 73 65 72 73  2f 6d 61 63 2f 2e 62 61  | ./Users/mac/.ba|
00000020  73 68 5f 68 69 73 74 6f  72 79 0a 2f 55 73 65 72  |sh_history./User|
00000030  73 2f 6d 61 63 2f 2e 62  61 73 68 5f 70 72 6f 66  |s/mac/.bash_prof|
00000040  69 6c 65 0a 2f 55 73 65  72 73 2f 6d 61 63 2f 2e  |ile./Users/mac/.|
00000050  62 61 73 68 72 63 2d 77  69 74 68 2d 63 6f 6c 6f  |bashrc-with-colo|
00000060  72 0a 2f 55 73 65 72 73  2f 6d 61 63 2f 2e 62 61  |r./Users/mac/.ba|
00000070  73 68 72 63 2d 77 69 74  68 2d 63 6f 6c 6f 72 2e  |shrc-with-color.|
00000080  73 68 0a                                          |sh.|
00000083


Could there be some terminal command to end the output then restart the output?


R

PS. I was trying to help out in this thread.

How to delete an extra .bash_profile file found in home directory?

Mac mini, OS X Yosemite (10.10.5), Fall 2014; iPhone 4 7.1.2

Posted on Apr 20, 2017 9:56 AM

Reply
16 replies

Apr 20, 2017 11:25 AM in response to Tony T1

Same bugzoo. I added echo "$i", but it's splitting on a blank in a file name and not displaying a trailing blank. [ the fun of bash. Python anyone? ]


    
#!/bin/bash

    arrayname=( $(find ~ -type f -maxdepth 1  -iname ".bash*") )

    for i in "${arrayname[@]}"
    do
          echo hexdump of "$i"
          echo "$i" | hexdump -C
    done


no joy! It's splitting on blanks .


    for i in "${arrayname[@]}"
additional input:      do
additional input:          echo hexdump of "$i"
additional input:          echo "$i" | hexdump -C
additional input:      done
hexdump of /Users/mac/.bash
00000000  2f 55 73 65 72 73 2f 6d  61 63 2f 2e 62 61 73 68  |/Users/mac/.bash|
00000010  0a                                                |.|
00000011
hexdump of /Users/mac/.bash
00000000  2f 55 73 65 72 73 2f 6d  61 63 2f 2e 62 61 73 68  |/Users/mac/.bash|
00000010  0a                                                |.|
00000011
hexdump of <
00000000  3c 0a                                            |
00000002
hexdump of /Users/mac/.bash_history
00000000  2f 55 73 65 72 73 2f 6d  61 63 2f 2e 62 61 73 68  |/Users/mac/.bash|
00000010  5f 68 69 73 74 6f 72 79  0a                      |_history.|
00000019
hexdump of /Users/mac/.bash_profile
00000000  2f 55 73 65 72 73 2f 6d  61 63 2f 2e 62 61 73 68  |/Users/mac/.bash|
00000010  5f 70 72 6f 66 69 6c 65  0a                      |_profile.|
00000019
hexdump of /Users/mac/.bashrc-with-color
00000000  2f 55 73 65 72 73 2f 6d  61 63 2f 2e 62 61 73 68  |/Users/mac/.bash|
00000010  72 63 2d 77 69 74 68 2d  63 6f 6c 6f 72 0a        |rc-with-color.|
0000001e
hexdump of /Users/mac/.bashrc-with-color.sh
00000000  2f 55 73 65 72 73 2f 6d  61 63 2f 2e 62 61 73 68  |/Users/mac/.bash|
00000010  72 63 2d 77 69 74 68 2d  63 6f 6c 6f 72 2e 73 68  |rc-with-color.sh|
00000020  0a                                                |.|
00000021
mac $



mac $  \ls -a | cat -vte | grep bash
.bash $
.bash <$
.bash_history$
.bash_profile$
.bashrc-with-color$
.bashrc-with-color.sh$
mac $

Apr 20, 2017 12:44 PM in response to rccharles

rccharles wrote:


I'm interested in finding out if there are special characters like blanks in the file names. I want to print out the file names with hexdump -C



I see, then just change hexdump -C "$i" to echo "$i" | hexdump -C


#!/bin/bash


arrayname=( $(find ~ -type f -maxdepth 1 -iname ".bash*") )


for i in "${arrayname[@]}"

do

echo hexdump of "$i"

echo "$i" | hexdump -C

done



I saw that thread. Why not just use Bob's suggetion of cat -vte with find:


find ~ -type f -maxdepth 1 -iname ".bash*" | cat -vte

Apr 20, 2017 12:37 PM in response to rccharles

rccharles wrote:

My general question was how to put multiple bash commands in one -exec statement.

Isn't that what xargs is for instead of the exec command? -exec has a history of bring pants as far as I can tell.


find ~ -maxdepth 1 -name '.bash*' -print0 | xargs -0 echo | hexdump -C

I don't know how to stop hex dump comingling, even with xargs using the -n 1 batch arg. You can kinda kludge separation via xargs -t …


find ~ -maxdepth 1 -name '.bash*' -print0 | xargs -t -n 1 -0 echo | hexdump -C

Apr 20, 2017 2:07 PM in response to rccharles

sh is the name of the inline script, it could be any name. You can use the semi-colon instead of the plus but the plus would add more than one argument to the inline script.


In your case, if you cd to your home directory then run find the output would be a lot cleaner. This example uses od-


cd; find . -maxdepth 1 -name '.bash*' -type f -exec ksh -c 'for file

do

echo "$file" | od -bc

done' sh {} +

Apr 20, 2017 3:22 PM in response to rccharles

find . -maxdepth 1 -name '.bash*' -type f | cat -vte | perl -ne 'print if / |\^|M-/;'


cat -vte will output any a $ at the end of the name so you can see trailing spaces

cat -vte will change any control characters to ^X notation, including TAB as ^I

cat -vte will change any characters with the high bit set to M-X


The perl command will

print the file name if and only if it contains a space or a ^ or a M-


all other filenames will be ignored.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Using the terminal find command

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.