In addition to basic operations like searching for files in a directory structure, you can also use the find command to perform some practical operations to make your command line easier.
This article will introduce 15 Linux find commands that are useful for beginners and laruence.
First, create the following empty file under your home directory to test the following find command example.
01 |
# Vim create_sample_files.sh |
02 |
Touch MybashProgram. sh |
10 |
Touch MybashProgram. sh |
15 |
# Chmod + x create_sample_files.sh |
17 |
#./Create_sample_files.sh |
21 |
Backup MybashProgram. sh MyCProgram. c |
22 |
Create_sample_files.sh mycprogram. c Program. c |
25 |
MybashProgram. sh mycprogram. c MyCProgram. c Program. c |
1. Search for files by file name
This is a basic usage of the find command. The following example shows how to use MyCProgram. c as the search name to find files in the current directory and Its subdirectories.
1 |
# Find-name "MyCProgram. c" |
2. Search for files by file name, case-insensitive
This is a basic usage of the find command. The following example shows how to use MyCProgram. c as the search name to find the file in the current directory and Its subdirectories, regardless of case.
1 |
# Find-iname "MyCProgram. c" |
3. Use mindepth and maxdepth to limit the depth of a specified directory
Search for the passwd file in the root directory and Its subdirectories.
2 |
./Usr/share/doc/nss_ldap-253/pam. d/passwd |
Search for passwd in the root directory and Its subdirectories at Layer 1 (for example, root-level 1, and one sub-directory-level 2)
1 |
# Find-maxdepth 2-name passwd |
Search for the passwd file in the root directory and Its subdirectories with a maximum depth of two layers (for example, root-level 1, and two sub-directories-level 2 and 3)
1 |
# Find/-maxdepth 3-name passwd |
Search for the passwd file between the second-level sub-directory and the fourth-level sub-directory.
1 |
# Find-mindepth 3-maxdepth 5-name passwd |
4. Execute the command on the file found by the find command
The following example shows how to use the find command to calculate MD5 verification and for all files with case-insensitive file names "MyCProgram. c. {} Will be replaced by the current file name.
1 |
Find-iname "MyCProgram. c"-exec md5sum {}\; |
2 |
D41d8cd98f00b204e9800998ecf8427e./mycprogram. c |
3 |
D41d8cd98f00b204e9800998ecf8427e./backup/mycprogram. c |
4 |
D41d8cd98f00b204e9800998ecf8427e./backup/MyCProgram. c |
5 |
D41d8cd98f00b204e9800998ecf8427e./MyCProgram. c |
5. Reverse matching
Display All files or directories whose names are not MyCProgram. c. Because maxdepth is 1, only files and directories under the current directory are displayed.
1 |
Find-maxdepth 1-not-iname "MyCProgram. c" |
4 |
./Create_sample_files.sh |
6. Search for files by inode number
Each file has a unique inode number, so that we can differentiate files. Create two files with similar names, for example, one with a space and one with no.
3 |
# Touch "test-file-name" |
4 |
[Note: There is a space at the end] |
The output from ls cannot tell which file ends with a space. Use Option-I to view the inode Number of the file, so that the two files can be distinguished.
2 |
16187429 test-file-name |
3 |
16187430 test-file-name |
You can specify the inode number in the find command as shown below. Here, the find command renames a file with an inode number.
1 |
Find-inum 16187430-exec mv {} new-test-file-name \; |
4 |
16187430 new-test-file-name |
5 |
16187429 test-file-name |
You can use this technology when you want to perform some operations on files with the same bad names as above. For example, the name is file ?. The txt file name contains a special character. If you want to execute "rm file ?. Txt, all three files shown below will be deleted. Therefore, follow these steps to delete "file ?. Txt file.
2 |
File1.txt file2.txt file ?. Txt |
Find the inode number of each file.
Use inode numbers to delete file names with special characters.
1 |
Find-inum 804180-exec rm {}\; |
5 |
[Note: The file with name "file ?. Txt "is now removed] |
7. Search for files based on their Permissions
The following operations are reasonable:
- Find the file with the specified permission
- Ignore other permission bits and check whether they match the specified permission
- Search based on the given octal/symbolic expression Permissions
In this example, the directory contains the following files. Note that these files have different permissions.
3 |
-Rwxrwxrwx 1 root 0 2009-02-19 20:31 all_for_all |
4 |
-Rw-r -- 1 root 0 2009-02-19 20:30 everybody_read |
5 |
---------- 1 root 0 2009-02-19 20:31 no_for_all |
6 |
-Rw ------- 1 root 0 2009-02-19 20:29 ordinary_file |
7 |
-Rw-r ----- 1 root 0 2009-02-19 20:27 others_can_also_read |
8 |
---- R ----- 1 root 0 2009-02-19 20:27 others_can_only_read |
Find the file with Group read permission. Use the following command to find the files in the current directory that have read permissions for users in the same group, and ignore other permissions on the files.
1 |
Find.-perm-g = r-type f-exec ls-l {}\; |
2 |
-Rw-r -- 1 root 0 2009-02-19 20:30./everybody_read |
3 |
-Rwxrwxrwx 1 root 0 2009-02-19 20:31./all_for |
4 |
---- R ----- 1 root 0 2009-02-19 20:27./others_can_only_read |
5 |
-Rw-r ----- 1 root 0 2009-02-19 20:27./others_can_also_read |
Find the file that has read-only permission on the group user.
1 |
Find.-perm g = r-type f-exec ls-l {}\; |
2 |
---- R ----- 1 root 0 2009-02-19 20:27./others_can_only_read |
Find the files that have read-only permissions on the group users (in the octal permission form ).
1 |
Find.-perm 040-type f-exec ls-l {}\; |
2 |
---- R ----- 1 root 0 2009-02-19 20:27./others_can_only_read |
8. Find all the empty files (0-byte files) in the home directory and subdirectory)
Most of the output files of the following command are locked by the place hoders created by other programs in the file box.
Only list Empty files in your home directory.
Only non-hidden Empty files in the current directory are listed.
1 |
Find.-maxdepth 1-empty-not-name ".*" |
9. Search for the five largest files
The following command lists the five largest files in the current directory and subdirectory. This takes some time, depending on the number of files to be processed by the command.
1 |
Find.-type f-exec ls-s {}\; | sort-n-r | head-5 |
10. Search for the five smallest files
The method is similar to searching for the five largest files. The difference is that the sort order is descending.
1 |
Find.-type f-exec ls-s {}\; | sort-n | head-5 |
In the above command, you may see only empty files (0-byte files ). In this way, you can use the following command to list the smallest file, instead of the zero-byte file.
1 |
Find.-not-empty-type f-exec ls-s {}\; | sort-n | head-5 |
11. Use-type to find files of the specified file type
Only query socket files
Find all directories
Search for all common files
Search for all hidden files
Find all Hidden Directories
12. Compare the modification time with other files to find the file
Displays the files modified after the specified file. The find command below will display all the files created and modified after ordinary_file.
03 |
-Rw-r ----- 1 root 0 2009-02-19 20:27 others_can_also_read |
04 |
---- R ----- 1 root 0 2009-02-19 20:27 others_can_only_read |
05 |
-Rw ------- 1 root 0 2009-02-19 20:29 ordinary_file |
06 |
-Rw-r -- 1 root 0 2009-02-19 20:30 everybody_read |
07 |
-Rwxrwxrwx 1 root 0 2009-02-19 20:31 all_for_all |
08 |
---------- 1 root 0 2009-02-19 20:31 no_for_all |
10 |
# Find-newer ordinary_file |
13. Search for files by file size
You can use the-size Option to search for files by file size.
Search for files larger than the specified file
Searches for files smaller than the specified file.
Search for files of the specified size
Note:-refers to smaller than the given size, + refers to larger than the given size. No symbol indicates that the given size is exactly the same.
14. Alias for common find operations
If you find something useful, you can give it an alias. And execute it wherever you want.
Delete a. out file.
1 |
Alias rmao = "find.-iname a. out-exec rm {}\;" |
Delete the core file generated by the c program.
1 |
Alias rmc = "find.-iname core-exec rm {}\;" |
15. Use the find command to delete large packaged files
The following command deletes the *. zip file larger than MB.
1 |
Find/-type f-name *. zip-size + 100 M-exec rm-I {}\;" |
Use the alias rm100m to delete all *. tar files with a heavy rain of MB. Using the same idea, you can create rm1g, rm2g, and rm5g aliases to delete all files larger than 1G, 2G, and 5G.
1 |
Alias rm100m = "find/-type f-name *. tar-size + 100 M-exec rm-I {}\;" |
2 |
# Alias rm1g = "find/-type f-name *. tar-size + 1G-exec rm-I {}\;" |
3 |
# Alias rm2g = "find/-type f-name *. tar-size + 2G-exec rm-I {}\;" |
4 |
# Alias RM5 G = "find/-type f-name *. tar-size + 5G-exec rm-I {}\;" |
Find command example (Part 2)