Is it possible to delete files based on modification from the command line?
I have been using an applescript to delete files from a Windows server in 10.3.9 for a while. The script uses the standard format I found on this forum:
tell application “Finder”
delete (every item of folder postscriptFolder whose modification date is less than ((get current date) - 30 * days))
end tell
The script does not work in 10.4.3. It seems like the finder is having problems with the Windows server. Sometimes when it asks the file for it’s modification date, the file does not return a value. It is an intermittent problem, sometime it works, sometimes it doesn’t.
I’m thinking it may be more reliable to try it from the command line, but I don’t have Unix scripting skills. Can anyone help me out, or offer a different approach from the Applescript side?
Try this:
--this doesn't delete anything - it just shows you what files are older than 5 days (the mtime value)
find ~/Documents -type f \! -name ".*" -mtime +5 -exec ls -la {} \;
--CAUTION THIS WILL DELETE ALL THE FILES IT FINDS OLDER THAN 5 DAYS
find /path/to/folder -type f \! -name ".*" -mtime +5 -exec rm -R {} \;
You can also use the -delete flag with find which may be faster.
Additionally. -maxdepth can control how far find descends into directories. ie: maxdepth 1 will only go one level deep.
This:
find /path/to/folder -type f ! -name “.*” -mtime +5 -exec rm -R {} ;
was exactly what I was looking for!
I couldn’t get it to compile with this in it:
“.*”
I also deleted a few other bits and ended up with:
find /path/to/folder -mtime +30 -exec rm -R {} ;
for anyone who doesn’t know, you have to put an extra \ character in front of the one at the end to get it to compile in AS, so it ends up looking like:
find /path/to/folder -mtime +30 -exec rm -R {} \;
It seems to work really well, and is faster than the old AS method I had before. Thanks ChrisAdmin!
If you want to use that, you’ll have to escape the quotes:
do shell script "find /path/to/folder -iname \".*\" -mtime +30 -exec rm -R {} \\;"
It also works with replacing the double quotes with single quotes, i.e. ’ instead of ", which doesn’t require escaping.
My question is how come I can’t get this to work with -maxdepth and -delete? I can get -delete to work but regardless of what -maxdepth specifies, it deletes everything in the target path and subpaths. If I keep the -maxdepth parameter but replace the -delete with “-exec rm -R {} ;” it works as expected. This is on 10.4.3.
Also, can anyone tell me what the “!” (in “-type f !”) is for?
Actually, in this case, the ! is an operator being used with the -iname expression.
Basically, it’s a way to get everything that doesn’t match an expression. In the script above, it has the effect of find all files whose names do not being with a period. (The slash is used to escape the exclamation point, because it has a special meaning to the shell.)
Ah. I should’ve been able to figure that out. Knowing its context helps.
Thanks.
Apparently the -delete option is buggy and does not work as expected… I’ve never used it before - so I would stay away from it. using rm and -exec is the way to go. maxdepth does work though - it’s just that the -delete flag apparently doesn’t care what you set the mexdepth option to. Sorry for the confusion
see this related thread.
http://forums.macosxhints.com/showthread.php?t=13639&highlight=find+delete