Locating files using the find command
The find command is a powerful *nix utility that allows the user to find files located in the file system via criteria such as the file name, when file was last accessed, when the file status was last changed, the file's permissions, owner, group, size, or even number of inodes.
Examples
Find a file "foo.bar" that exists somewhere in the filesystem
$ find / -name foo.bar -print
|
If the file is found the path to the file will be printed as output. On most platforms the -print is optional, however, on some Unix systems nothing will be output without it. Without specifications find searches recursively through all directories.
Find a file without searching network or mounted filesystems
$ find / -name foo.bar -print -xdev
|
This is useful if you have mounted network drives or filesystems that you do not want searched. This can increase search speed greatly if the mounted filesystem is large or over a slow network. "-mount" does the same thing as "-xdev" for compatibility with other versions of find.
Find a file without showing "Permission Denied" messages
$ find / -name foo.bar -print 2>/dev/null
|
When find tries to search a directory or file that you do not have permission to read the message "Permission Denied" will be output to the screen. The 2>/dev/null option sends these messages to /dev/null so that the found files are easily viewed.
Find a file, who's name ends with .bar, within the current directory and only search 2 directories deep
$ find . -name *.bar -maxdepth 2 -print
|
Search directories "./dir1" and "./dir2" for a file "foo.bar"
$ find ./dir1 ./dir2 -name foo.bar -print
|
Search for files that are owned by the user "joebob"
$ find /some/directory -user joebob -print
|
The files output will belong to the user "joebob". Similar criteria are -uid to search for a user by their numerical id, -group to search by a group name, and -gid to search by a group id number.
Find a file that is a certain type. "-type l" searches for symbolic links
Search for directories that contain the phrase "foo" but do not end in ".bar"
The power of find
find becomes extremely useful when combined with other commands. One such combination would be using find and grep together.
$ find ~/documents -type f -name '*.txt' \
-exec grep -s DOGS {} \; -print |
This sequence uses find to look in /users/home/directory/documents for a file (-type f) with a name ending in .txt. It sends the files it finds to the grep command via the -exec option and grep searches the file found for any occurrences of the word "DOG". If the file is found it will be output to the screen and if the word "DOG" is found, within one of the found files, the line that "DOG" occurs in will also be output to the screen.
The ordering of find's options are important for getting the expected results as well as for performance reason.
Visit POWER TOOLS:A Very Valuable Find, by Jerry Peek, for creative ways to use find and important tips about constructing the command's options.
Corrections, comments, questions e-mail scott@hypexr.org
Top of Page
|