In the Linux command,find
It’s a more complicated and difficult command to use. When you use this command to search files, you often find that you can find some examples to use, but you can’t find the desired results with a little change of conditions.
Here are some examples to illustrate the use offind
Key points and precautions of the order, explaining the reasons why each condition can work or cannot work.
Find command format
To use a command, you must first understand the format of the command, what parameters to provide and what the function of the parameters is.
See man find for an explanation of this command
- find – search for files in a directory hierarchy.
- find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path…] [expression]
- GNU find searches the directory tree rooted at each given file name by evaluating the given expression from left to right, according to the rules of precedence, until the outcome is known (the left hand side is false for and operations, true for or), at which point find moves on to the next file name.
- The -H, -L and -P options control the treatment of symbolic links. Command-line arguments following these are taken to be names of files or directories to be examined, up to the first argument that begins with
‘-’
, or the argument‘(’
or‘!’
. - If no paths are given, the current directory is used. If no expression is given, the expression -print is used (but you should probably consider using -print0 instead, anyway).
- The expression is made up of options (which affect overall operation rather than the processing of a specific file, and always return true), tests (which return a true or false value), and actions (which have side effects and return a true or false value), all separated by operators. -and is assumed where the operator is omitted.
- If the expression contains no actions other than -prune, -print is performed on all files for which the expression is true.
That is,find
The function of the command is to search for files in the directory hierarchy. By default, the subdirectories of the given directory will be searched recursively. The following expression is used to judge each file name found (the directory name also belongs to the file name) to decide whether to print the searched file name or to perform other operations.
be careful: it is very important to evaluate the expression of each file name in turn,find
The command will pass each file name searched as a parameter to the following expression for evaluation to determine how to process the file. If the expression of a file is evaluated as false, it will continue to evaluate the next file, unless the finished operation is executed actively.
Understanding this will make it clear why some file names are printed and some are not because they are not related to each other.
The details are as followsfind
The meaning of each part of the command format:
-
[-H] [-L] [-P] [-D debugopts] [-Olevel]
This part belongs to the command option, which is rarely used, and will not be explained here. -
[path...]
This parameter specifies which directory to look for. Multiple directory names can be provided at the same time, separated by spaces. If this parameter is not provided, the current directory and its subdirectories are searched by default. You can also provide a file name to find the file only in the current directory, not in subdirectories. -
[expression]
This parameter specifies the evaluation expression. Multiple expressions can be provided. Different expressions should be usedoperatorIf no operator is provided between expressions, the – and operator will be used by default. The expression isoption、test、actionThree types. If this parameter is not provided, it is used by default-print
Expression, that is, print out the given file name. Referring to the above description, the expression parameter requires the‘-’
、‘(’
, or‘!’
To distinguish the previous directory parameters. Note that in Bash‘\(’
Right‘(’
To escape.
aboutfind
You can also check the GNU find online help manual for a description of the command https://www.gnu.org/software/… The description is more detailed than man find, and provides many examples for reference.
In Linux, directories are also files,find
When searching, the directory is also treated as a file. The directory name will be found and processed, not just the file name.
If there is no special remark, the file name includes the directory name.
Find all the files in the specified directory
find
The simplest use of the command is to directly execute this command without providing any parameters. By default, all files in the current directory and its subdirectories will be searched and all file names will be printed out.
Specific examples are as follows:
$ ls
Makefile.am src tests
$ find
.
./src
./src/main.c
./tests
./tests/bre.tests
./Makefile.am
As you can see, execute in the shell’s current working directoryfind
Command, which does not provide any parameters, will print out all file names in the current directory and its subdirectories, including directory names.
Can be infind
A directory name is provided after the command to specify which directory to look for:
$ find .
.
./src
./src/main.c
./tests
./tests/bre.tests
./Makefile.am
$ find src
src
src/main.c
$ find src tests
src
src/main.c
tests
tests/bre.tests
In Linux, dot‘.’
Corresponding to the current directory, sofind .
It is to find all the files in the current directory. When no directory parameter is provided, it is used by default‘.’
This parameter.
find src
The command specifies to find onlysrcAll the files in this directory.
find src tests
The command specifies the lookupsrc、testsFor all files in these two directories, multiple directory names can be provided to specify the search of these directories.
find src tests
The command can also be written asfind ./src ./tests
。
Iffind
If the file name is provided after the command, the file is only found in the current directory, not in the subdirectory
$ find Makefile.am
Makefile.am
$ find main.c
find: `main.c': No such file or directory
Combined with the file information printed above, you can see that there is aMakefile.am
Documents,find Makefile.am
The command can find this file without error.
andmain.cThe file is insrcIn the subdirectory,find main.c
Command execution error, indicating that the file can not be found, it will not entersrcSubdirectories.
be carefulAs mentioned earlier, the search condition requires the‘-’
、‘(’
, or‘!’
Before any parameter starting with these characters is encountered, the previous parameter will be regarded as a directory parameter. When searching multiple directories, the previous parameter will be used as a directory parameterfind
After the command, write the paths of these directories and separate them with spaces. There is no need to add – O, – path and other options.
New contactfind
Command, one of the common misunderstandings is that you should use-o
Option to specify to find multiple directories.
For example, think thatfind src -o tests
Yes, search at the same timesrc、testsThis is an incorrect way to write these two directories. An error will be reported during execution:
$ find src -o tests
find: paths must precede expression: tests
As you can see, an error is reported, indicating that the directory path parameter must be provided before the expression parameter.-o
Parameters to-
At the beginning, it is considered an expression parameter. It itself and all parameters after it are considered expression parameters. The directory name provided later is not considered as the directory to be searched.
Some expression parameters can be followed by directory names, but these directory names are not used to specify the search for files in the directory, but have other meanings.
Another misconception is implementationfind src -o tests
After the command reported an error, he didn’t know where the error was-path
Option, mistakenly written asfind src -o -path tests
, orfind src -path -o tests
。 Both commands will report errors, and you will know by self testing.
Although it is written asfind src -path tests
It doesn’t report an error, but it doesn’t print outsrc、testsThe file names in these two directories.
It will be explained later-path
Parameter usage.
Specifies to ignore one or more directories when searching
Based on the directory structure of the above example, if you want to find the file in the current directory, and ignore ittestsDirectory, you can execute the following commands:
$ find . -path ./tests -prune -o -print
.
./src
./src/main.c
./Makefile.am
As you can see, there is no file name in the printtestsThe directory name, and the file name under it.
But if you put it on the top-path
hinder./tests
Change totests
, or will it look fortestsFiles in directory:
$ find . -path tests -prune -o -print
.
./src
./src/main.c
./tests
./tests/bre.tests
./Makefile.am
This result is strange, and I want to ignore it when searchingtestsTable of contents, written as-path ./tests
Can be ignored, written as-path tests
It can’t be ignored.
This is the use offind
Imperative-path
The common error in parameter is that other people’s examples can take effect, but it does not take effect when you write it yourself. This needs to be understood-path
The meaning of the parameter can be used correctly.
As mentioned earlier, operators should be used to separate different expressions. If no operator is provided, it is used by default-and
Operator.
thereforefind . -path ./tests -prune -o -print
The full format of the command isfind . -path ./tests -and -prune -o -print
。
The following is a detailed description of the parameters of this complete command format, so as to understand its working principle and know why it is written as-path ./tests
Can be ignored, written as-path tests
It cannot be ignored.
-path pattern
This is atestType expression, which is described in the GNU find online help manual as follows:
Test: -path pattern
True if the entire file name, starting with the command line argument under which the file was found, matches shell pattern pattern.
To ignore a whole directory tree, use ‘-prune’ rather than checking every file in the tree.
The “entire file name” as used by find starts with the starting-point specified on the command line, and is not converted to an absolute pathname.
That is, whenfind
The file name found by the command exactly matches the one givenpatternMode, the expression returns true.
The key point is,To match exactlyfind
The name found by the command is not a partial match or the absolute pathname of the matching file。
Examples are as follows:
$ find . -path ./tests
./tests
$ find . -path tests
$ find . -path ./tests/
$ find tests
tests
tests/bre.tests
$ find tests -path tests
tests
$ find tests -path ./tests
As you can see,find . -path ./tests
The command is printed./tests
Directory name, butfind . -path tests
The command didn’t print anything.
Check it outfind .
You can see the information printed by the commandtestsThe directory name is./tests
,-path
If the parameter is required to be an exact match, it will return true. Therefore, based on the print result, it is required to write as-path ./tests
Will return true.
As mentioned in the man find description posted above, it is not provided except that-prune
Other than expressionsactionWhen type expression is used, all filenames that return true are executed by default-print
Expression to print the file name.
So the print results only match./tests
Directory names, those don’t exactly match./tests
The file name of will return false and will not be printed.
becausefind .
The directory name printed by the command is not appended/
Character, sofind . -path ./tests/
No file names were matched and no information was printed.
Similarly, executefind tests
Commands, printedtestsThe directory name istests
, sofind tests -path tests
Commands can match exactlytests
Mode, print out the directory name.
andfind tests -path ./tests
It doesn’t match, it doesn’t print.
That is, according to the different directory parameters passed in,find
The directory names printed are different,-path
The directory names to be provided later are also different.
Generally speaking, in the-path
The following directory name needs to match exactlyfind
Command prints the directory name, not a partial match. If you’re not surefind
The directory name printed by the command can be left blank-path
Parameter is executed oncefind
Command to see what the printed file name is, and then write the corresponding file name to-path
After parameter。
stay-path
hinderpatternA pattern can match the file name of a specific pattern with a wildcard*
To match zero or more characters.
stayfind
There are some points that need to be paid attention to when using the
$ find . -path *test*
$ find . -path ./test*
./tests
$ find . -path \*test\*
./tests
./tests/bre.tests
$ find . -path "*test*"
./tests
./tests/bre.tests
As you can see,find . -path *test*
Nothing was printed,*test*
No match to./tests
This name.
The reason is here*
The wildcard is handled by bash. The subdirectory name or file name under the current directory can be obtained through the file name extension, but it will not be added before the directory name./
。
That is, herefind . -path *test*
amount tofind . -path tests
This is not a match, as explained earlier.
find . -path ./test*
You can print the matching directory name. After bash extension, this command is equivalent tofind . -path ./tests
。
find . -path \*test\*
Not only does the command match./tests
Directory, which also matches the./tests/bre.tests
Documents.
It’s used here\*
Yes*
To bash, it is no longer a wildcard. Instead, it does not do extension processing*
This character is passed tofind
Order, byfind
The command itself is processed with wildcards to match more files.
This involves Bash and find*
The difference between wildcard extension and Bash extension in file name*
When, a slash character is encountered/
The file name under the directory will not be extended.
And find didn’t/
As a special character, it will continue to expand to the file name under the directory.
Check out the GNU find online help manual https://www.gnu.org/software/… The following is a description of:
Slash characters have no special significance in the shell pattern matching that find and locate do, unlike in the shell, in which wildcards do not match them.
find . -path "*test*"
The print result of the command followsfind . -path \*test\*
The same.
The reason is that bash doesn’t put double quotes*
As a wildcard, this character will be passed to find, and find will handle the extension of the wildcard.
If you don’t want to use it\*
You can use double quotation marks to enclose the pattern string.
be careful: Although-path
The name of the expression appears to be the corresponding directory path, but it can also be used to match file names, not just directories.
In man find, there is one-wholename
Expressions and-path
Expressions are equivalent, but only supported by the GNU find command-wholename
Expression, which is not supported in other versions of the find command. By name,-wholename
The expression expresses exactly the name of the file to be exactly matched.
-and
This is aoperatorThe GNU find online help manual describes the operator as follows:
expr1 expr2
expr1 -a expr2
expr1 -and expr2
And; expr2 is not evaluated if expr1 is false.
As you can see,-and
Operators are written in three different ways, all of which are equivalent.
find
The operator of the command combines multiple expressions to form a new combination expression. The combined expression will also have its own return value.
use-and
The combined expression of the operator requires that both expressions are true, and the combined expression is true.
sinisterexpr1When the expression is false, the on the right is no longer evaluatedexpr2Expression, which returns false.
In the example abovefind . -path tests
The command didn’t print anything, just follow-and
The properties of the operator are related.
Because the command is not providedactionType expression is added by default-print
Expression, that isfind . -path tests -print
。
Due to the-path tests
and-print
There is no operator provided between, which will be added by default-and
Operator, that isfind . -path tests -and -print
。
andfind .
All filenames found by the command do not match-path tests
Mode, all return false, based on-and
The property of the operator is not executed-print
Expression, and no file name will be printed.
-prune
This is aactionType expression, which is described in the GNU find online help manual as follows:
Action: -prune
If the file is a directory, do not descend into it. The result is true.
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 . -wholename './src/emacs' -prune -o -print
The above command will not print ./src/emacs among its list of results. This however is not due to the effect of the ‘-prune’ action (which only prevents further descent, it doesn’t make sure we ignore that item).
Instead, this effect is due to the use of ‘-o’. Since the left hand side of the “or” condition has succeeded for ./src/emacs, it is not necessary to evaluate the right-hand-side (‘-print’) at all for this particular file.
The example given here is similar to the example we are discussing now. It also explains the reason why the directory can be ignored when searching, for reference.
As mentioned earlier,find
The command will pass each file name searched as a parameter to the following expression for evaluation.
If passed to-prune
If the file name of the expression is a directory, it will not be searched in that directory.
The return value of this expression is always true.
Examples are as follows:
$ find . -path \*test\* -prune
./tests
$ find . -path \*test\* -o -prune
.
As mentioned in the previous example,find . -path \*test\*
Will match to./tests
Directory and the./tests/bre.tests
Documents.
And here it isfind . -path \*test\* -prune
Match only to./tests
Directory. If you do not enter the directory to search for files, you will be affected-prune
Expression.
Based on the previous explanation,find . -path \*test\* -prune
amount tofind . -path \*test\* -and -prune -and print
。
For mismatches\*test\*
The file name of the pattern,-path \*test\*
The expression returns false. It will not be processed further and the unmatched file name will not be printed.
For matching\*test\*
The file name of the pattern,-path \*test\*
If the expression returns true, it will be processed down and encountered-prune
expression. The expression always returns true and continues to work on it-print
Expression to print out the directory name.
because-prune
The expression specifies that it will not enter the corresponding directory, so the file in the directory is not found./tests/bre.tests
Documents.
-o
This is aoperatorThe GNU find online help manual describes the operator as follows:
expr1 -o expr2
expr1 -or expr2
Or; expr2 is not evaluated if expr1 is true.
use-o
The combination expression of the operator requires that both expressions are false, and the combined expression is false.
sinisterexpr1When the expression is true, the on the right is no longer evaluatedexpr2Expression that returns true.
As mentioned earlier,find . -path tests
The command didn’t print anything. It was used-and
Operator, if changed to-o
Operator, the result will be different.
Specific examples are as follows:
$ find . -path tests -o -print
.
./src
./src/main.c
./tests
./tests/bre.tests
./Makefile.am
$ find . -path ./tests -o -print
.
./src
./src/main.c
./tests/bre.tests
./Makefile.am
As you can see,find . -path tests -o -print
The command prints all file names in the current directory.
because-path tests
If nothing is matched, false is returned-o
The properties of the operator are all implemented later-print
Expression to print all file names.
This result is consistent withfind . -path tests
The order is the opposite.
allied,find . -path ./tests -o -print
The print result of the command followsfind . -path ./tests
The order is the opposite.
The former does not contain./tests
Directory name, the latter print results only contain./tests
Directory name.
For matching-path ./tests
The directory name of the pattern, which returns true based on-o
The property of the operator is not executed-print
Expression, so the directory name is not printed.
This is aactionType expression, which is described in the GNU find online help manual as follows:
Action: -print
True; print the entire file name on the standard output, followed by a newline.
The previous example has shown-print
Expression, it will print the full file path name passed down, will automatically add line breaks.
If not provided, in addition to-prune
Other thanactionType expression,find
It will be added by default-print
Expression with-and
To join the previous expression.
This behavior may lead to some misunderstandingfind
The command always prints the file names that are found or matched, but sometimes the file names that are found or matched are not printed.
For example, abovefind . -path ./tests -o -print
For example.
To eliminate this misunderstanding, we must clearly realize that,find
If the command wants to print the file name, it must be executed to-print
Expression, or other expression that prints the file name.
That is, the file name will be printed only if the expression that can print the file name is executed; otherwise, it will not be printed.
Whether the file name matching the specific pattern will be printed or not will be printed only if the file name does not match the specific pattern depends on the judgment result of each expression, including the operator combination expression, to see whether the expression that can print the file name will be executed.
summary
Combined with the above description, yesfind . -path ./tests -and -prune -o -print
The command can be ignored when searching./tests
The reasons for the files under the directory are summarized as follows:
-
find .
Specify to find all the files in the current directory and its subdirectories. Every time a file name is found, the file name is passed to the following expression for evaluation and processing. -
-path ./tests
Specifies that the passed file names should match exactly./tests
This string. If the expression returns false for mismatched file names, then-path ./tests -and -prune
This composite expression returns false and does not evaluate the following-prune
expression. because-and
Operator precedence over-o
The combination expression is followed by the-o -print
Forms a new composite expression that returns false and is executed down-print
Expression to print out the mismatched file name. - For matching
-path ./tests
The directory name of the pattern, which returns true,-path ./tests -and -prune
The combined expression evaluates the following-prune
Expression, which specifies not to enter the matching directory name to find the file under it. In this example, it is not entered./tests
Directory, so the files under the directory will be ignored when searching, but the files will still be found./tests
The directory name itself. - about
./tests
This directory name, because-prune
Returns true,-path ./tests -and -prune
The composite expression returns true based on-o
Operator, do not execute the following-print
Expression, so the directory name is not printed. - final
-o -print
If these two parameters are not added, the mismatch will not be printed./tests
The file name of the pattern. - Based on these analyses, the final printing result of this command does not contain
./tests
This directory name does not contain the file name under it.
In general, usingfind
If you want to ignore a directory, you can use similarfind . -path ./tests -prune -o -print
This way of writing.
After understanding the above description of this command, it should be easier to ignore directories of other modes.
Ignore multiple directory writing
If you want to ignore multiple directories, use the-o
Operators put multiple-path pattern
Expressions.
The directory structure based on the above example is as follows:
$ find . \( -path ./tests -o -path ./src \) -prune -o -print
.
./Makefile.am
As you can see,find . \( -path ./tests -o -path ./src \) -prune -o -print
The search result printed by the command does not contain./src
、./tests
These two directories and their underlying files are ignored.
be based on-o
The properties of the operator,-path ./tests -o -path ./src
The combined expression does not match at./tests
Pattern, it tries to match again./src
Pattern. If both patterns do not match, false is returned.
because-and
Operator precedence over-o
Operator, so use parentheses()
hold-path ./tests -o -path ./src
Combined expressions are enclosed to form a separate expression, followed by-prune
Combine them into a new expression.
Parentheses have a special meaning in Bash, so they should be added\
Escape characters, written as\(
To avoid bash’s special treatment of parentheses.
be careful: in\(
and\)
Separate the front and back with a space. These two are separate operators. If no space is added, they will be combined into other names.
For the meaning and function of other expressions, please refer to the previous examples.
If you can deduce the print result based on the function of each expression and operator of this command, you will understand itfind
How the command works.
File names that match a specific pattern
It’s stated above-path pattern
If you want to match only the file name without the directory path part, you can use the-name pattern
expression.
This is atestType expression, which is described in the GNU find online help manual as follows:
Test: -name pattern
True if the base of the file name (the path with the leading directories removed) matches shell pattern pattern. As an example, to find Texinfo source files in /usr/local/doc:
find /usr/local/doc -name '*.texi'
Notice that the wildcard must be enclosed in quotes in order to protect it from expansion by the shell.
As the example given in this help note, this expression is commonly used to match a file with a specific suffix. Specific examples are as follows.
Match single suffix
Here is an example of matching a single suffix name:
$ find . -name '*.c'
./src/main.c
As you can see,find . -name '*.c'
The command prints out all suffix names as.c
The file name of.
be careful*.c
Use quotation marks to avoid bash when*
The number is treated as a wildcard.
The order is equivalent tofind . -name '*.c' -and -print
, only-name '*.c'
Only when the expression returns true will the file name be executed-print
Expression to print out the file name.
be careful: Using-name pattern
The expression does not mean that only matches are foundpatternThe file of the pattern,find
The command will still find all the files in the given directory and pass each file name to the following expression for evaluation-name pattern
The file name of the expression will return true and be printed.
Files that do not match this expression will also be found, but not printed.
Match multiple suffixes
Here is an example of matching multiple suffixes:
$ find . -name '*.c' -o -name '*.am'
./src/main.c
./Makefile.am
$ find . \( -name '*.c' -o -name '*.am' \) -and -print
./src/main.c
./Makefile.am
$ find . -name '*.c' -o -name '*.am' -and -print
./Makefile.am
As you can see,find . -name '*.c' -o -name '*.am'
The command prints out all suffix names as.c
and.am
The file name of.
The order is equivalent tofind . \( -name '*.c' -o -name '*.am' \) -and -print
, rather than equivalent tofind . -name '*.c' -o -name '*.am' -and -print
, the latter can only print the suffix.am
The file name of.
It’s also explained earlier,find
The command executes by default for all filenames that return true-print
Expression, which returns true, is the judgment result of the whole expression provided manually.
That is to say, the entire expression provided manually should be enclosed in parentheses to form a separate expression, and then be added by default-and -print
Expressions are combined into new expressions to avoid direct addition-and -print
After that, it will be affected by the priority of the operator, and the print result may not meet the expectation.
Reformat the file name information to be printed
In addition to using-print
In addition to the expression print file name, you can also use the-printf
Expression formats the file name information to print.
This is aactionType expression, which is described in the GNU find online help manual as follows:
Action: -printf format
True; print format on the standard output, interpreting ‘’ escapes and ‘%’ directives.
Field widths and precisions can be specified as with the printf C function.
Unlike ‘-print’, ‘-printf’ does not add a newline at the end of the string. If you want a newline at the end of the string, add a ‘n’.
That is,-printf
Expressions are similar to the C languageprintfFunction to format the information to be printed. Some supported formats are as follows:
- %p
File’s name.
This format contains the file name of the full path.
- %f
File’s name with any leading directories removed (only the last element).
This format contains only the file name, and the directory path part is removed.
be careful:-printf
yesactionType expression, as mentioned earlier, if provided in addition to-prune
OutsideactionType expression, will not be added automatically-print
expression.
Yes-printf
Expression will be used by this expression to determine the file information to be printed.
use-printf
Examples of expressions are as follows:
$ find . \( -name '*.c' -o -name '*.am' \) -printf "%f \t%p\n"
main.c ./src/main.c
Makefile.am ./Makefile.am
As you can see, the given find command prints out the file name itself with the specified suffix, as well as the file name of the full path.
-name '*.c' -o -name '*.am'
Expressions need to be enclosed in parentheses to form a separate expression.
If you do not use parentheses, because-and
Operator precedence over-o
Operator,-name '*.am'
Actually, it’s with-printf
Expression combination with suffix.c
The file name of cannot be executed to-printf
Expression, these filenames will not be printed.
because-printf
The expression will not automatically add a newline character at the end. If you want to break a line, you need to add a ‘n’ newline character to the format string.
Match full path file names with regular expressions
stayfind
In the order,-path pattern
Expressions and-name pattern
Expressions use wildcards to match patterns. If you want to match with regular expressions, you can use-regex expr
expression.
This istestType expression, which is described in the GNU find online help manual as follows:
-regex expr
True if the entire file name matches regular expression expr. This is a match on the whole path, not a search.
As for ‘-path’, the candidate file name never ends with a slash, so regular expressions which only match something that ends in slash will always fail.
That is,-regex expr
The expression matches the file name of the full path with a regular expression, including the directory path part.
Match the suffix name with regular expression.c
Examples of documents are as follows:
$ find . -regex '.*\.c'
./src/main.c
$ find . -regex '.*c'
./src
./src/main.c
As you can see,find . -regex '.*\.c'
The command prints only the suffix.c
The file name of.
andfind . -regex '.*c'
In addition to printing the suffix.c
The regular expression of this command is not accurate enough, so it is less critical\.
To escape the matching point number.
Character.
stay.*\.c
In this regular expression, the.
To match any single character,*
Indicates that zero or consecutive preceding characters are matched,\.
To express by means of an escape.
The character itself,c
Representation charactercIn combination, it matches the suffix.c
String of.
and.*c
This regular expression matches the last character asc
String, regardless of the charactercIs there any in front.
Character, this does not meet the requirements of the suffix.
The following example uses regular expressions to match multiple suffixes:
$ find . -regex '.*\.\(c\|am\)'
./src/main.c
./Makefile.am
This example also matches the suffix.c
and.am
The file name of.
In regular expressions,(a|b)
Represents a matcha
Or matchb
。 above\(c\|am\)
After being transferred, that is(c|am)
, for matchingc
perhapsam
This makes it easier to understand. Don’t be scared by the escape character.
Match a specific type of file
In Linux, file types can be divided into directory, regular file, symbolic link, socket, etc.
find
The command can be used-type c
Expression to specify files that match these types.
This is atestType expression, which is described in the GNU find online help manual as follows:
Test: -type c
True if the file is of type c:
d: directory
f: regular file
l: symbolic link
s: socket
For example, use-type f
To specify that only text files are matched:
$ find . -type f
./src/main.c
./tests/bre.tests
./Makefile.am
As you can see, in the print result, there is no directory name, only the text file name.
be careful:-type f
The expression only represents the matching text file, and does not mean that it only looks for the text file,find
The command will still find all the files in the given directory and pass each file name to the following expression for evaluation-type f
Only when the expression file returns true will it be printed.
Files that do not match this expression will also be found, but not printed.