Unexpected behavior when using eval in "find -exec"
When you execute this find
command:
find . -name '*.png' -exec echo `dirname {}` \;
It effectively executes this command:
find . -name '*.png' -exec echo . \;
It is because command substitution, i.e. the part surrounded by backticks `...`, happens before find
command executes, and {}
is replaced by a single dot.
You can verify this by running bash -cx
(debug turned on):
bash -cx 'find . -name "*.png" -exec echo $(dirname {}) \;'++ dirname '{}'+ find . -name '*.png' -exec echo . ';'..
You can see dirname '{}'
executed first and then find
results are processed.
btw here is a better command to move files to parent directory if you run find
from current directory:
find . -name '*.png' -execdir mv {} .. \;
Note that *.png
should be quoted, otherwise it will expanded by shell even before find
command executes.