The standard command find
lets you match files based on metadata (name, permissions, etc.) and act on them. It traverses a directory recursively. The basic syntax is find
followed by the directory(s) to traverse, followed by match criteria, optionally followed by a command to execute (if you don't specify a command, find
prints the matching names).
find /var/spool/postfix/maildrop -user web2 -exec mv -t /var/spool/postfix/temp-spam {} +
This executes the command mv -t /var/spool/postfix/temp-spam …
for the matching files: {}
is replaced by the list of all matching files (if the list is too long, find
will execute mv
as many times at it takes).
The -t
option to mv
is a GNU (Linux/Cygwin) extension. On systems that don't have it, you need to pass the file names to move first, and then the destination. The -exec … +
action only allows passing the file names at the end of the command line, so you need to resort to other methods.
find /var/spool/postfix/maildrop -user web2 -exec mv {} /var/spool/postfix/temp-spam \;
(calls mv
separately for each file)
find /var/spool/postfix/maildrop -user web2 -exec sh -c 'mv "$@" "$0"' /var/spool/postfix/temp-spam {} +
(uses an intermediate shell to get the arguments in the desired order)
If you are using zsh, you can use its glob qualifiers to perform the same job as find
. Here, the u
flag to match files owned by a user.
mv /var/spool/postfix/maildrop/*(u:web2:) /var/spool/postfix/temp-spam
(Unlike the find
commands above, this doesn't recurse into subdirectories. It isn't needed here, but if you need it, insert **/
in the path to say “zero or more subdirectories”, e.g. /var/spool/postfix/maildrop/**/*(u:web2:)
.)