Help with multiple input redirections in find -exec command {}


I currently have a cron job that clears out all files in a scratch directory that are older than 14 days:

find SomeDirectory -ctime +14 -print -exec rm -R {} \; > ~/Library/Logs/delete.log 2>&1

I run it once every night, and it makes sure that this directory doesn't grow out of bounds.

I have played with the find command and have managed to print to STDOUT only those files that don't start with a period, because I would like to keep files like .DS_Store in place:

find SomeDirectory -ctime +14 -print | sed -e '/No\ Delete.txt/d' -e '/\/\./d'

This commands also removes any file named "No Delete.txt" from the find output, which would result in its being spared deletion.

How do I properly sandwich the sed command into the -exec rm argument of my cron job?


I find it much easier to use find with xargs. I guess you need command

find . -ctime +14 \! -name "No Delete.txt"' -print0 | xargs -0 rm -r

Replace rm with echo to see how the command works. Find sends the file list to the pipe and xargs reads the pipe, constructing rm call with several file names as arguments. This means that there are fever rm calls, thus running the command faster.

Option -print0 separates the filenames with \0 (ASCII code null), so filenames can have spaces etc. in them.