The find command can optionally pass the files it finds to another command using the -exec option. The syntax is easy, just add -exec 'other_command ;' as an argument to find. But I did that, and it's telling me find: missing argument to `-exec'.

Turns out it just needs the semi-colon escaping

This is another one of those errors that comes up sometimes and for some reason I never remember what I did to fix it last time.

The important thing here is that the semi-colon is not getting passed correctly to the exec option, even though I tried the command inside single quotes so that the shell didn't intercept anything - it still didn't work. You really do just have to escape the semi colon with a backslash so you have -exec other_command \;.
This is actually mentioned in the man page for find, but I always expect the single quotes to fix it

All following arguments to find are
taken to be arguments to the command until an argument consisting of ‘;’ is encoun‐
tered.  The string ‘{}’ is replaced by the current file name being processed every‐
where  it occurs in the arguments to the command, not just in arguments where it is
alone, as in some versions of find.  Both of these constructions might need  to  be
escaped  (with  a  ‘\’) or quoted to protect them from expansion by the shell.

So, the right way to use -exec is something like (to delete files older than 1000 days)

find . -mtime +1000 -exec rm -v \;

and the wrong way is

find . -mtime +1000 -exec 'rm -v ;'

Previous Post Next Post