The full syntax format for the Find command is as follows:
find [path] [options] [tests] [actions]
- Path
The path section is easy to understand: You can use either an absolute path, such as/bin, or a relative path, such as. 。 You can also specify multiple paths, such as find/var/home, if desired.
- Options
The Find command has many options available, and the following table lists several of the most commonly used options.
Options |
Meaning |
-depth |
Search the contents of the directory before viewing the directory itself |
-follow |
Follow Symbolic Links |
-maxdepths N |
Search for N -tier directories up to |
-mount (or-xdev) |
Do not search directories in other file systems ( such as Windows and Linux dual systems, this option can be used to bypass the Windows system to find the disk ) |
- Tests
In the test section, there are very many tests that can be provided to the Find command, and each test returns two possible results: TRUE or FALSE. When the find command starts working, it applies each test that is defined in order to each file it searches for at a time. If a test returns the False,find command, it stops processing the file it currently finds and continues the search. If a test returns the True,find command, it will continue with the next Test or take action on its current file (actions). The following table lists the commonly used tests.
Test |
Meaning |
-atime N |
The file was last accessed N days ago |
-mtime N |
The file was last modified after N days. |
-name pattern |
The file name (not including the pathname) matches the pattern provided, in order to ensure that the pattern is passed to the Find command instead of being handled by the shell, thepattern must always be enclosed in double quotes |
-newer Otherfile |
Files are newer than otherfile files |
-type C |
The file type of c,c is a special type. The most common are D (directories) and F (plain files). Other available types can refer to the man page. |
-user username |
The owner of the file is the specified user username |
In shell scripting, you can also use operators to combine tests. Most operators have two formats: short format and long format. See table below:
operator, short form |
operator, long format |
Meaning |
! |
-not |
Test inversion |
-A |
-and |
All two tests must be true |
-O |
-or |
Two tests have a must be true |
You can also enforce the precedence of tests and operators by using parentheses. Since parentheses have a special meaning for the shell, you must also use backslashes to refer to parentheses. In addition, if the matching pattern is used at the file name, quotation marks must be used on the pattern to ensure that the pattern is not extended by the shell, but is passed directly to the Find command. For example, if you want to write a test that "searches for a file that is newer than file X, or if the file name starts with an underscore", you can write:
\ (-newer x-o-name "_*" \)
- actions
It is now possible to search for files reliably. Here's a look at the actions you can perform after discovering a file that matches a specified condition . The following table shows a few common actions.
action |
|
-exec com Mand |
executes a command, which is one of the most common actions. This action must be used \; () character pair to end. |
-ok command |
similar to-exec, but it prompts the user for confirmation before executing the command for each file to be processed. This action must be used \; The character pair to end. |
-print |
print Item name |
-ls |
for the current article Use command ls-dils |
The-exec and-OK commands use subsequent parameters on the command line as part of their arguments, knowing that the sequence terminates. In fact, the-exec and-OK commands perform an embedded command, so the embedded command must end with an escaped semicolon so that the find command can decide when it can continue to find the command-line options for its own.
The find command in shell programming