Linux skills: find command usage introduction, detailed explanation and method of ignoring multiple directories

Time:2020-7-30

In the Linux command,findIt’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 offindKey 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,findThe 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,findThe 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 followsfindThe 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 isoptiontestactionThree types. If this parameter is not provided, it is used by default-printExpression, 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.

aboutfindYou 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,findWhen 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

findThe 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 directoryfindCommand, which does not provide any parameters, will print out all file names in the current directory and its subdirectories, including directory names.

Can be infindA 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 srcThe command specifies to find onlysrcAll the files in this directory.

find src testsThe command specifies the lookupsrctestsFor all files in these two directories, multiple directory names can be provided to specify the search of these directories.

find src testsThe command can also be written asfind ./src ./tests

IffindIf 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.amDocuments,find Makefile.amThe command can find this file without error.

andmain.cThe file is insrcIn the subdirectory,find main.cCommand 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 parameterfindAfter 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 contactfindCommand, one of the common misunderstandings is that you should use-oOption to specify to find multiple directories.

For example, think thatfind src -o testsYes, search at the same timesrctestsThis 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.-oParameters 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 testsAfter the command reported an error, he didn’t know where the error was-pathOption, 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 testsIt doesn’t report an error, but it doesn’t print outsrctestsThe file names in these two directories.

It will be explained later-pathParameter 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-pathhinder./testsChange 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 ./testsCan be ignored, written as-path testsIt can’t be ignored.

This is the use offindImperative-pathThe 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-pathThe 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-andOperator.

thereforefind . -path ./tests -prune -o -printThe 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 ./testsCan be ignored, written as-path testsIt 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, whenfindThe file name found by the command exactly matches the one givenpatternMode, the expression returns true.

The key point is,To match exactlyfindThe 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 ./testsThe command is printed./testsDirectory name, butfind . -path testsThe command didn’t print anything.

Check it outfind .You can see the information printed by the commandtestsThe directory name is./tests-pathIf 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 ./testsWill return true.

As mentioned in the man find description posted above, it is not provided except that-pruneOther than expressionsactionWhen type expression is used, all filenames that return true are executed by default-printExpression to print the file name.

So the print results only match./testsDirectory names, those don’t exactly match./testsThe 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 testsCommands, printedtestsThe directory name istests, sofind tests -path testsCommands can match exactlytestsMode, print out the directory name.

andfind tests -path ./testsIt doesn’t match, it doesn’t print.

That is, according to the different directory parameters passed in,findThe directory names printed are different,-pathThe directory names to be provided later are also different.

Generally speaking, in the-pathThe following directory name needs to match exactlyfindCommand prints the directory name, not a partial match. If you’re not surefindThe directory name printed by the command can be left blank-pathParameter is executed oncefindCommand to see what the printed file name is, and then write the corresponding file name to-pathAfter parameter

stay-pathhinderpatternA pattern can match the file name of a specific pattern with a wildcard*To match zero or more characters.

stayfindThere 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./testsThis 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 testsThis 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./testsDirectory, which also matches the./tests/bre.testsDocuments.

It’s used here\*Yes*To bash, it is no longer a wildcard. Instead, it does not do extension processing*This character is passed tofindOrder, byfindThe 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-pathThe 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-wholenameExpressions and-pathExpressions are equivalent, but only supported by the GNU find command-wholenameExpression, which is not supported in other versions of the find command. By name,-wholenameThe 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,-andOperators are written in three different ways, all of which are equivalent.

findThe operator of the command combines multiple expressions to form a new combination expression. The combined expression will also have its own return value.

use-andThe 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 testsThe command didn’t print anything, just follow-andThe properties of the operator are related.

Because the command is not providedactionType expression is added by default-printExpression, that isfind . -path tests -print

Due to the-path testsand-printThere is no operator provided between, which will be added by default-andOperator, that isfind . -path tests -and -print

andfind .All filenames found by the command do not match-path testsMode, all return false, based on-andThe property of the operator is not executed-printExpression, 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,findThe command will pass each file name searched as a parameter to the following expression for evaluation.

If passed to-pruneIf 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./testsDirectory and the./tests/bre.testsDocuments.

And here it isfind . -path \*test\* -pruneMatch only to./testsDirectory. If you do not enter the directory to search for files, you will be affected-pruneExpression.

Based on the previous explanation,find . -path \*test\* -pruneamount 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-pruneexpression. The expression always returns true and continues to work on it-printExpression to print out the directory name.

because-pruneThe expression specifies that it will not enter the corresponding directory, so the file in the directory is not found./tests/bre.testsDocuments.

-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-oThe 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 testsThe command didn’t print anything. It was used-andOperator, if changed to-oOperator, 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 -printThe command prints all file names in the current directory.

because-path testsIf nothing is matched, false is returned-oThe properties of the operator are all implemented later-printExpression to print all file names.

This result is consistent withfind . -path testsThe order is the opposite.

allied,find . -path ./tests -o -printThe print result of the command followsfind . -path ./testsThe order is the opposite.

The former does not contain./testsDirectory name, the latter print results only contain./testsDirectory name.

For matching-path ./testsThe directory name of the pattern, which returns true based on-oThe property of the operator is not executed-printExpression, so the directory name is not printed.

-print

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-printExpression, it will print the full file path name passed down, will automatically add line breaks.

If not provided, in addition to-pruneOther thanactionType expression,findIt will be added by default-printExpression with-andTo join the previous expression.

This behavior may lead to some misunderstandingfindThe 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 -printFor example.

To eliminate this misunderstanding, we must clearly realize that,findIf the command wants to print the file name, it must be executed to-printExpression, 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 -printThe command can be ignored when searching./testsThe 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 ./testsSpecifies that the passed file names should match exactly./testsThis string. If the expression returns false for mismatched file names, then-path ./tests -and -pruneThis composite expression returns false and does not evaluate the following-pruneexpression. because-andOperator precedence over-oThe combination expression is followed by the-o -printForms a new composite expression that returns false and is executed down-printExpression to print out the mismatched file name.
  • For matching-path ./testsThe directory name of the pattern, which returns true,-path ./tests -and -pruneThe combined expression evaluates the following-pruneExpression, which specifies not to enter the matching directory name to find the file under it. In this example, it is not entered./testsDirectory, so the files under the directory will be ignored when searching, but the files will still be found./testsThe directory name itself.
  • about./testsThis directory name, because-pruneReturns true,-path ./tests -and -pruneThe composite expression returns true based on-oOperator, do not execute the following-printExpression, so the directory name is not printed.
  • final-o -printIf these two parameters are not added, the mismatch will not be printed./testsThe file name of the pattern.
  • Based on these analyses, the final printing result of this command does not contain./testsThis directory name does not contain the file name under it.

In general, usingfindIf you want to ignore a directory, you can use similarfind . -path ./tests -prune -o -printThis 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-oOperators put multiple-path patternExpressions.

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 -printThe search result printed by the command does not contain./src./testsThese two directories and their underlying files are ignored.

be based on-oThe properties of the operator,-path ./tests -o -path ./srcThe combined expression does not match at./testsPattern, it tries to match again./srcPattern. If both patterns do not match, false is returned.

because-andOperator precedence over-oOperator, so use parentheses()hold-path ./tests -o -path ./srcCombined expressions are enclosed to form a separate expression, followed by-pruneCombine 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 itfindHow the command works.

File names that match a specific pattern

It’s stated above-path patternIf you want to match only the file name without the directory path part, you can use the-name patternexpression.

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.cThe file name of.

be careful*.cUse 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-printExpression to print out the file name.

be careful: Using-name patternThe expression does not mean that only matches are foundpatternThe file of the pattern,findThe command will still find all the files in the given directory and pass each file name to the following expression for evaluation-name patternThe 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.cand.amThe 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.amThe file name of.

It’s also explained earlier,findThe command executes by default for all filenames that return true-printExpression, 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 -printExpressions are combined into new expressions to avoid direct addition-and -printAfter 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-printIn addition to the expression print file name, you can also use the-printfExpression 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,-printfExpressions 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-printfyesactionType expression, as mentioned earlier, if provided in addition to-pruneOutsideactionType expression, will not be added automatically-printexpression.

Yes-printfExpression will be used by this expression to determine the file information to be printed.

use-printfExamples 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-andOperator precedence over-oOperator,-name '*.am'Actually, it’s with-printfExpression combination with suffix.cThe file name of cannot be executed to-printfExpression, these filenames will not be printed.

because-printfThe 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

stayfindIn the order,-path patternExpressions and-name patternExpressions use wildcards to match patterns. If you want to match with regular expressions, you can use-regex exprexpression.

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 exprThe 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.cExamples 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.cThe file name of.

andfind . -regex '.*c'In addition to printing the suffix.cThe regular expression of this command is not accurate enough, so it is less critical\.To escape the matching point number.Character.

stay.*\.cIn 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,cRepresentation charactercIn combination, it matches the suffix.cString of.

and.*cThis regular expression matches the last character ascString, 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.cand.amThe file name of.

In regular expressions,(a|b)Represents a matchaOr matchb。 above\(c\|am\)After being transferred, that is(c|am), for matchingcperhapsamThis 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.

findThe command can be used-type cExpression 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 fTo 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 fThe expression only represents the matching text file, and does not mean that it only looks for the text file,findThe command will still find all the files in the given directory and pass each file name to the following expression for evaluation-type fOnly when the expression file returns true will it be printed.

Files that do not match this expression will also be found, but not printed.

Recommended Today

Redis 6.0 multithreading performance test results and analysis

  Single threaded redis has always been known for its simplicity and efficiency, but it also has Achilles heel: blocking! Before the completion of the most bottleneck network read-write (redis big key, including some other heavyweight operations such as sort / Sunion / zunionstore / sdiff, centralized expired key cleaning, memory overflow maxmemory policy, etc.), […]