Home  >  Article  >  Operation and Maintenance  >  Detailed explanation of how to use find command in Linux

Detailed explanation of how to use find command in Linux

巴扎黑
巴扎黑Original
2017-09-04 14:00:003204browse

In Windows, you can search for files in certain paths, or you can set not to search for files in certain paths. Let’s use the find command in Linux combined with the -path -prune parameter to see how to implement it in Linux. this function.

If you are searching for a file in the current directory, and there are many files and directories (multi-level directories) in the current directory, including dir0, dir1 and dir2...directories and dir00, dir01...dir10, dir11 ...and other subdirectories.

1. Find all txt suffix files in the current directory

find ./ -name *.txt

2. In the dir0 directory and subdirectories of the current directory Find txt suffix file

find ./ -path './dir0*' -name *.txt

3. In the subdirectory dir00 and its subdirectories under the dir0 directory in the current directory Find txt suffix files under

find ./ -path '*dir00*' -name *.txt

4. Find txt suffix files in directories other than dir0 and subdirectories

find ./ -path './dir0*' -a -prune -o -name *.txt -print

Explanation: -a should be the abbreviation of and, which means logical operator' or '(&&); -o should be the abbreviation of or, which means the logical operator 'and' (||), -not means non.

The meaning of the command line is: if the directory dir0 exists (i.e. The left side of -a is true), then find the value of -prune, -prune returns true, the 'AND' logical expression is true (that is, -path './dir0*' -a -prune is true), the find command will be in addition Search for the txt suffix file in a directory other than this directory and print it out; if the directory dir0 does not exist (that is, the left side of -a is false), -prune will not be evaluated, and the 'AND' logical expression is false, then it will be searched in the current directory. All txt suffix files.

5. Search for txt suffix files in directories other than dir0, dir1 and subdirectories

find ./ /( -path './dir0*' -o -path './ dir1*' /) -a -prune -o -name *.txt -print

Note: Parentheses () indicate the combination of expressions. That is, it instructs the shell not to make special interpretations of the following characters, but to leave it to the find command to interpret their meanings. Since parentheses cannot be used directly on the command line, you need to use the backslash '/' to escape them (that is, the '/' escape character makes the command line recognize the parentheses). Also note that '/(', '/)' requires spaces on both sides.

6. Find txt suffix files in dir0, dir1 and subdirectories

find ./ /( -path './dir0*' -o -path './dir1*' / ) -a -name *.txt -print

+1. Find txt suffix files in all directories named dir_general

find ./ -path '*/dir_general/*' -name *.txt -print

The above is the detailed content of Detailed explanation of how to use find command in Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn