Home > php教程 > PHP开发 > Summary of usage of find (file search) command under Linux

Summary of usage of find (file search) command under Linux

高洛峰
Release: 2016-12-14 16:42:24
Original
1639 people have browsed it

Preface: Why do we need to learn to use the find command?

Every operating system is composed of thousands of files, and this is no exception for an operating system like Linux where "everything is a file". Everyone should be able to easily use the file search function under Windows. However, you may not be very familiar with this function of Linux. In fact, if you want to play Linux, you must firmly master this command, because Linux does not have a fixed file name suffix like Windows, and because of the characteristics of a hundred schools of thought contending in the Linux camp, one of the same The files may be different in different distributions, so if you can firmly master the use of the find command, you will be much smoother on the road to exploring Linux. At the same time, you will find that the file search function under Linux is actually very simple. And it is much more powerful than the search function under Windows! Okay, let’s get to the point

There are two commands to find files under Linux; locate and find

First of all, let’s talk about locate. The locate command is to traverse the database it generates (the command to generate the database: updatedb) , this feature determines that using locate to find files is very fast, but the locate command can only perform fuzzy matching on files, which is a bit less accurate. Let’s briefly introduce its two options:

#locate

- I // When looking for files, do not distinguish between ups and downs. For example, Locate --i Passwd

-n // Just display the front n line of the search result, such as Locate -N 5 PASSWD

If the search directory is not specified, the entire system will be traversed and searched

Usage format: find [Specify the search directory] [Search rules] [Action to be executed after the search]

[Specify the search directory] For example: ​​​​# - iname //Search based on file name, but not case-sensitive

Here is another introduction to the knowledge of file name wildcards

* means wildcarding any characters

? Represents any single character in wildcard

[ ] Represents any character in wildcard brackets

Summary of usage of find (file search) command under Linux

(2), search for files based on the user and group to which the file belongs

                            Mainly to find filesSummary of usage of find (file search) command under Linux

                                                                                                                                                                                                    . -uid 500 //Find uid 500 File in #Find/TMP-Gid 1000 // Find a file with 1000 files

Summary of usage of find (file search) command under Linux (4), -A and -O and — Not use

# -a to connect two different conditions ( Both conditions must be met at the same time)

       # -o Connect two different conditions (one of the two conditions can be met)

                                                                                                                                                                

(5), Find files based on the relevant attributes of the file timestamp

We can use the stat command to view the time information of a file as follows:

Summary of usage of find (file search) command under Linux

#-atime

           #-ctime

           #-amin

                                                                                                                                                                                                                                                                                The unit of atime here refers to "days", and the unit of amin is minutes

                #find   /tmp   –atime   + 5                                                                   using using use using using  -- 5                                                                                                                                                                                                                                                                                                                                                                                                                                ; File

                                                                                           Block device file File

                                                                                                                                                                                                                                                      socket file                                    . D #Find/TMP -Size 2m // Find a file that is equal to 2m in/tmp directory

#Find/TMP -SIZE+2M // Find files greater than 2m in/tmp directory

#find/ tmp -size -2M //Find files smaller than 2M in the /tmp directory

(8), find files based on file permissions

-perm

                                                                                                      /Find files with permission 755 in the /tmp directory

                                                                                                                                                                                                          ​​​​#find / -nogroup –a –nouser //In Search the entire system for files that have neither owner nor group (such files are usually very dangerous, and as system engineers we should clear them in time)

[Find the executed action]

# -Print // Action at the default situation

# -ls // Find it after finding ls

# -OK [Comnd] // When finding the command after executing the command, ask the user if you want to execute

# -exec [ commend]                                                                                                                                                                                                  // Do not ask the user when executing the command, directly execute

Note the use of {} here: replace the found file

#find /tmp -atime +30 –exec rm –rf {} ; #Delete the found files that have not been accessed for more than 30 days

Summary of usage of find (file search) command under Linux

We can also use xargs to further operate the found files

Summary of usage of find (file search) command under Linux

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template