Wednesday, May 28, 2008

Using find to delete old junk files on a system

#find / \( -name a.out -o -name core -o -name '*~'\
-o -name '#*#' \) -type f -atime +14 \
-exec rm -f {} \; -o -fstype nfs -prune


Command explanation:
This command searches the entire filesystem and removes various editor backup files, core dump files, and random executables (a.out) that haven't been accessed in two weeks and that don't reside on a remotely mounted filesystem. The logic is messy: the final -o option ORs all the options that preceded it with those that followed it, each of which is computed separately. Thus, the final operation finds files that match either of two criteria:

*The filename matches, it's a plain file, and it hasn't been accessed for 14 days.

*The filesystem type is nfs (meaning a remote disk).

If the first criteria set is true, the file gets removed; if the second set is true, a "prune" action takes place, which says "don't descend any lower into the directory tree." Thus, every time find comes across an NFS-mounted filesystem, it will move on, rather than searching its entire contents as well.




Got it from OReilly's Essential System Administration 3rd Edition

No comments: