I have a directory with many pairs of files. Each non-text file has a textual description partner. For example a directory can look like this:
a.jpg
a.jpg.text
b.ogv
b.ogv.text
cd ef.JpG
cd ef.JpG.text
I want to confirm that there are no loose text files (description files of something already deleted). So I've attempted to execute the following:
find . -name '*.text' -exec if [ ! -f `basename -s .text {}` ]; then echo {}; fi \;
However, now I get an error:
bash: syntax error near unexpected token `then'
You just can't use
if
inside of an-exec
action offind
command in the way you may wish. This because of the following reasons:-exec
action expects a command. This is fromman find
(somewhere at the line 697 in my terminal):if
is not a command as you mat think, but a shell keyword. See the output oftype if
command.Now, to accomplish what you wish, you don't really need to use
if
inside of-exec
. Just make the test inside of-exec
, then use-print
(seeman find
for more info):Another way would be to use a bash script, as follow:
where
cat ~/bin/my_if
gives the following output in my case:Finally, I think that all the normal people using bash would use:
Here is a non-optimized way to use an
if
in anexec
clause:and here is something much better that doesn't launch one shell per file: