I use the following command to clear a directory, of files and directories over 30 days old, and move them to an archive directory which I can delete after a few weeks if nobody asks for their files back. The target directory has subdirectories by user name, so will the archive directory.
This is the command I use:
find /path/to/directory/username/ -mtime +30 -exec mv "{}" /path/to/archive/username/ \;
I suggested a modified version of this to answer a question on ask ubuntu, another user edited the code to change the end of line \;
for +
as it's faster(and more correct?). See here
However, using +
in this way works if the -exec
command is ls -lh
but not in the actual command that I use. If I try it with +
I get an error message:
find: missing argument to '-exec'
I don't understand why it's behaving this way, or what the correct command would be. Please don't just post a command correction, I'd like to understand rather than just follow a suggestion blindly.
Best Answer
The user in that post may said that the
+
sign at the end of a-exec
command is faster, but not why.Lets assume the
find
command return the following files:The normal
-exec
command (-exec command {} \;
) runs once for each matching file. For example:Executes:
If you use the
+
sign (-exec command {} +
) the command is build by adding multiple matched files at the end of the command. For example:Executes:
To use the
+
flag correctly the argument to process must be at the end of the command, not in the middle. That's whyfind
trowsmissing argument to '-exec'
in your example; it misses the closing{}
.