Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the uses of the find command in Linux

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

The purpose of this article is to share with you the usage of the find command in Linux. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

First, test the following find command example by creating the following empty file under your home directory.

The code is as follows:

# vim create_sample_files.sh

Touch MybashProgram.sh

Touch mycprogram.c

Touch MyCProgram.c

Touch Program.c

Mkdir backup

Cd backup

Touch MybashProgram.sh

Touch mycprogram.c

Touch MyCProgram.c

Touch Program.c

# chmod + x create_sample_files.sh

#. / create_sample_files.sh

# ls-R

.:

Backup MybashProgram.sh MyCProgram.c

Create_sample_files.sh mycprogram.c Program.c

. / backup:

MybashProgram.sh mycprogram.c MyCProgram.c Program.c

1. Find a file with a file name

This is a basic use of the find command. The following example shows how to use MyCProgram.c as the lookup name to find files in the current directory and its subdirectories.

The code is as follows:

# find-name "MyCProgram.c"

. / backup/MyCProgram.c

. / MyCProgram.c

two。 Find a file with a file name, ignoring case

This is a basic use of the find command. The following example shows how to use MyCProgram.c as the lookup name to find files in the current directory and its subdirectories, ignoring case.

The code is as follows:

# find-iname "MyCProgram.c"

. / mycprogram.c

. / backup/mycprogram.c

. / backup/MyCProgram.c

. / MyCProgram.c

3. Use mindepth and maxdepth to limit the depth of the search for the specified directory

Look for the passwd file in the root directory and its subdirectories.

The code is as follows:

# find /-name passwd

. / usr/share/doc/nss_ldap-253/pam.d/passwd

. / usr/bin/passwd

. / etc/pam.d/passwd

. / etc/passwd

Look for passwd in the root directory and its 1-tier deep subdirectory. (for example, root-level 1, and one sub-directory-level 2)

The code is as follows:

# find-maxdepth 2-name passwd

. / etc/passwd

Look for the passwd file in the root directory and its maximum two-tier depth subdirectory. (e.g. root-level 1, and two sub-directories-level 2 and 3)

The code is as follows:

# find /-maxdepth 3-name passwd

. / usr/bin/passwd

. / etc/pam.d/passwd

. / etc/passwd

Look for the passwd file between the layer 2 subdirectory and the layer 4 subdirectory.

The code is as follows:

# find-mindepth 3-maxdepth 5-name passwd

. / usr/bin/passwd

. / etc/pam.d/passwd

4. Execute the command on the file found by the find command

The following example shows the find command to calculate the MD5 validation and for all case-insensitive files named "MyCProgram.c". {} will be replaced by the current file name.

The code is as follows:

Find-iname "MyCProgram.c"-exec md5sum {}\

D41d8cd98f00b204e9800998ecf8427e. / mycprogram.c

D41d8cd98f00b204e9800998ecf8427e. / backup/mycprogram.c

D41d8cd98f00b204e9800998ecf8427e. / backup/MyCProgram.c

D41d8cd98f00b204e9800998ecf8427e. / MyCProgram.c

5. Opposite matching

Displays all files or directories whose names are not MyCProgram.c. Because maxdepth is 1, only the files and directories in the current directory are displayed.

The code is as follows:

Find-maxdepth 1-not-iname "MyCProgram.c"

.

. / MybashProgram.sh

. / create_sample_files.sh

. / backup

. / Program.c

6. Use inode numbers to find files

Any file has a unique inode number, so we can distinguish between files. Create two files with similar names, such as one with a space ending and one without.

The code is as follows:

Touch "test-file-name"

# touch "test-file-name"

[Note: There is a space at the end]

# ls-1 test*

Test-file-name

Test-file-name

The output from ls cannot tell which file has a space ending. Using the option-I, you can see the inode number of the file, thus distinguishing the two files.

The code is as follows:

Ls-i1 test*

16187429 test-file-name

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.

The code is as follows:

Find-inum 16187430-exec mv {} new-test-file-name\

# ls-i1 * test*

16187430 new-test-file-name

16187429 test-file-name

You can use this technique when you want to do something with poorly named files like the one above.

For example, a file name named file?.txt has a special character in it.

If you want to execute "rm file?.txt", all three files shown below will be deleted.

So, use the following steps to delete the "file?.txt" file.

The code is as follows:

Ls

File1.txt file2.txt file?.txt

Find the inode number of each file.

The code is as follows:

Ls-i1

804178 file1.txt

804179 file2.txt

804180 file?.txt

As follows:? Use inode numbers to delete file names with special symbols.

The code is as follows:

Find-inum 804180-exec rm {}\

# ls

File1.txt file2.txt

[Note: The file with name "file?.txt" is now removed]

7. Find files based on file permissions

The following operations are reasonable:

Find a file with the specified permissions

Ignore other permission bits and check whether they match the specified permissions

Search based on the permissions of a given octal / symbolic expression

In this example, assume that the directory contains the following files. Note that the permissions of these files are different.

The code is as follows:

Ls-l

Total 0

-rwxrwxrwx 1 root root 0 2009-02-19 20:31 all_for_all

-rw-r--r-- 1 root root 0 2009-02-19 20:30 everybody_read

-1 root root 0 2009-02-19 20:31 no_for_all

-rw- 1 root root 0 2009-02-19 20:29 ordinary_file

-rw-r- 1 root root 0 2009-02-19 20:27 others_can_also_read

-r-1 root root 0 2009-02-19 20:27 others_can_only_read

Find the file with group read permissions. Use the following command to find a file in the current directory that has read permissions for the same group of users, ignoring other permissions for that file.

The code is as follows:

Find. -perm-grubr-type f-exec ls-l {}\

-rw-r--r-- 1 root root 0 2009-02-19 20:30. / everybody_read

-rwxrwxrwx 1 root root 0 2009-02-19 20:31. / all_for_all

-r-1 root root 0 2009-02-19 20:27. / others_can_only_read

-rw-r- 1 root root 0 2009-02-19 20:27. / others_can_also_read

Locate the file that has read-only access to the group user.

The code is as follows:

Find. -perm galler-type f-exec ls-l {}\

-r-1 root root 0 2009-02-19 20:27. / others_can_only_read

Locate the file that has read-only access to the group users (in the form of octal permissions).

The code is as follows:

Find. -perm 040-type f-exec ls-l {}\

-r-1 root root 0 2009-02-19 20:27. / others_can_only_read

8. Find all empty files (0-byte files) in the home directory and subdirectories

Most of the output files of the following command are place hoders created by other programs in the locked file box.

Find ~-empty

List only the empty files in your home directory.

Find. -maxdepth 1-empty

Only non-hidden empty files in the current year's directory are listed.

Find. -maxdepth 1-empty-not-name ".

9. Find the 5 largest files

The following command lists the 5 largest files in the current directory and subdirectories. This will take some time, depending on the number of files that the command needs to process.

The code is as follows:

Find. -type f-exec ls-s {}\; | sort-n-r | head-5

10. Find the 5 smallest files

The method is similar to finding the five largest files, except that the order of the sort is descending.

The code is as follows:

Find. -type f-exec ls-s {}\; | sort-n | head-5

In the above command, it is likely that all you see is an empty file (0-byte file). In this way, you can use the following command to list the smallest files instead of 0 bytes.

The code is as follows:

Find. -not-empty-type f-exec ls-s {}\; | sort-n | head-5

11. Use-type to find files of the specified file type

Find only socket files

Find. -type s

Find all the directories

Find. -type d

Find all the general files

Find. -type f

Find all hidden files

Find. -type f-name ". *"

Find all hidden directories

Find-type d-name ".

twelve。 Find the file by comparing the modification time with other files

Displays the files that made changes after the file was specified. The following find command displays all files that were created and modified after ordinary_file.

The code is as follows:

Ls-lrt

Total 0

-rw-r- 1 root root 0 2009-02-19 20:27 others_can_also_read

-r-1 root root 0 2009-02-19 20:27 others_can_only_read

-rw- 1 root root 0 2009-02-19 20:29 ordinary_file

-rw-r--r-- 1 root root 0 2009-02-19 20:30 everybody_read

-rwxrwxrwx 1 root root 0 2009-02-19 20:31 all_for_all

-1 root root 0 2009-02-19 20:31 no_for_all

# find-newer ordinary_file

.

. / everybody_read

. / all_for_all

. / no_for_all

13. Find files by file size

Use the-size option to find files by file size.

Find a file larger than the specified file

Find ~-size + 100m

Find a file smaller than the specified file

Find ~-size-100m

Find files that match a given size

Find ~-size 100m

Note:-the finger is smaller than the given size and the + finger is larger than the given size. No symbol represents exactly the same size as a given size.

14. Alias common find operations

If you find something useful, you can give it an alias. And do it wherever you want.

The code is as follows:

Commonly used to delete a.out files.

Alias rmao= "find.-iname a.out-exec rm {}\;"

# rmao

Delete the core file generated by the c program.

Alias rmc= "find.-iname core-exec rm {}\;"

# rmc

15. Delete large package files with the find command

The following command deletes * .zip files greater than 100m.

Find /-type f-name * .zip-size + 100m-exec rm-I {}\; "

Delete all heavy rain 100m * .tar files with alias rm100m. Using the same idea, you can create a class of aliases for rm1g,rm2g,rm5g to delete all files larger than 1G, 2G, and 5G.

The code is as follows:

Alias rm100m= "find /-type f-name * .tar-size + 100m-exec rm-I {}\;"

# alias rm1g= "find /-type f-name * .tar-size + 1G-exec rm-I {}\;"

# alias rm2g= "find /-type f-name * .tar-size + 2G-exec rm-I {}\;"

# alias rm5g= "find /-type f-name * .tar-size + 5G-exec rm-I {}\;"

# rm100m

# rm1g

# rm2g

# rm5g

Thank you for reading! This is the end of the article on "what is the use of the find command in Linux". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report