The "find" Utility
July 19, 1998
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 starting
at the specified point.
There are quite a few possible options.
just take a look at the following list of fairly useful ones:
| Option |
Explanation |
| -atime num_days |
Displays the file if it was accessed (read or executed) within the
given number of days |
| -ctime num_days |
Displays the file if it was changed (inode modified such as a change
in permissions) within the given number of days |
| -exec command {}\; |
Executes the given command on each file found. |
| -group name |
Displays the file if it is owned by the given group |
| -mtime days |
Displays the file if it was modified (file was written to) within
the given number of days |
| -name file_name |
Displays the file if it's name matches the regular expression |
| -newer file |
Displays the file if it is newer than the given file |
| -ok command |
Does the same thing as -exec but asks the user before executing
the command. This is particularly useful if the command you are
executing is "rm" |
| -perm octal |
Displays the file if its permissions match the given octal |
| -print |
Sends the list of matching files to standard output |
| -type flag |
Displays the file if it matches the given type (For example,
d = directory and f=file) |
| -user name |
Displays the file if it is owned by the specified user. |
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.
The "grep" Utility
Introduction to UNIX for Web Developers | Table of Contents
The "sort" Utility
|