I'm using FreeBSD and having difficulty with commands that include {} as part of a string. For example, if renaming the found files. Note that this is an example of the situation. The question itself is about working around the issues of "{}" syntax in general:
find . -type f -name 'data*' -execdir mv {} OLD_{} \;
find . -type f -name 'data*' -execdir mv {} archive/{} \;
Note that -execdir
avoids issues with the containing dir path by executing the command from within the containing dir and expanding {}
to just the filename.
There are two problems:
- How to correctly quote the args in the
-execdir mv
clause (many files will have spaces or single quotes in their names). - How to get the target to substitute the filename at all.
The second problem arises because {}
for "found item's path" only seems to be expanded if it's surrounded by leading/trailing spaces, which messes up the command args. Example with non-space leading and trailing characters:
# /usr/bin/find . -maxdepth 1 -execdir echo "(result):" {} \;
(result): .
(result): dir 1
(result): dir 2
(result): dir 3
# /usr/bin/find . -maxdepth 1 -execdir echo "(result):"{} \;
(result):
(result):
(result):
(result):
# /usr/bin/find . -maxdepth 1 -execdir echo {}":(result)" \;
:(result)
:(result)
:(result)
:(result)
man find
states that " Historic implementations of the -exec and -ok primaries did not replace the string “{}” in the utility name or the utility arguments if it had preceding or following non-whitespace characters. This version replaces it no matter where in the utility name or arguments it appears", but that doesn't seem to be happening.
How can I execute the command I want to run?
find
doesn't replace the{}
, then surely it should at least leave it as-is? What version of FreeBSD are you running? And what shell?