6

When I run the following command, it gives me an error:

$ rm -f `find /home/domain/imap/domain.com/*/Maildir -mtime +190 | grep -E '/cur/|/new/'`
-bash: /bin/rm: Argument list too long

How can I run it without the error? (Or how can I put it in .sh?)

9

Well I would be tempted to do something like this instead, making the entire operation a single command.

find /home/domain/imap/domain.com/ -mtime +190 \
     \( -ipath '*/Maildir/new/*' -o -ipath '*/Maildir/cur/*' \) \
     -delete
2

Simplest way: create a loop, getting the result via the pipe

find [expression] | while read; do [do something with $REPLY, for example] echo "$REPLY"; done

or a temporary file:

FILE="OUT_`date`"
find [expression] > $FILE
while read; do
   [do something with $REPLY, for example] echo "$REPLY"
done < $FILE

Note that if you do a "rm", an argument may look like an option. Consider a file that just happens to be named '-rf'. It is therefore recommended to write rm -- "$FILE" or rm "./$FILE" (or both) or else rm "$PATH/$FILE" if the deletion does not start from the current directory. The single-liner with find does not have that problem. In order to avoid a possible aliasing for rm to a command with options, explicitly call /bin/rm.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy