Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

The find command searches for files in a specified directory using the expression given by the user and performs the specified operation.

Syntax format

The general syntax of the find command is as follows:

find path options [-print -exec -ok ...]
Copy the code
  • path: Indicates the directory path to be searched. For example, “.” indicates the current directory, and “/” indicates the root directory
  • -print: Outputs matching files to standard output
  • -exec: Executes the shell command specified by this parameter on the matched file
  • -ok: has the same function as -exec, but is more secure. Before each command is executed, the system asks the user to decide whether to execute it

Common command options are as follows:

  • -name: Searches for files by file name
  • -type: Searches for files by file type
  • -perm: Searches for files by file permission
  • -prune: indicates the directory to be ignored
  • -depth: Specifies the search order, starting from the most sublevel directory in the file tree
  • -user: Finds all files belonging to the specified user name
  • -group: Finds all files belonging to the specified user group
  • -size n: Searches for files whose length is N blocks. By default, each block contains 512 bytes
  • -mtime -n/+n-n indicates that the file is changed within N days ago. +n indicates that the file is changed n days ago.
  • -atime: searches for files according to the access time of files. The use of this parameter is the same as that of mtime.
  • -ctime: searches for files according to the time when the file state changes. The value is the same as mtime.

Find files by name

The most common use of the find command is to find files by name. For example, to find a file named find_test. TXT in the /root directory, use the following command:

find /root -name find_test.txt
Copy the code

If you want to ignore case search, change the name option to -iname:

find /root -iname FIND_test.txt
Copy the code

In addition, find supports the use of wildcards, such as:

find /root -name *_test.txt
Copy the code

A common way to search for files by extension is to find all files ending in.tar.gz in the /root directory, for example:

find /root -name '*.tar.gz'
Copy the code

Find files by type

Sometimes you need to search for files of a particular file type, such as plain files, directories, or symbolic links, etc. You can specify the file type using the -type option and one of the following descriptors:

  • f: General file
  • dDirectory:
  • l: symbolic link file
  • c: character device file
  • b: Block device file
  • p: Pipe file
  • s: socket file

For example, to find all directories in the current directory:

find . -type d
Copy the code

Find files by size

To find files by file size, you can use the -size option. You can specify the file size with the following suffixes:

  • b: 512-byte block (default)
  • cByte:
  • K: kilobytes
  • M: megabytes
  • G: Gigabytes

The following command will find all 1024-byte files in the /root directory:

find /root -size 1024c
Copy the code

The find command also supports searching for files larger or smaller than a specified size. For example, to search for all files smaller than 1MB in the /root directory, use the – symbol:

find /root -size -1M
Copy the code

To search for files larger than 1MB, use the + symbol:

find /root -size +1M
Copy the code

You can even search for files in a range of sizes, for example, files between 1MB and 2MB:

find /root -size +1M -size 2M
Copy the code

Find files by permission

The -perm option searches for files by file permission, for example, to find files with permission 775 in the /root directory:

find /root -perm 775
Copy the code

Find files by owner

To find files owned by a particular user or group, use the -user and -group options.

For example, to search for all files and directories owned by user Tigeriaf, run:

find / -user tigeriaf
Copy the code

Finds a file and performs the specified action

Find all the matching files and execute the specified command, using either -exec or -ok options. As mentioned above, -exec and -ok have the same effect, except that -ok is a little more secure, and the system asks the user to decide whether to execute each command before executing it. The corresponding format is -exec/-ok command {} \; , “command” is the command to be executed, “{}” is the file to be checked instead.

For example, find all files that match *test.txt and delete them:

find . -name *test.txt -exec rm {} \;
Copy the code

or

find . -name *test.txt -ok rm {} \;
Copy the code

conclusion

This section only introduces the find command part of the more commonly used functions, there are many powerful functions, want to learn more friends can learn by themselves.

Original is not easy, if small partners feel helpful, please click a “like” and then go ~

Finally, thank my girlfriend for her tolerance, understanding and support in work and life!