How to use the find command in Linux

  

We all know that the command to find files under Linux is the find command. With this command, you can quickly find the files you want. What are the tips for using the find command? The following small series will introduce you to the clever use of the find command in Linux.

find command works by traversing downward along the hierarchy of the file, matching matching document, and perform the corresponding operation.

1, according to the file name or regular expression matching search

The option -name parameter specifies the string that the file name must match, we can use the wildcard as a parameter, “*. Txt” matches all filenames ending in .txt.

The code is as follows:

[root@localhost test]# touch {data,log,file,File,LOG}_{1,2,3,4,5,6}_{ .txt, .pdf, .log, .conf}

[root@localhost test]# find . -name “*.txt” –print

If you want to match one of several conditions, you can use the -o parameter.

The code is as follows:

[root@localhost test]# find . \\( -name “*.txt” -o -name “*.log” \\)

The option -iname ignores the case of letters

The parameters of the -path option can use wildcards to Match file paths or files.

2, Negative parameters

find with “! ” to negate the parameters, match all file names that do not end with .txt.

The code is as follows:

[root@localhost test]# find . ! -name “*.txt” –print

3, based on directory depth search

The find command will traverse all subdirectories when used, we can use -maxdepth and -mindepth To limit the depth of the find command traversal.

-maxdepth: Specify the maximum depth;

-mindepth: Specify the minimum depth.

The code is as follows:

[root@localhost ~]# find . -maxdepth 1 -type f

Lists all the normal files in the current directory, which are followed by the target path.

4, search according to the file type

The code is as follows:

find. –type d –print
File Type Type Parameter Normal File f Symbol File l Directory d Character Device c Block Device b Socket s fifo p Previous12Next Total 2 Pages

Copyright © Windows knowledge All Rights Reserved