From today on, I will post UNIX notes on my personal web site. The notes is for personal reference use only, however, if you find it useful, you are warmly welcome here. You are encouraged to contact me.
The find command is handy when you trying to find a file in the filesystem.
syntax:
# find starting-dir(s) matching-criteria-and-actions
The first argument is quite straightforward, the hard part is the second argument. Here is a simple rundown of the the matching criteria
-atime n
File was last accessed exactly n days ago.
-atime -2
Last accessed less than 2 days ago.
-mtime n
File was last modified exactly n days ago.
-mtime +7
Last modified more than 7 days ago.
-newer file
File(s) was modified more recently than file was.
-size n
File is exactly n 512-byte blocks long.
-size +100
larger than 50K.
-type c
Specifies the file type: f=plain file, d=directory, etc.
-fstype typ
Specifies filesystem type.
-name nam
The filename is nam. Wild card is accepted.
-perm p
The file's access mode is p.
-user usr
The file's owner is usr.
-group grp
The file's group owner is grp.
-nouser
The file's owner is not listed in the password file.
-nogroup
The file's group is not listed in the group file.
By default, multiple criteria are joined with AND. To join by OR, see:
\(-atime +7 -o mtime +30 \)
! used as NOT.
! -name gold.dat -name \*.dat
All .dat files except gold.dat file.
Other than criteria, find command can also take actions:
-print
Display pathname for matching file. The default action.
-ls
Display long directory listing for matching file.
-exec cmd
Execute command on file.
-exec rm -f {} \;
delete each matching file as it is found
-ok cmd
Prompt before executing command on file
-xdev
Restrict the search to the filesystem of the starting directory
-prune
Don't descend into directory encountered.
$ find . -name \*.c -print
Prints pathname of all .c files under current directory
$ find /chem -size +2048 -mtime +30 -exec ls -l {} \;
list all files under /chem larger than 1 MB that haven't been modified for 30 days
$ find /chem -size +2048 \( -mtime +30 -o -atime +120 \) -ls
list all files under /chem larger than 1 MB that haven't been modified for 30 days or haven't been accessed for 120 days.
$ find / \( -name a.out -o -name core -o -name '*~' \
-o -name '.*~' -o -name '#*#' \) -type f -atime +14 -exec rm -f {} \; -o -fstype nfs -prune
the last -o separates two criteria and actions:
criteria 1: filename matches, plain file, and wasn't accessed for 14 days.
criteria 2: filesystem type is nfs.
If the criteria 1 is matched, the files get removed; if criteria 2 is matched, it get pruned (ignored). The overall action is to delete every matching files in criteria 1 unless they are on nfs filesystems.
# find /home /aux1 -type f -atime +30 -size +1000 -print \ -name core -exec rm {} \;
# find /home/chavez -exec chown {} \; \
-exec chgrp physics {} \;
changes all files to owner chavez, group owner to physics.
# find /chem -name '*.c' -exec mv {} /chem1/src \;
gathers all .c files under /chem into /chem1/src
# find /chem -name '*.c' -exec /usr/local/bin/prettify {} \;
runs the scrip prettify on every .c file under /chem.
End of 2/27/03's entry.