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


UNIX in a Nutshell: System V Edition

UNIX in a Nutshell: System V EditionSearch this book
Previous: Reference: file Chapter 2
UNIX Commands
Next: Reference: finger
 

find



find

 

pathname

(s) 

condition(s)

An extremely useful command for finding particular groups of files (numerous examples follow this description). find descends the directory tree beginning at each pathname and locates files that meet the specified conditions . At least one pathname and one condition must be specified. The most useful conditions include -print (which must be explicitly given to display any output), -name and -type (for general use), -exec and -size (for advanced users), and -mtime and -user (for administrators).

Conditions may be grouped by enclosing them in \( \) (escaped parentheses), negated with ! (use \! in the C shell), given as alternatives by separating them with -o , or repeated (adding restrictions to the match; usually only for -name , -type , -perm ).

Conditions

-atime + n | - n | n

Find files that were last accessed more than n ( + n ), less than n ( - n ), or exactly n days ago. Note that find will change the access time of directories supplied as pathnames .

-cpio dev

Take matching files and write them on device dev , using cpio . (SVR3 only.)

-ctime + n | - n | n

Find files that were changed more than n ( + n ), less than n ( - n ), or exactly n days ago. Change refers to modification, permission or ownership changes, etc.; therefore, -ctime is more inclusive than -atime or -mtime .

-depth

Descend the directory tree, skipping directories and working on actual files first (and then the parent directories). Useful when files reside in unwritable directories (e.g., when using find with cpio ).

-exec command { } \;

Run the UNIX command on each file matched by find , (provided command executes successfully on that file; i.e., returns a 0 exit status). When command runs, the argument { } substitutes the current file. Follow the entire sequence with an escaped semicolon ( \; ).

-follow

Follow symbolic links and track the directories visited (don't use this with -type l ).

-fstype type

Find files that reside on file system type .

-group gname

Find files belonging to group gname . gname can be a group name or a group ID number.

-inum n

Find files whose inode number is n .

-links n

Find files having n links.

-local

Find files that physically reside on the local system.

-mount

Search for files that reside only on the same file system as pathname . (Use -xdev on BSD systems.)

-ls

Display matching files with associated statistics (as if run through ls -gilds .

-mtime + n | - n | n

Find files that were last modified more than n ( + n ), less than n ( - n ), or exactly n days ago.

-name pattern

Find files whose names match pattern . Filename metacharacters may be used, but should be escaped or quoted.

-ncpio dev

Take matching files and write them on device dev , using cpio -c .

-newer file

Find files that have been modified more recently than file ; similar to -mtime .

-nogroup

Find files belonging to a group not in /etc/group .

-nouser

Find files owned by a user not in /etc/passwd .

-ok command { } \;

Same as -exec , but user must prompt (with a y ) before command is executed.

-perm nnn

Find files whose permission flags (e.g., rwx ) match octal number nnn exactly (e.g., 664 matches -rw-rw-r-- ). Use a minus sign to make a "wildcard" match of any specified bit (e.g., -perm -600 matches -rw-****** , where * can be any mode).

-print

Print the matching files and directories, using their full pathnames.

-prune

"Prune" the directory tree of unwanted directory searches; that is, skip the directory most recently matched.

-size n [ c ]

Find files containing n blocks, or if c is specified, n characters long.

-type c

Find files whose type is c . c can be b ( b lock special file), c ( c haracter special file), d ( d irectory), p (fifo or named p ipe), l (symbolic l ink), or f (plain f ile).

-user user

Find files belonging to a user name or ID.

Examples

List all files (and subdirectories) in your home directory:



find $HOME -print

List all files named chapter1 in the /work directory:



find /work -name chapter1 -print

List "memo" files owned by ann :



find /work /usr -name 'memo*' -user ann -print

Search the file system (begin at root) for manpage directories:



find / -type d -name 'man*' -print

Search the current directory, look for filenames that don't begin with a capital letter, and send them to the printer:



find . \! -name '[A-Z]*' -exec lp {} \;

Find and compress files whose names don't end with .Z :



compress `find . \! -name '*.Z' -print`

Remove all empty files on the system (prompting first):



find / -size 0 -ok rm {} \;

Skip RCS directories, but list remaining read-only files:



find . -name RCS -prune -o -perm 444 -print

Search the system for files that were modified within the last two days (good candidates for backing up):



find / -mtime -2 -print

Recursively grep for a pattern down a directory tree:



find /book -print | xargs grep '[

Nn

]utshell'


Previous: Reference: file UNIX in a Nutshell: System V Edition Next: Reference: finger
Reference: file Book Index Reference: finger

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