Hi, I’m Chopin and this is my 10th original article.

In Linux, as an administrator, I want to find all the files in the system whose size is over 200M, check which files have been modified in the system in the last seven days, and find all the executable files in the subdirectories. All of these tasks require the find command.

Common attributes in Linux system files are classified into the following: Name, size, permission, owner, modification time, and access time. The find command can be used to search for a specified attribute.

Nonsense is not much, directly open dry, below into the actual case.

Case of actual combat

(1) Search by file name

Finding by file name is the most common use of find. It is important to note that the file names must match exactly to find the corresponding file.

1. Search for all go files in the current directory

$ find . -name "*.go"
Copy the code

2. In the etc directory, search for TXT files starting with uppercase letters

$ find /etc -name "[A-Z]*.txt" -print
Copy the code

3. Search for TXT files that do not start with out in the current directory

$ find . -name "out*" -prune -o -name "*.txt" -print
Copy the code

4. Search for TXT files in the current directory except the git subdirectory

$ find . -path "./git" -prune -o -name "*.txt" -print
Copy the code

5. Find all hard links of a file. The ls -i option is used to view the inode number of the file

$ ls -i 1.txt
138956 1.txt
$ find . -num 138956
Copy the code

As a bonus, the -iname parameter ignores file name case when searching for files

(2) search by file type

1. Search for the soft connection file in the current directory

$ find . -type l -print
Copy the code

2. In the current directory, search for common files ending in log. F indicates the common file type

$ find . -type f -name "*.log"
Copy the code

(three) according to the file size search

1. Search for files whose size is smaller than 64 KB

$ find . -size -64k -print
Copy the code

2. Search for files whose size exceeds 200 MB

$ find . -size +200M -type f -print
Copy the code

(4) search by time

1. Search for files that have been modified within 2 days

$ find . -mtime -2 -type f -print
Copy the code

2. Search for the file that was modified two days ago. -mtime indicates the modification time

$ find . -mtime +2 -type f -print
Copy the code

3. Search for files accessed within one day. -atime indicates the access time

$ find . -atime -1 -type f -print
Copy the code

4. Search for the file whose status is changed within one day. -ctime indicates the time when the metadata is changed

$ find . -ctime -1 -type f -print
Copy the code

5. Find new files than chopin.txt

$ find . -newer "chopin.txt" -type f -print
$ find . ! -newer "chopin.txt" -type f -print # old
Copy the code

(5) Search according to authority

1. Search for files whose permission is 644 in the current directory

$ find . -type f -perm 644
Copy the code

2. Search for files in the etc directory where at least one user has the write permission

$ find /etc -type f -perm /222
Copy the code

3. Search for files in the etc directory that all users have execution permission on

$ find /etc -perm -111 -ls
Copy the code

(6) Combination conditions

1. Search for common files belonging to Chopin users in the current directory

$ find . -type f -a -user chopin -print
Copy the code

2. Search for files larger than 2 MB or repaired two days ago in the current directory

$ find . -size +2M -o -mtime +2 -print
Copy the code

3. Find abnormal files in the current directory

$ find . -not -type f
$ find . ! -type f
Copy the code

4. Search for non-empty files

$ find . ! -empty
Copy the code

(7) Processing action

Find allows us to perform processing actions based on each of these criteria, making it more convenient and flexible than just printing it out

1. -print Prints by default and can be omitted

$ find . -name "*.log" -print
$ find . -name "*.log" # equivalent
Copy the code

2. -ls The output is in ls long file format

$ find . -name "*.txt" -ls
138957      4 -rw-r--r--   1 root     root           16 Jan 24 23:20 ./a.txt
138959      4 -rw-r--r--   1 root     root          172 Jan 24 13:06 ./T.txt
138956      4 -rw-r--r--   1 root     root           27 Jan 24 23:28 ./1.txt
Copy the code

3. -delete Deletes the found file

$ find . -size +100M -delete
Copy the code

4. -exec passes the found file to command. The following example passes the found file to the ls command. Similarly, we can pass it to any Linux command, which is very powerful and flexible.

$ find . -name "*.txt" -execls -lh {} \; -rw-r--r-- 1 root root 16 Jan 24 23:20 ./a.txt -rw-r--r-- 1 root root 172 Jan 24 13:06 ./T.txt -rw-r--r-- 1 root root 27  Jan 24 23:28 ./1.txtCopy the code

5. -ok has the same function as -exec except that it will prompt the user to confirm the operation, that’s all. Of course, in a production environment, we still recommend ok

(8) Classic cases

What if a file with a garbled name exists and you want to delete it? Even if we copy the garbled names to the command line, there is a good chance that the terminal will not recognize them correctly. Don’t worry, we’ll show you how elegantly find solves this problem.

$ls-i 138957 a.inum 138959 t.inum 132395 ڹ�� TXT $find. -inum 132395 -exec rm {} \;
Copy the code

In the command, -inum specifies the inode number of the file, which is the unique number of each file in the system. Find finds the file using the inum number and deletes the file.

summarizing

The find command is one of the most useful Linux commands. It is very powerful and has a complex syntax. In fact, we do not need to know all the details of it, master the common use of the above case is enough to meet most of the needs of daily work.

Let’s summarize the common uses of the find command to understand how to use it.

The command format

find path -option [-exec ...]

Search by filename

  • -name: According to the file name search, accurate match;
  • -iname: is case-insensitive to file names.
  • -inode: Search by file inode number.

Search by file type

You can use the -type option to search by file type. The supported file types are as follows:

  • f: Common file
  • d: Directory file
  • l: Link file
  • s: socket file
  • p: Pipe file
  • b: Block device files, such as disks
  • c: character device files, such as keyboard, mouse, and nic

Search by file dependency

  • -user: Searches by user name
  • -group: Searches by group name
  • -uid: Searches by user ID
  • -gid: Searches by group ID
  • -nouser: Finds files that have no owner
  • -nogroup: Searches for files that have no owner group

Search by file size

-size = 1024M; -size = 1024M; -size = 1024M

  • -5M: searches for files smaller than 5M
  • +5M: Searches for files larger than 5M
  • 5M: Finds a file with a size of 5M

The unit supports c (bytes), K, M, and G. Note that the default unit is B, which is 512 bytes.

Search by time

The find by time function is commonly used by system administrators. Find supports the following time types:

  • atime: Searches by access time
  • mtime: Searches by data modification time
  • ctime: Searches by metadata modification time
  • newer: Based on the file, judge the file is newer than it

When searching by time, use the following format:

  • -atime -5: indicates the files accessed within 5 days.
  • -atime +5: indicates the files accessed 6 days ago.
  • -atime 5: indicates the files accessed in the last 5-6 days.

This +5 meaning is always misunderstood by people, mistaken for a file modified 5 days later, if you can know the next 5 days, xiaobian would have to buy lottery tickets! Maybe it is not very clear to say so, just look at the picture!

Find can find files by amin, mmin, and cmin, not only by day, but also by minute.

Search by permission

Searching by permission is done with the -perm option, which can be used as follows:

  • -perm 644: Precise permission search
  • -perm /666: Any user of any type meets the conditions
  • -perm -222: All users of each type meet the conditions

Combination conditions

Find can use a combination of conditions, including -a, -o, -not, or! , relatively simple, not detailed description of its meaning.

Processing action

Find allows us to perform processing actions based on various criteria, making it more convenient and flexible than just printing them out.

  • -print: Print, default action, can be omitted
  • -ls: Output in ls long file format
  • -delete: Deletes the found file
  • -exec: The files found are passed to any Linux command
  • -ok: Has the same function as exec, but requires the user to confirm each operation

Again, the find command supports a wide range of arguments and options, and this article summarizes the most common and core arguments. If the above command does not meet the requirements, you can ask your man find.

It is important to mention here that find searches for files by scanning disks. Try not to search for files in a large area, especially in/directories, which will consume CPU resources of the server for a long time. If the machine is in production environment, consider whether it will affect the business before execution.

Extend the locate

While find is very powerful, it is important to remember that the find execution process scans disk files for searches, which can take a long time and consume CPU resources on the server.

Another Linux file locate artifact, similar to Everything on the Win platform, is recommended. It is based on the index table to query, query speed is very fast, basically does not occupy CPU resources.

It’s very simple to use

$ locate file.txt
$ locate /etc/httpd
Copy the code

Note that if a file is created that day, it cannot be found through Locate by default, because its database is automatically updated daily by default. To query the new file created on the current day, run updatedb.

Speed of lookup is the advantage of Locate, but its disadvantages are also very obvious:

  • Fuzzy query
  • The search match pattern is single
  • Query the name match path command
  • The creation of index tables occupies disk space
  • Non-real-time query, the current day data may not be found

Well, that’s the end of the find command, and hopefully the examples and summaries will help you make better use of it. I also strongly recommend bookmarking this article as a manual of common Linux commands.

Well, that’s all for this sharing! Thank you. I’m Chopin. Stay tuned for more.

Recommended reading:

  • A letter for Linux beginners
  • The most detailed load balancing principle diagram in the whole network
  • Sed tutorial details, xiao Bai can understand
  • Linux three Musketeers grep tutorial details