[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Sometimes you will need to find files on the system that match given criteria, such as name and file size. This chapter will show you how to find a file when you know only part of the file name, and how to find a file whose name matches a given pattern. You will also learn how to list files and directories by their size and to find the locations of commands.
NOTE: When you want to find files in a directory whose
contents match a particular pattern, search through the files
with grep
---see Searching Text. A method
of searching for a given pattern in the contents of files in different
directories is given in Running Commands on the Files You Find.
See Info file `find.info', node `Top', for more information on finding files.
8.1 Finding All Files That Match a Pattern Quickly locating files that match a pattern. 8.2 Finding Files in a Directory Tree The find tool is for finding files. 8.3 Finding Files in Directory Listings Finding files in directory listings. 8.4 Finding Where a Command Is Located Locating a command.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The simplest way to find files is with GNU locate
. Use it when
you want to list all files on the system whose full path name matches a
particular pattern--for example, all files with the text `audio'
somewhere in their full path name, or all files ending with `ogg';
locate
outputs a list of all files on the system that match the
pattern, giving their full path name. When specifying a pattern, you can
use any of the file name expansion characters (see section Specifying File Names with Patterns).
$ locate audio RET |
$ locate *ogg RET |
$ locate /. RET |
NOTE: locate
searches are not case sensitive.
Sometimes, a locate
search will generate a lot of output. Pipe
the output to less
to peruse it (see section Perusing Text).
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Use find
to find specific files in a particular directory tree,
specifying the name of the directory tree to search, the criteria to
match, and--optionally--the action to perform on the found
files. (Unlike most other tools, you must specify the directory tree
argument before any other options.)
You can specify a number of search criteria, and format the output in
various ways; the following sections include recipes for the most
commonly used find
commands, as well as a list of find
's
most popular options.
8.2.1 Finding Files in a Directory Tree by Name The basic find options. 8.2.2 Finding Files in a Directory Tree by Size Finding files by size. 8.2.3 Finding Files in a Directory Tree by Modification Time Finding files by date. 8.2.4 Finding Files in a Directory Tree by Owner Finding files by owner. 8.2.5 Running Commands on the Files You Find Running commands on the files you find. 8.2.6 Finding Files by Multiple Criteria A list of find's many options.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Use find
to find files in a directory tree by name. Give the name
of the directory tree to search through, and use the `-name' option
followed by the name you want to find.
$ find / -name top RET |
This command will search all directories on the system to which you have
access; if you don't have execute
permission for a directory,
find
will report that permission is denied to search the
directory.
The `-name' option is case sensitive; use the similar `-iname' option to find name regardless of case.
$ find / -iname top RET |
This command would match any files whose name consisted of the letters `top', regardless of case--including `Top', `top', and `TOP'.
Use file expansion characters (see section Specifying File Names with Patterns) to find files whose names match a pattern. Give these file name patterns between single quotes.
$ find / -name 'top*' RET |
$ find / -name 'top???' RET |
$ find / -name 'top?????*' RET |
$ find ~ -iname '*.tex' RET |
$ find /usr/share -name '*farm*' RET |
Use `-regex' in place of `-name' to search for files whose names match a regular expression, or a pattern describing a set of strings (see section Regular Expressions--Matching Text Patterns).
$ find . -regex '.*\(net\|comm\).*' RET |
NOTE: The `-regex' option matches the whole path name, relative to the directory tree you specify, and not just file names.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
To find files of a certain size, use the `-size' option, following it with the file size to match. The file size takes one of three forms: when preceded with a plus sign (`+'), it matches all files greater than the given size; when preceded with a hyphen or minus sign (`-'), it matches all files less than the given size; with neither prefix, it matches all files whose size is exactly as specified. (The default unit is 512-byte blocks; follow the size with `k' to denote kilobytes or `b' to denote bytes.)
$ find /usr/local -size +10000k RET |
$ find ~ -size -300b RET |
$ find / -size 42 RET |
Use the `-empty' option to find empty files--files whose size is 0 bytes. This is useful for finding files that you might not need, and can remove.
$ find ~ -empty RET |
NOTE: To find the largest or smallest files in a given directory, output a sorted listing of that directory (see section Finding Files in Directory Listings).
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
To find files last modified during a specified time, use find
with the `-mtime' or `-mmin' options; the argument you give
with `-mtime' specifies the number of 24-hour periods, and with
`-mmin' it specifies the number of minutes.
$ find /usr/local -mtime 1 RET |
$ find /usr -mmin 5 RET |
To specify a range of time, precede the number you give with either a plus sign (`+') to match times that are equal to or greater than the given argument, or a hyphen or minus sign (`-') to match times that are equal to or less than the given argument.
$ find /usr/local -mtime -1 RET |
$ find /usr -mmin -5 RET |
Include the `-daystart' option to measure time from the beginning of the current day instead of 24 hours ago.
$ find ~ -mtime 1 -daystart RET |
$ find /usr -mtime +356 -daystart RET |
$ find ~ -mtime 2 -mtime -4 -daystart RET |
In the preceding example, the combined options `-mtime 2' and `-mtime -4' matched files that were modified between two and four days ago.
To find files newer than a given file, give the name of that file as an argument to the `-newer' option.
$ find /etc -newer /etc/motd RET |
To find files newer than a given date, use the trick described in the
find
Info documentation: create a temporary file in `/tmp'
with touch
whose timestamp is set to the date you want to search
for, and then specify that temporary file as the argument to
`-newer'.
$ touch -t 05040000 /tmp/timestamp RET $ find ~ -newer /tmp/timestamp RET |
In this example, a temporary file called `/tmp/timestamp' is written; after the search, you can remove it (see section Removing Files and Directories).
NOTE: You can also find files that were last accessed a number of days after they were modified by giving that number as an argument to the `-used' option. This is useful for finding files that get little use--files matching `-used +100', say, were accessed 100 or more days after they were last modified.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
To find files owned by a particular user, give the username to search for as an argument to the `-user' option.
warwick
, type:
$ find /usr/local/fonts -user warwick RET |
The `-group' option is similar, but it matches group ownership instead of user ownership.
audio
group, type:
$ find /dev -group audio RET |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
You can also use find
to execute a command you specify on each
found file, by giving the command as an argument to the `-exec'
option. If you use the string `'{}'' in the command, this string
is replaced with the file name of the current found file when the
command executes. Mark the end of the command with the string
`';''.
$ find ~/html/ -name '*.html' -exec grep organic '{}' ';' RET |
In this example, the command grep organic file
is executed
for each file that find
finds, with file being the name of
each file in turn.
To have find
pause and confirm execution for each file it finds,
use `-ok' instead of `-exec'.
$ find ~ -used +365 -ok rm '{}' ';' RET |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
You can combine many of find
's options to find files that match
multiple criteria.
$ find ~ -name 'top*' -newer /etc/motd RET |
gzip
(having a `.gz' file name extension), type:
$ find ~ -size +2000000c -regex '.*[^gz]' -exec gzip '{}' ';' RET |
The following tables describe many other options you can use with
find
. The first table lists and describes find
's general
options for specifying its behavior. As you will see, find
can
take many different options; see its man
page or its info
documentation for all of them.
OPTION | DESCRIPTION |
-daystart |
Use the beginning of today rather than 24 hours previous for time criteria. |
-depth |
Search the subdirectories before each directory. |
-help |
Output a help message and exit. |
-maxdepth levels |
Specify the maximum number of directory levels to descend in the specified directory tree. |
-mount or -xdev |
Do not descend directories that have another disk mounted on them. |
-version |
Output the version number and exit. |
The following table lists and describes find
's options for
specifying which files to find.
Specify the numeric arguments to these options in one of three ways: preceded with a plus sign (`+') to match values equal to or greater than the given argument; preceded with a hyphen or minus sign (`-') to match values equal to or less than the given argument; or give the number alone to match exactly that value.
OPTION | DESCRIPTION |
-amin minutes |
Time in minutes since the file was last accessed. |
-anewer file |
File was accessed more recently than file. |
-atime days |
Time in days since the file was last accessed. |
-cmin minutes |
Time in minutes since the file was last changed. |
-cnewer file |
File was changed more recently than file. |
-ctime days |
Days since the file was last changed. |
-empty |
File is empty. |
-group group |
Name of the group that owns file. |
-iname pattern |
Case-insensitive file name pattern to match (`report' matches the files `Report', `report', `REPORT', etc.). |
-ipath pattern |
Full path name of file matches the pattern pattern, regardless of case (`./r*rt' matches `./records/report' and `./Record-Labels/ART'. |
-iregex regexp |
Path name of file, relative to specified directory tree, matches the regular expression regexp, regardless of case (`t?p' matches `TIP' and `top'). |
-links links |
Number of links to the file (see section Giving a File More than One Name). |
-mmin minutes |
Number of minutes since the file's data was last changed. |
-mtime days |
Number of days since the file's data was last changed. |
-name pattern |
Base name of the file matches the pattern pattern. |
-newer file |
File was modified more recently than file. |
-path pattern |
Full path name of file matches the pattern pattern (`./r*rt' matches `./records/report'). |
-perm access mode |
File's permissions are exactly access mode (see section Controlling Access to Files). |
-regex regexp |
Path name of file, relative to specified directory tree, matches the regular expression regexp. |
-size size |
File uses size space, in 512-byte blocks. Append size with `b' for bytes or `k' for kilobytes. |
-type type |
File is type type, where type can be `d' for directory, `f' for regular file, or `l' for symbolic link. |
-user user |
File is owned by user. |
The following table lists and describes find
's options for
specifying what to do with the files it finds.
OPTION | DESCRIPTION |
-exec commands |
Specifies commands, separated by semicolons, to be executed on matching files. To specify the current file name as an argument to a command, use `{}'. |
-ok commands |
Like `-exec' but prompts for confirmation before executing commands. |
-print |
Outputs the name of found files to the standard output, each followed by a newline character so that each is displayed on a line of its own. On by default. |
-printf format |
Use "C-style" output (the same as used by the printf
function in the C programming language), as specified by string
format.
|
The following table describes the variables may be used in the format string used by the `-printf' option.
VARIABLE | DESCRIPTION |
\a |
Ring the system bell (called the "alarm" on older systems). |
\b |
Output a backspace character. |
\f |
Output a form feed character. |
\n |
Output a newline character. |
\r |
Output a carriage return. |
\t |
Output a horizontal tab character. |
\\ |
Output a backslash character. |
%% |
Output a percent sign character. |
%b |
Output file's size, rounded up in 512-byte blocks. |
%f |
Output base file name. |
%h |
Output the leading directories of file's name. |
%k |
Output file's size, rounded up in 1K blocks. |
%s |
Output file's size in bytes. |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The following recipes show how to find the largest and smallest files and directories in a given directory or tree by listing them by size. They also show how to find the number of files in a given directory.
8.3.1 Finding the Largest Files in a Directory Finding the largest files. 8.3.2 Finding the Smallest Files in a Directory Finding the smallest files. 8.3.3 Finding the Smallest Directories Finding the largest directories. 8.3.4 Finding the Largest Directories Finding the smallest directories. 8.3.5 Finding the Number of Files in a Listing Counting the number of files you find.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
To find the largest files in a given directory, use ls
to list
its contents with the `-S' option, which sorts files in descending
order by their size (normally, ls
outputs files sorted
alphabetically). Include the `-l' option to output the size and
other file attributes.
$ ls -lS RET |
NOTE: Pipe the output to less
to peruse it
(see section Perusing Text).
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
To list the contents of a directory with the smallest files first, use
ls
with both the `-S' and `-r' options, which
reverses the sorting order of the listing.
$ ls -lSr RET |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
To output a list of directories sorted by their size--the size
of all the files they contain--use du
and sort
. The
du
tool outputs directories in ascending order with the smallest
first; the `-S' option puts the size in kilobytes of each directory
in the first column of output. Give the directory tree you want to
output as an option, and pipe the output to sort
with the
`-n' option, which sorts its input numerically.
$ du -S . | sort -n RET |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Use the `-r' option with sort
to reverse the listing
and output the largest directories first.
$ du -S . | sort -nr RET |
$ du -S /usr/local | sort -nr RET |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
To find the number of files in a directory, use ls
and pipe the
output to `wc -l', which outputs the number of lines in its input
(see section Counting Text).
$ ls | wc -l RET 19 $ |
In this example, the command outputs the text `19', indicating that there are 19 files in the current directory.
Since ls
does not list hidden files by default (see section Listing Hidden Files), the preceding command does not count
them. Use ls
's `-A' option to count dot files as well.
$ ls -A | wc -l RET 81 $ |
This command outputs the text `81', indicating that there are 81 files, including hidden files, in the current directory.
To list the number of files in a given directory tree, and not
just a single directory, use find
instead of ls
, giving
the special find
predicate `\! -type d' to exclude the
listing (and therefore, counting) of directories.
$ find /usr/share \! -type d | wc -l RET |
$ find /usr/share | wc -l RET |
$ find /usr/share \! -type f | wc -l RET |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Use which
to find the full path name of a tool or application
from its base file name; when you give the base file name as an option,
which
outputs the absolute file name of the command that would
have run had you typed it. This is useful when you are not sure whether
or not a particular command is installed on the system.
perl
is installed on your system, and, if so,
where it resides, type:
$ which perl RET /usr/bin/perl |
In this example, which
output `/usr/bin/perl', indicating
that the perl
binary is installed in the `/usr/bin'
directory.
NOTE: This is also useful for determining "which" binary
would execute, should you type the name, since some systems may have
different binaries of the same file name located in different
directories. In that case, you can use which
to find which one
would execute.
[ << ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |