PDA

View Full Version : Help with multiple input redirections in find -exec command {}



DominikHoffmann
January 31st, 2012, 01:30 PM
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?

midijeep
January 31st, 2012, 06:28 PM
I think you need to use the 'find -regex' option'

'rm' command can become a nasty thing if you are not careful.

DominikHoffmann
January 31st, 2012, 11:11 PM
I'm getting help on this in the forums of the Apple Support Communities at <https://discussions.apple.com/message/17459391>. As of this writing the thread is still developing.

artov
February 1st, 2012, 10:53 AM
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.