The "find" Utility
Like the "grep" utility, the "find" utility is very useful for searching throughout UNIX. However, unlike grep, which looks for patterns within files, find looks for files that match a given pattern. The find utility is a little more complex though. Typically, you will use the following syntax:
find [location to start from] [options]
Consider the following example in which we simply look for all files starting in the current directory (".") that have ".bak" extension.
Notice that we used the "-print" option to instruct find to display the list of files to standard output. Notice also that we used the -name option to specify a file type to look for. Finally, notice that find does a recursive search through the directory tree startin at the specified point.
There are quite a few possible options. just take a look at the following list of fairly useful ones:
Let's take a look at some of the more useful ways you can use find
The -exec option is one of the most useful of all the find options because it allows you to execute a command on each file found. Consider the following example in which we modify the permissions of all the files found. Note that as usual, this will be done recursively throughout the directory tree.
|Alternatively, you can use the -ok option to make sure that UNIX prompts you before it executes each command. This is particularly useful for commands such as "rm".|
|Similarly, you can find all files owned by a specific user or group using the -user and -group options. In the following example we demonstrate finding files by username.|
A final cool usage of find is to incorporate it into another command such as "cp". For example in the following command, we copy all .bak files found by find, to the "Backup" directory
cp `find . -name '*.bak' -print` Backup
In this case, the use of back ticks (`) allows us to substitute the results of the command into the rest of the line.
In the next case, we find all files with the keyword
"selena" in them.