Tuesday, March 22, 2011

Deleting Old Files

Deleting old files should be an easy task, right? find can locate them and rm can delete them. But, what's the right combination? xargs must be useful as it converts newline-separated data into space separated data, but the first thing I tried didn't work because there were spaces in some of my file names:

$ find -mtime +90 | xargs ls
ls: cannot access ./download: No such file or directory
...
The xargs man page provides one solution:
...filenames containing blanks and/or new‐lines are incorrectly processed by xargs. In these situations it is better to use the -0 option, which prevents such problems. When using his option you will need to ensure that the program which produces the input for xargs also uses a null character as a separator. If that program is GNU find for example, the -print0 option does this for you.
Another option is to tell xargs to use newline as the delimiter. 'course, I'd recommend that you first try listing the files to see what you'll be deleting. Hence the ls in these examples. You should change to rm when you're ready to destroy.
$ find -mtime +90 -print0 | xargs -0 ls
$ find -mtime +90 | xargs -d "\n" ls

No comments:

Post a Comment