Posted: July 15th, 2009 | Author: Troy | Filed under: Solaris | Tags: examples | No Comments »
Searching for Files (find)
The find command searches for files that meet conditions you specify, starting from a directory you name. For example, you might search for file names that match a certain pattern or that have been modified within a specified time frame.
Unlike most commands, find options are several characters long. You must specify the starting directory before your desired options.
In the previous example, directory is the name of the starting directory and options represents the options for the find command.
Each option describes a criterion for selecting a file. A file must meet all criteria to be selected. The more options you apply, the narrower the field becomes. The -print option indicates that you want the system to display the results.
The -name filename option tells find to select files that match filename. Here filename is taken to be the rightmost component of a file’s full path name. For example, the rightmost component of the file /usr/bin/calendar is calendar. This portion of a file’s name is often called the base name.
For example, to see which files within the current directory and its subdirectories end in s, type the following command.
$ find . -name '*s' -print
./programs
./programs/graphics
./programs/graphics/gks
./src/gks
$
|
The following table describes other options of the find command.
Table 3–1 find Options
| Option |
Description |
| -name filename |
Selects files with a rightmost component that matches filename. Surround filename with single quotes if it includes filename substitution patterns. |
| -user userid |
Selects files that are owned by userid. userid can be either a login name or user ID number. |
| -group group |
Selects files that belong to group. |
| -m -time n |
Selects files that have been modified within n days. |
| -newer checkfile |
Selects files that have been modified more recently than checkfile. |
You can specify an order of options by combining options within escaped parentheses (for example, \(options\) ). Within escaped parentheses, you can use the -o flag between options to indicate that find should select files that qualify under either category, rather than just those files that qualify under both categories.
$ find . \( -name AAA -o -name BBB \) -print
./AAA
./BBB
|
In the previous example, the find command searches in the . directory for all files that are named AAA, then looks for all files named BBB. find then displays the results of both searches.
You can invert the sense of an option by including an escaped exclamation point before the option. find then selects files for which the option does not apply:
$ find . \!-name BBB -print
./AAA
|
You can also use find to apply commands to the files it selects with the following options.
-exec command ‘{}’ \;
You terminate this option with an escaped semicolon (\;). The quoted braces are replaced with the file names that find selects.
You can use find to automatically remove temporary work files. If you name your temporary files consistently, you can use find to search for and remove these files. For example, if you name your temporary files junk or dummy, this command finds and removes the files.
$ find . \( -name junk -o -name dummy \) -exec rm '{}' \;
Posted: July 15th, 2009 | Author: Troy | Filed under: Linux | Tags: examples | No Comments »
The following examples illustrate typical uses of the command find for finding files on a computer.
find / -name game
Looks for a file named “game” starting at the root directory (searching all directories including mounted filesystems). The `-name’ option makes the search case sensitive. You can use the `-iname’ option to find something regardless of case.
find /home -user joe
Find every file under the directory /home owned by the user joe.
find /usr -name *stat
Find every file under the directory /usr ending in “stat”.
find /var/spool -mtime +60
Find every file under the directory /var/spool that was modified more than 60 days ago.
find /tmp -name core -type f -print | xargs /bin/rm -f
Find files named core in or below the directory /tmp and delete them. Note that this will work incorrectly if there are any filenames containing newlines, single or double quotes, or spaces.
find /tmp -name core -type f -print0 | xargs -0 /bin/rm -f
Find files named core in or below the directory /tmp and delete them, processing filenames in such a way that file or directory names containing single or double quotes, spaces or newlines are correctly handled. The -name test comes before the -type test in order to avoid having to call stat(2) on every file.
find . -type f -exec file ‘{}’ \;
Runs `file’ on every file in or below the current directory. Notice that the braces are enclosed in single quote marks to protect them from interpretation as shell script punctuation. The semicolon is similarly protected by the use of a backslash, though ‘;’ could have been used in that case also.
find / \( -perm -4000 -fprintf /root/suid.txt ‘%#m %u %p\n’ \), \
\( -size +100M -fprintf /root/big.txt ‘%-10s %p\n’ \)
Traverse the filesystem just once, listing setuid files and directories into /root/suid.txt and large files into /root/big.txt.
find $HOME -mtime 0
Search for files in your home directory which have been modified in the last twenty-four hours. This command works this way because the time since each file was last modified is divided by 24 hours and any remainder is discarded. That means that to match -mtime 0, a file will have to have a modification in the past which is less than 24 hours ago.
find . -perm 664
Search for files which have read and write permission for their owner, and group, but which other users can read but not write to. Files which meet these criteria but have other permissions bits set (for example if someone can execute the file) will not be matched.
find . -perm -664
Search for files which have read and write permission for their owner and group, and which other users can read, without regard to the presence of any extra permission bits (for example the executable bit). This will match a file which has mode 0777, for example.
find . -perm /222
Search for files which are writable by somebody (their owner, or their group, or anybody else).
find . -perm /220
find . -perm /u+w,g+w
find . -perm /u=w,g=w
All three of these commands do the same thing, but the first one uses the octal representation of the file mode, and the other two use the symbolic form. These commands all search for files which are writable by either their owner or their group. The files don’t have to be writable by both the owner and group to be matched; either will do.
find . -perm -220
find . -perm -g+w,u+w
Both these commands do the same thing; search for files which are writable by both their owner and their group.
find . -perm -444 -perm /222 ! -perm /111
find . -perm -a+r -perm /a+w ! -perm /a+x
These two commands both search for files that are readable for everybody (-perm -444 or -perm -a+r), have at least on write bit set (-perm /222 or -perm /a+w) but are not executable for anybody (! -perm /111 and ! -perm /a+x respectively)