15 practical Linux find command examples

Source: Internet
Author: User

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
03 Touch mycprogram. c
04 Touch MyCProgram. c
05 Touch Program. c
06  
07 Mkdir backup
08 Cd backup
09  
10 Touch MybashProgram. sh
11 Touch mycprogram. c
12 Touch MyCProgram. c
13 Touch Program. c
14  
15 # Chmod + x create_sample_files.sh
16  
17 #./Create_sample_files.sh
18  
19 # Ls-R
20 .:
21 Backup MybashProgram. sh MyCProgram. c
22 Create_sample_files.sh mycprogram. c Program. c
23  
24 ./Backup:
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 ./Backup/MyCProgram. c
3 ./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"
2 ./Mycprogram. c
3 ./Backup/mycprogram. c
4 ./Backup/MyCProgram. c
5 ./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.

1 # Find/-name passwd
2 ./Usr/share/doc/nss_ldap-253/pam. d/passwd
3 ./Usr/bin/passwd
4 ./Etc/pam. d/passwd
5 ./Etc/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
2 ./Etc/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
2 ./Usr/bin/passwd
3 ./Etc/pam. d/passwd
4 ./Etc/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
2 ./Usr/bin/passwd
3 ./Etc/pam. d/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"
2 .
3 ./MybashProgram. sh
4 ./Create_sample_files.sh
5 ./Backup
6 ./Program. c
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.

1 Touch "test-file-name"
2  
3 # Touch "test-file-name"
4 [Note: There is a space at the end]
5  
6 # Ls-1 test *
7 Test-file-name
8 Test-file-name

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.

1 Ls-i1 test *
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 \;
2  
3 # Ls-i1 * test *
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.

1 Ls
2 File1.txt file2.txt file ?. Txt

Find the inode number of each file.

1 Ls-i1
2 804178 file1.txt
3 804179 file2.txt
4 804180 file ?. Txt

Use inode numbers to delete file names with special characters.

1 Find-inum 804180-exec rm {}\;
2  
3 # Ls
4 File1.txt file2.txt
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.

1 Ls-l
2 Total 0
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.

1 Find ~ -Empty

Only list Empty files in your home directory.

1 Find.-maxdepth 1-empty


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

1 Find.-type s

Find all directories

1 Find.-type d

Search for all common files

1 Find.-type f

Search for all hidden files

1 Find.-type f-name ".*"

Find all Hidden Directories

1 Find-type d-name ".*"
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.

01 Ls-lrt
02 Total 0
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
09  
10 # Find-newer ordinary_file
11 .
12 ./Everybody_read
13 ./All_for_all
14 ./No_for_all
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

1 Find ~ -Size + 100 M

Searches for files smaller than the specified file.

1 Find ~ -Size-100 M

Search for files of the specified size

1 Find ~ -Size 100 M

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 {}\;"
2 # Rmao

Delete the core file generated by the c program.

1 Alias rmc = "find.-iname core-exec rm {}\;"
2 # Rmc
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 {}\;"
5  
6 # Rm100m
7 # Rm1g
8 # Rm2g
9 # RM5 g

Find command example (Part 2)


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.