| ||||||
|
|
Go to the previous, next chapter. Finding FilesBy default,
NameHere are ways to search for files whose name matches a certain pattern. See section Shell Pattern Matching, for a description of the pattern arguments to these tests. Each of these tests has a case-sensitive version and a case-insensitive version, whose name begins with i. In a case-insensitive comparison, the patterns fo* and F?? match the file names Foo, FOO, foo, fOo, etc.
Base Name Patterns-Test: -name PATTERN -Test: -iname PATTERN True if the base of the file name (the path with the leading directories removed) matches shell pattern pattern. For -iname, the match is case-insensitive. To ignore a whole directory tree, use -prune (see section Directories). As an example, to find Texinfo source files in /usr/local/doc:
Full Name Patterns- Test:-path PATTERN -Test: -ipath PATTERN True if the entire file name, starting with the command line argument under which the file was found, matches shell pattern pattern. For -ipath, the match is case-insensitive. To ignore a whole directory tree, use -prune rather than checking every file in the tree (see section Directories). - Test: -regex EXPR -Test -iregex EXPR True if the entire file name matches regular expression expr. This is a match on the whole path, not a search. For example, to match a file named ./fubar3, you can use the regular expression .*bar. or .*b.*3, but not b.*r3. See section 'Syntax of Regular Expressions' in The GNU Emacs Manual, for a description of the syntax of regular expressions. For -iregex, the match is case-insensitive. Fast Full Name SearchTo search for files by name without having to actually scan
the directories on the disk (which can be slow), you can use the If a pattern is a plain string---it contains no
metacharacters--- The command locate pattern is almost equivalent to find directories -name pattern where directories are the directories for which the
file name databases contain information. The differences are that
the The file name databases contain lists of files that were on the system when the databases were last updated. The system administrator can choose the file name of the default database, the frequency with which the databases are updated, and the directories for which they contain entries. Here is how to select which file name databases
Shell Pattern Matching
You must quote patterns that contain metacharacters to prevent the shell from expanding them itself. Double and single quotes both work; so does escaping with a backslash.
In the Slash characters have no special significance in the shell
pattern matching that LinksThere are two ways that files can be linked together. Symbolic links are a special type of file whose contents are a portion of the name of another file. Hard links are multiple directory entries for one file; the file names all have the same index node (inode) number on the disk. Symbolic Links-Test -lname PATTERN -Test: -ilname PATTERN True if the file is a symbolic link whose contents match shell pattern pattern. For -ilname, the match is case-insensitive. See section Shell Pattern Matching, for details about the pattern argument. So, to list any symbolic links to sysdep.c in the current directory and its subdirectories, you can do: find . -lname '*sysdep.c' - Option: -follow Dereference symbolic links. The following differences in behavior occur when this option is given:
Hard LinksTo find hard links, first get the inode number of the file whose links you want to find. You can learn a file's inode number and the number of links to it by running ls -i or find -ls. If the file has more than one link, you can search for the other links by passing that inode number to -inum. Add the -xdev option if you are starting the search at a directory that has other filesystems mounted on it, such as /usr on many systems. Doing this saves needless searching, since hard links to a file must be on the same filesystem. See section Filesystems. -Test: -inum N File has inode number n. You can also search for files that have a certain number of links, with -links. Directories normally have at least two hard links; their . entry is the second one. If they have subdirectories, each of those also has a hard link called .. to its parent directory. - Test: -links N File has n hard links. TimeEach file has three time stamps, which record the last time that certain operations were performed on the file:
You can search for files whose time stamps are within a certain age range, or compare them to other time stamps. Age RangesThese tests are mainly useful with ranges (+n and -n). - Test: -atime N - Test: -ctime N - Test: -ntime N True if the file was last accessed (or its status changed, or it was modified) n*24 hours ago. @deffn Test -amin n @deffnx Test -cmin n @deffnx Test -mmin n True if the file was last accessed (or its status changed, or it was modified) n minutes ago. These tests provide finer granularity of measurement than -atime et al. For example, to list files in /u/bill that were last read from 2 to 6 hours ago: find /u/bill -amin +2 -amin -6 @end deffn @deffn Option -daystart Measure times from the beginning of today rather than from 24 hours ago. So, to list the regular files in your home directory that were modified yesterday, do find ~ -daystart -type f -mtime 1 @end deffn Comparing TimestampsAs an alternative to comparing timestamps to the current time,
you can compare them to another file's timestamp. That file's
timestamp could be updated by another program when some event
occurs. Or you could set it to a particular fixed date using the touch -t 02010000 /tmp/stamp$$ find /usr -newer /tmp/stamp$$ rm -f /tmp/stamp$$ @deffn Test -anewer file @deffnx Test -cnewer file @deffnx Test -newer file True if the file was last accessed (or its status changed, or it was modified) more recently than file was modified. These tests are affected by -follow only if -follow comes before them on the command line. See section Symbolic Links, for more information on -follow. As an example, to list any files modified since /bin/sh was last modified: find . -newer /bin/sh @end deffn @deffn Test -used n True if the file was last accessed n days after its status was last changed. Useful for finding files that are not being used, and could perhaps be archived or removed to save disk space. @end deffn Size@deffn Test -size n[bckw] True if the file uses n units of space, rounding up. The units are 512-byte blocks by default, but they can be changed by adding a one-character suffix to n:
The size does not count indirect blocks, but it does count blocks in sparse files that are not actually allocated. @end deffn @deffn Test -empty True if the file is empty and is either a regular file or a directory. This might make it a good candidate for deletion. This test is useful with -depth (see section Directories) and -exec rm -rf '{}' ';' (see section Single File). @end deffn Type@deffn Test -type c True if the file is of type c:
@end deffn @deffn Test -xtype c The same as -type unless the file is a symbolic link. For symbolic links: if -follow has not been given, true if the file is a link to a file of type c; if -follow has been given, true if c is l. In other words, for symbolic links, -xtype checks the type of the file that -type does not check. See section Symbolic Links, for more information on -follow. @end deffn Owner@deffn Test -user uname @deffnx Test -group gname True if the file is owned by user uname (belongs to group gname). A numeric ID is allowed. @end deffn @deffn Test -uid n @deffnx Test -gid n True if the file's numeric user ID (group ID) is n. These tests support ranges (+n and -n), unlike -user and -group. @end deffn @deffn Test -nouser @deffnx Test -nogroup True if no user
corresponds to the file's numeric user ID (no group corresponds
to the numeric group ID). These cases usually mean that the files
belonged to users who have since been removed from the system.
You probably should change the ownership of such files to an
existing user or group, using the PermissionsSee section File Permissions, for information on how file permissions are structured and how to specify them. @deffn Test -perm mode True if the file's permissions are exactly mode (which can be numeric or symbolic). Symbolic modes use mode 0 as a point of departure. If mode starts with -, true if all of the permissions set in mode are set for the file; permissions not set in mode are ignored. If mode starts with +, true if any of the permissions set in mode are set for the file; permissions not set in mode are ignored. @end deffn ContentsTo search for files based on their contents, you can use the grep -l thing *.[ch] If you also want to search for the string in files in
subdirectories, you can combine find . -name '*.[ch]' | xargs grep -l thing The -l option causes find . -name '*.[ch]' -print0 | xargs -r -0 grep -l thing For a fuller treatment of finding files whose contents match a
pattern, see the manual page for DirectoriesHere is how to control which directories @deffn Option -maxdepth levels Descend at most levels (a non-negative integer) levels of directories below the command line arguments. -maxdepth 0 means only apply the tests and actions to the command line arguments. @end deffn @deffn Option -mindepth levels Do not apply any tests or actions at levels less than levels (a non-negative integer). -mindepth 1 means process all files except the command line arguments. @end deffn @deffn Option -depth Process each directory's contents before
the directory itself. Doing this is a good idea when producing
lists of files to archive with @deffn Action -prune If -depth is not given, true; do not descend the current directory. If -depth is given, false; no effect. -prune only affects tests and actions that come after it in the expression, not those that come before. For example, to skip the directory src/emacs and all files and directories under it, and print the names of the other files found: find . -path './src/emacs' -prune -o -print @end deffn @deffn Option -noleaf Do not optimize by assuming that
directories contain 2 fewer subdirectories than their hard link
count. This option is needed when searching filesystems that do
not follow the Unix directory-link convention, such as CD-ROM or
MS-DOS filesystems or AFS volume mount points. Each directory on
a normal Unix filesystem has at least 2 hard links: its name and
its . entry. Additionally, its subdirectories (if any)
each have a .. entry linked to that directory. When FilesystemsA filesystem is a section of a disk, either on the
local host or mounted from a remote host over a network.
Searching network filesystems can be slow, so it is common to
make There are two ways to avoid searching certain filesystems. One
way is to tell @deffn Option -xdev @deffnx Option -mount Don't descend directories on other filesystems. These options are synonyms. @end deffn The other way is to check the type of filesystem each file is on, and not descend directories that are on undesirable filesystem types: @deffn Test -fstype type True if the file is on a filesystem of type type. The valid filesystem types vary among different versions of Unix; an incomplete list of filesystem types that are accepted on some version of Unix or another is: ufs 4.2 4.3 nfs tmp mfs S51K S52K You can use -printf with the %F directive to see the types of your filesystems. See section Print File Information. -fstype is usually used with -prune to avoid searching remote filesystems (see section Directories). @end deffn Combining Primaries With OperatorsOperators build a complex expression from tests and actions. The operators are, in order of decreasing precedence:
There are two other tests that can be useful in complex expressions: @deffn Test -true Always true. @end deffn @deffn Test -false Always false. @end deffn To return to the Ready-to-Run Software Super ReadyPak Table of Contents please press here. |
|
|
Email addresses listed on this site may NOT be used for unsolicited commercial email. Ready-to-Run Software, Inc Privacy Statement Portions (c)Copyright, 1996-2005 by
Ready-to-Run
Software, Inc |