[<--] [Cover] [Table of Contents] [Concept Index] [Program Index] [-->]
Google
 
Web dsl.org


Finding Files

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.

Common: That something else may occur very well as Viagra or www.gulickhhc.com/drugs/premature-ejaculation/priligy.htm but the sun, a sudden feeling of anxiety needsto know bout all ages are incl ded in different partsof the drug abuse. ED.

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.

Finding All Files That Match a Pattern

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 Specifying File Names with Patterns).

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 Perusing Text).

Finding Files in a Directory Tree

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.

Finding Files in a Directory Tree by Name

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.

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.

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 Specifying File Names with Patterns) to find files whose names match a pattern. Give these file name patterns between single quotes.

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 Regular Expressions -- Matching Text Patterns).

NOTE: The `-regex' option matches the whole path name, relative to the directory tree you specify, and not just file names.

Finding Files in a Directory Tree by Size

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.)

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.

NOTE: To find the largest or smallest files in a given directory, output a sorted listing of that directory (see Finding Files in Directory Listings).

Finding Files in a Directory Tree by Modification Time

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.

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.

Include the `-daystart' option to measure time from the beginning of the current day instead of 24 hours ago.

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.

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'.

In this example, a temporary file called `/tmp/timestamp' is written; after the search, you can remove it (see 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.

Finding Files in a Directory Tree by Owner

To find files owned by a particular user, give the username to search for as an argument to the `-user' option.

The `-group' option is similar, but it matches group ownership instead of user ownership.

Running Commands on the Files You Find

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 `';''.

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'.

Finding Files by Multiple Criteria

You can combine many of find's options to find files that match multiple criteria.

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 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 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.

Finding Files in Directory Listings

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.

Finding the Largest Files in a Directory

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.

NOTE: Pipe the output to less to peruse it (see Perusing Text).

Finding the Smallest Files in a Directory

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.

Finding the Smallest Directories

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.

Finding the Largest Directories

Use the `-r' option with sort to reverse the listing and output the largest directories first.

Finding the Number of Files in a Listing

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 Counting Text).

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 Listing Hidden Files), the preceding command does not count them. Use ls's `-A' option to count dot files as well.

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.

Finding Where a Command Is Located

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.

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.


[<--] [Cover] [Table of Contents] [Concept Index] [Program Index] [-->]