Find command under Linux (1)

Time:2020-1-14

The usage of find command in Linux / Unix

Function Description: find mainly traverses down the file hierarchy (directory) structure to match the qualified files, and corresponding operation options can be executed. The default operation result is to print out the qualified files and directories (you can also specify it by using – print, emphasizing that the command result is to print out, without this option).

 

The main functions are as follows (all tests are matched and searched under the current directory (test):

⓪ : basic usage: list all files and subdirectories under the given directory (base path): find base path – Print

   

 

Supplement:

① : search by filename and regular expression, using the option – name or – Iname (ignore case):

     find base_path -name ‘xxx’ -print

     find base_path -iname ’xxx‘ -print

  

 

 

② : negative parameter, can be used! Excludes the mode specified to.

  

All files except the TXT text file will be printed here.

 

③ Based on the search of directory depth, the find command specifies to traverse all subdirectories. Use – maxdepth and – mindefth to limit the directory depth traversed by the find command, and the find command does not search for symbolic links by default. You can change this behavior with the – L option.

  

 

For example, when the parameter of – maxdepth is 1, only the current directory will be matched.

  

-The parameter of mindepth represents the shortest distance from the directory where the matching is started to the base path.

 

④ : search based on file type: use – type to specify the file type to search. Linux / Unix treats everything as a file (file type s are: ordinary file f, directory D, symbolic link L, character device C, block device B, sockets s, fifo-p). Use – type option to filter the file type.

  

 

This will only match all normal files and directories under a specific item.

 

⑤ Search according to the time stamp of the file: every file in the Linux / Unix file system has three kinds of time stamps: access time (- atime), modification time (- Mtime), change time (- CTime), the unit is the number of days, which is specified by an integer, plus + before the number, which means greater than this time; plus – which means less than this day; and not plus which means just this day.

  

The file here was created before I took the screenshot. The time of access, modification and change is less than one day.

Of course, you can use the option (- Amin) (- mmin) (- Cmin) in minutes, as follows: we test the modification time

   

 

⑥ : file size based search: find provides the unit option to specify the size of the file and then search for files that match the size. This search also often makes users feel very comfortable (B: block, C: byte, W: word, K: kilobyte, M: megabyte, G: Gigabyte).

Before searching, we use the LS (list) command to view the file information in the current directory:

  

 

The fifth column of information is the size (bytes) of each file directory. We search by specifying matching criteria:

  

After testing, in the start directory, the file type is a normal directory, and the file size greater than 30 bytes is zl.txt

 

It should be noted that we should also consider the order in which the options appear, because it will affect the execution efficiency of the entire command.

Tip: This article needs to know about LS (list), chown (change owner) and other commands, as well as Linux / Unix file system file types and file permissions.

 

Unfinished: see the brief description of find command under Linux (2)