Find -exec dry run?
You can run echo rm
instead of rm
find . type f -exec echo rm {} \;
Also, find
has -delete
option to delete files it finds
For rm
specifically, you don't need -exec
: simply run find . -type f
to list, and add -delete
to delete the files listed by the previous command (obviously barring any matching files being created/deleted in the meantime).
Also, for commands like rm
which take an arbitrary number of arguments you'll want to replace \;
with +
to run as few commands as possible.
It's a bit of a mouthful, but unlike approaches using echo
, the below outputs code you could run in your shell without any changes to have the correct result, even when your filenames contain quotes, spaces, shell metacharacters, etc.
printcmd() { printf '%q ' "$@"; printf '\n'; }
find . -exec bash -c "$(declare -f printcmd); "'printcmd "$@"' _ \
somecommand {} \;
Note that the string we're prepending to our -exec
argument is precisely bash -c "$(declare -f printcmd); "'printcmd "$@"' _
-- the $(declare -f printcmd)
expands to the code for the function; after that, we actually call the function with arguments $1
and onward, and put _
as a placeholder for $0
.
You can substitute zsh
or ksh
instead of bash, if you want output escaped for that shell.