Introduction to the find command in Linux

  • 2020-05-15 03:28:48
  • OfStack

preface

The Find command on the Linux system has powerful search capabilities and can traverse the entire file system. So the find command is resource-intensive and sometimes even time-consuming, so it is recommended that it be executed in the background.

The Find command format is as follows:

find pathname -options [-print -exec -ok …]

A simple format to remember:

find <指定目录> <指定条件> <指定动作>

Action parameters

1, -exec command name {} \;

Execute the unix command given to the qualified file without asking the user if he needs to execute the command. {} indicates that the parameter of the command is the file found, and the end of the command must be "\;" End, "{}" and "\;" There must be one space between them.

2, -ok command name {} \;

Execute the Linux command given to a qualified file, and unlike exec, it asks the user if they need to execute the command.

3, - ls

List all files found in detail.

4. -fprintf file name

Writes the found file name to the specified file.

5, - print

Displays the found file name on the standard output device.

6, - printf

The format can be used in a book about the C language.

Command options

1, - name

Look for files by filename.

2, - perm

Find files by file permissions.

3, - prune

Use this 1 option to keep the find command from looking in the currently specified directory, and -prune will be ignored by the find command if the -depth option is used at the same time.

4, - user

Find the file by file owner.

5, - group

Find files by the group they belong to.

6, - nogroup

Find a file that does not have a valid group to which it belongs, i.e. the group to which it belongs does not exist in /etc/groups.

7, - nouser

Find a file with no valid owner, that is, the owner of the file does not exist in /etc/passwd.

8. -newer file1! file2

Find files that are newer than file file1 but older than file file2.

9, - regex pattern

The file name matches the regular expression pattern. This is a match for the entire path, not a search file. For example, to match a file named './fubar3', you can use the regular expression '.bar.' or '.* b.3 ', but not 'b.*r3'.

10 - type

Find a file of type 1, such as:

b - block device file. d - directory. c - character device file. p - pipeline file. l - symbolic link file. f - general file

10. -size n: [c]

Find a file with a file length of n block, with c indicating the file length in bytes.

11, - depth

When looking for a file, first look for the file in the current directory and then look in its subdirectory.

12, - fstype

Look for files located on a type 1 file system. These file system types can usually be found in the configuration file /etc/fstab, which contains information about the file system in the system.

13, - mount

Do not cross the file system mount point when searching for files.

14, - follow

If the find command encounters a symbolic link file, it tracks to the file to which the link points.

15, - cpio

Use the cpio command for matching files to back them up to a tape device.

16. Time control

-mtime -n +n

Find the file by the time it has changed, -n means that the file has changed within days of n, + n means that the file has changed within days of n. The find command also has -atime and -ctime options, but they are both similar to -mtime option 1, looking for files by time node, but with some differences:

-amin n finds the last file on the system that N accessed in minutes -atime n finds the last n* 24-hour access file in the system -cmin n finds the last N minute in the system where the file state was changed -ctime n finds the last file on the system that n* changed file status 24 hours a day -mmin n finds the last N minute file data changed in the system -mtime n finds the last file in the system that n* changed file data 24 hours a day

Logic control

Logic and


expr1 -a expr2

expr1 -and expr2

Find files that satisfy both the criteria expr1 and expr2, for example, find files that belong to neither the master nor the group in the whole system:


find / -nogroup  � a  � nouser

Logic or


expr1 -o expr2

expr1 -or expr2

Find a file that meets the criteria expr1 or expr2, for example, find a file in the tmp directory that ends in ".sh "or ends in".log ":


find /tmp -name  " .sh "  -o -name  " .log " 

Logic is not


-not expr

Find files that do not meet the expr criteria, such as files belonging to a user other than root in the /tmp directory:


find /tmp -not -user root -exec ls -l {} \;

Some typical applications

Recursively modify all directory permissions under the directory (only modify the directory, not the file)

Three ways:


1 ,  find path -type d -exec chmod 744{} \; ( There is a semicolon at the end of this sentence )

2 ,  find path -type d | xargs chmod 744

3 ,  chmod 755 `find -type d`

Recursively modify the permissions of all normal files in the directory (only modify files, do not modify the directory)

Three ways:


1 ,  find path -type f -exec chmod 644 {} \; 
2 ,  find path -type f | xargs chmod 644 
3 ,  chmod 755 `find -type f` 

Recursively deletes all files of the execution type

For example, recursively delete the.exe normal file in the current directory:


expr1 -and expr2
0

In addition to -exec, you can also use pipes, such as recursively deleting.deb files in the current directory:


expr1 -and expr2
1

Count the number of lines of code


expr1 -and expr2
2

This command may fail on other platforms, so you can use grep to filter files:


wc -l `find $path | grep ".*\.\(py\|html\|js\|css\)$"`

conclusion

The above is the whole content of this article, I hope the content of this article to your study or work can bring 1 definite help, if you have questions you can leave a message to communicate.


Related articles: