Saturday, May 12, 2001

Find Techniques on UNIX

Find a file with given name
find /home/sriram -name core

Find files larger than 1MB
find /home/sriram -size +1000000c -print

Find files larger than 10 KB, but less than 32 KB
(The . denotes current directory)
find . -size +10000c -size -32000c -print

Find files that were modified less than 8, more than 6 days ago
find . -mtime +6 -mtime -8 -print

Find files that were modified exactly 7 days old
find . -mtime 7 -print

Find files that were last accessed more than 30 days ago
find . -atime +30 -print

To delete files older than 200 days
find /home/sriram/ -mtime +200 -exec rm {} \;

find can be used in many other ways. Here are a couple of examples:

  1. To find all subdirectories in your home directory:
    find ~ -type d
  2. To remove all files containing the string trivial in your home directory and subdirectories (and be asked before deleting each file):
    find ~ -name \*trivial\* -exec rm -iv {} \;
  3. To find all files accessed 3 minutes ago:
    find ~ -amin 3
  4. To find all files whose status was changed 5 minutes ago:
    find ~ -cmin 5
  5. To find all empty files:
    find ~ -empty