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.
$ 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 / -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 / -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 . -name *.bar -maxdepth 2 -print |
$ find ./dir1 ./dir2 -name foo.bar -print |
$ 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 /some/directory -type l -print |
Several types of files can be searched for:
$ find . -name '*foo*' ! -name '*.bar' -type d -print |
The "!" allows you to exclude results that contain the phrases following it.
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.
转自:http://www.hypexr.org/linux_find_help.php