find searches through directory trees beginning with each pathname and finds the files that match the specified condition(s). You must specify at least one pathname and one condition.
Structure: find pathname(s) condition(s)
A wise Unix-master friend once told me, "Never forget that in Unix find is your friend." He was right. The find command is useful for searching for particular files, directories, and patterns in your system.
There are several handy conditions you can use to find exactly what you want. The -name condition will find files whose names match a specified pattern. The structure for the name condition is:
find pathname -name pattern
The condition -print will print the matching files to the pathname specified. -print can also be used in conjunction with other conditions to print the output.
If you wanted to find all the files named favorites.html in the directory john_hughes, then you'd do this:
find /john_hughes -name favorites.html -print
This looks through the directory john_hughes and finds all the files in that directory that contain favorites.html, then prints them to the screen. Your output would look like this:
All meta-characters (!, *, ., etc.) used with -name should be
escaped (place a \ before the character) or quoted.
Meta-characters come in handy when you are searching for a pattern and only
know part of the pattern or need to find several similar patterns. For example, if you are searching for a file
that contains the word "favorite," then use the meta-character
* to represent matching zero or more of the preceding characters. This
will show you all files which contain favorite.
find /john_hughes -name '*favorite*' -print
This looks through the directory john_hughes and finds all the
files in that directory that contain the word "favorite." The output
would look like this:
You can also use find to do recursive operations for commands that don't have recursive options. For example, if you want to grep an entire directory tree, you could use find with grep to do this. If you wanted to find all the index.html files in the john_hughes directory tree, you would type: