We all know that the command to find files under Linux is the find command. Using this command, you can quickly search for the files you want. So what are the tips for using the find command? The editor below will introduce to you the clever use of the find command in Linux.
The find command works by traversing down the file hierarchy, matching files that meet the criteria, and performing the appropriate operations.
1. Search based on file name or regular expression matching
The parameter of option -name specifies the string that the file name must match. We can use wildcards as parameters. "*.txt" matches all strings ending with .txt file name.
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 multiple conditions, you can use the -o parameter.
The code is as follows:
[root@localhost test]# find. (-name “*.txt” -o -name “*.log” )
Option-iname ignores letter case
Option-path parameter can use wildcards to match file paths or files.
2. Negate parameters
Find uses "!" to negate parameters and match all file names that do not end with .txt.
The code is as follows:
[root@localhost test]# find. ! -name “*.txt” –print
3. Search based on directory depth
When the find command is used, it will traverse all subdirectories. 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
List all ordinary files in the current directory. These two commands should follow the target path.
4. Search based on file type
The code is as follows:
find. –type d –print