Find
From Liki
The find command in linux/unix is for finding files. This is similar to the locate command but with an important difference: locate searches a pre-compiled database of file locations (which may or may not be current), while find performs a realtime search and is highly customizable.
This is one of those cases where the man page makes find seem harder to use than it actually is.
Contents |
Basic Usage
The basic syntax of the find command is:
find (path) [expression]
which we will unpack here. The first argument, path, is the directory in which the search should begin. The default directory if no path is given is the current one (.). Find always searches recusively from the starting point by default. Experession is the complicated part but, it's format is basically
(options) [tests] (actions)
where only tests are required. The tests are where the real searching is done, like finding a file by name. As an example, lets say you want to update your webpages and need to find every .html file in there? Do the following:
cd /my_home_directory/public_html find -name \*.html
where the slash is necessary for find to see the wilcard instead of the shell (you could encase the searching string in quotes instead). The default action of find is then to print out a list of every file matching your test. The -name test matches based on file name and is case sensitive. The case insensitive version is -iname.
Options
There aren't terribly many options to pass to find, but I'll menteion two, -maxdepth (num) and -mindepth (num) which specifiy, respecively, a maximum or minimum directory depth at which to start applying the tests. A max of 0 means stop after the commandline argument, while a min of 1 means start after the commandline arguments.
Tests
File can matched not just by names but also timestamps, ownership, size, etc... Numeric searches, like timestamp, take a number as argument, which can be specified in 3 ways:
n = exactly n +n = greater than n -n = less than n
For example, then "-amin n" test for files accessed n minutes ago. If you type
find /var/log -amin -10"
then you'll be searching for logs that have been updated anytime in the past 10 minutes.
The other searchs are straight forward and can be found in the man page for find.
Actions
The default action is to print the names of the files out, 1 per line. Files can be printed out with -printf [format] which specifies fancier ways to present the results. The man page has a list of formatting symbols and what they do.
Find can also execute commands on the returned files by using -exec [command], which executes the command listed on each returned file. The filename is invoked using the string {}. So, suppose I want to cat each file returned? Try
find /my_home_directory/ -name "todo.list" -exec cat {} \;
where you will need spaces around the {} and a backslash infront of the semicolon. The semicolon indicates we've reached the end of the command, and the slash makes sure the semicolon is interpreted by find and not by the shell.
A typical use of exec is to hand off prelimintary matches to grep to narrowing them down using regular expression matching which find doesn't do. This allows grep to work on the file contents, not just file names, and still alows for further formatting by find (that is, other actions).
Operators
You can have multiple tests by adding -a (and) or -o (or) inbetween them. So if you want files of a certin name and recently edited you can do:
find /var/www/htdocs -name inex.html -a -amin -10
Note that you can omit the "-a" for anding commands. If you have multiple commands in a row, 'and' is the default choice.

