home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 23.21 Removing Every File but One Chapter 23
Removing Files
Next: 24. Other Ways to Get Disk Space

23.22 Using find to Clear Out Unneeded Files

Do you run find on your machine every night? Do you know what it has to go through just to find out if a file is three days old and smaller than 10 blocks or owned by "fred" or setuid root? This is why I tried to combine all the things we need done for removal of files into one big find script:


#! /bin/sh
# cleanup - find files that should be removed and clean them
# out of the file system.

find / \(    \( -name '#*'                 -atime +1 \)  \
        -o   \( -name ',*'                 -atime +1 \)  \
        -o   \( -name rogue.sav            -atime +7 \)  \
        -o   \(      \( -name '*.bak'                    \
                     -o -name '*.dvi'                    \
                     -o -name '*.CKP'                    \
                     -o -name '.*.bak'                   \
                     -o -name '.*.CKP' \)  -atime +3 \)  \
        -o   \( -name '.emacs_[0-9]*'      -atime +7 \)  \
        -o   \( -name core                           \)  \
        -o   \( -user guest                -atime +9 \)  \
\) -print -exec rm -f {} \; > /tmp/.cleanup 2>&1

[This is an example of using a single find command to search for files with different names and last-access times (see article 17.5 ). As Chris points out, doing it all with one find is much faster, and less work for the disk, than running a lot of separate find s. The parentheses group each part of the expression. The neat indentation makes this big thing easier to read. The -print   -exec at the end removes each file and also writes the filenames to standard output, where they're collected into a file named /tmp/.cleanup -people can read it to see what files were removed. You should probably be aware that printing the names to /tmp/.cleanup lets everyone see pathnames, like /home/joe/personal/resume.bak , that some people might consider sensitive. Another thing to be aware of is that this find command starts at the root directory; you can do the same thing for your own directories. -JP  ]

- CT in net.unix-wizards on Usenet, 9 June 1985

Previous: 23.21 Removing Every File but One UNIX Power Tools Next: 24. Other Ways to Get Disk Space
23.21 Removing Every File but One Book Index 24. Other Ways to Get Disk Space

The UNIX CD Bookshelf Navigation The UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System