“This is the 20th day of my participation in the Gwen Challenge.

The Linux lookup command is one of the most important and commonly used commands in Linux systems. Find commands to search and find lists of files and directories based on the criteria specified by the file that matches the argument. Lookup can be used under a variety of conditions, you can find files by permission, user, group, file type, date, size, and other possible criteria.

It is also one of the more complex commands to use.

usage

Its usage is as follows:

·find path-option [-print] [-exec   -ok   command ]   {} \;
Copy the code

The meanings are as follows:

  • Path: Indicates the path of the directory searched by the find command. For example, a dot represents the current directory and a slash represents the system root directory.
  • -print: find prints the matched files to standard output.
  • -exec: find Executes the shell command given by this parameter on the matched files. The corresponding command is of the form ‘command’ {}; , note {} and \; The space between.
  • -ok: Serves the same purpose as -exec, except that the shell commands given by this parameter are executed in a more secure mode. Before each command is executed, a prompt is given for the user to determine whether to execute.
  • Option: various control parameters

The control parameters are as follows

-name   filename             Find a file named filename
-perm                        Search by execute permission
-user    username             Search by file owner
-group groupname            Search by group
-mtime   -n +n                -n = n days ago +n = n days ago
-atime    -n +n               GIN: 0px">
-ctime    -n +n              -n = n days ago +n = n days ago
-nogroup                     /etc/groups file does not have valid parent group
-nouser                     The owner of the file is not found in /etc/passwd-newer f1 ! f2-n = n days ago +n = n days ago
-nogroup                     /etc/groups file does not have valid parent group
-nouser                      The owner of the file is not found in /etc/passwd-newer f1 ! f2# check for files that are newer than F1 but older than F2
-type    b/d/c/p/l/f         # lookup is block device, directory, character device, pipe, symbolic link, plain file
-size      n[c]               Select * from files whose length is n bytes
-depth                       Complete the search before entering the subdirectory
-fstype                     # check for files that are newer than F1 but older than F2
-type    b/d/c/p/l/f         # lookup is block device, directory, character device, pipe, symbolic link, plain file
-size      n[c]               Select * from files whose length is n bytes
-depth                       Complete the search before entering the subdirectory
-fstype                      Look for files in a certain type of file system, usually found in /etc/fstab
-mount                       File lookup without crossing filesystem mount points
-follow                      If a symbolic link file is encountered, trace the file to which the link refers
-cpio                %;      Look for files in a certain type of file system, usually found in /etc/fstab
-mount                       File lookup without crossing filesystem mount points
-follow                      If a symbolic link file is encountered, trace the file to which the link refers
-cpio                        Use the CPIO command on the matching files to back them up to the tape device
-prune                       # ignore a directory
Copy the code

The sample

Basic lookup command

1. In the current directory, search for the file named test.js

find test.js
Copy the code

2. Search for files in the home directory

Look for all files named test.js in /home

find /home -name test.js
Copy the code

3. Search for files regardless of file name case

Look for all files named test.js in /home, regardless of case, ‘test.js’ will also match

find /home -iname test.js
Copy the code
  1. Find directories by name

In the current directory, look for the directory named test. Where. Indicates the current directory, / indicates the root directory, and ~ indicates the main directory

find . -type d -name test
Copy the code
  1. Find js files by name

In the current directory, look for the file named test.js

find . -type f -name test.js
Copy the code
  1. Find all js files in the directory

All js files in the current directory

find . -type f -name "*.js"
Copy the code
  1. Find the file and delete it

Find all test.js and TXT files in the current directory and delete them

find . -type f -name "test.js" -exec rm -f {} \;
find . -type -f -name "*.txt" -exec rm -f {} \;
Copy the code
  1. Found all empty files
find . -type f -empty
Copy the code
  1. All empty directories are found
find . -type d -empty
Copy the code

Find files based on permissions

  1. All files with permission 777
find . -type f -perm 777 
Copy the code
  1. Find all files that are not 777 privileges
find . -type f ! -perm 777
Copy the code
  1. Find all 777 files and change permission to 644
find . -type f -perm 777 -exec chmod 644 {} \;
Copy the code
  1. Find all hidden files
find . -type f -name ". *"
Copy the code

Find files by owner and group

  1. Find files based on a user

Find the file named test.js owned by user root in the current directory

find . -user root -name test.js
Copy the code
  1. Find all files for a user

Find all files belonging to Moke in the current directory

find . -user moke 
Copy the code
  1. Find all files owned by a user group

Find all files owned by the staff user group in the current directory

find . -group staff
Copy the code

Find files and directories by date and time

  1. The current directory looks for files that were modified in the last day
find . -mtime -1
Copy the code
  1. Home directory Search for files that have been modified in the last 50 to 100 days
find ~ -mtime +50 -mtime -100
Copy the code
  1. The root directory looks for all files that have changed in the last hour
find / -cmin -60
Copy the code

Find files and directories based on file size

  1. Find all 50MB files in current directory
find . -size 50M
Copy the code
  1. Search for all files in the current directory that are larger than 50M and smaller than 100M
find . size +50M -size -100M
Copy the code
  1. The home directory finds all MP3 files larger than 10M and deletes them
find ~ -type f -name "*.mp3" -size +10M -exec rm {} \;
Copy the code

Replace tool

Because the find command is more complex to use, there are alternatives.

  • fd
  • locate

Other articles

  • Shenbing Blade: Environment variables, path switching, flow operation for the beginning of shell
  • Magic sword: shell file permissions, sudo ls | | chmod
  • Cut command for Linux shell
  • Magic Sword: Linux shell programming assignment, functions, conditional judgment, wildcards, command replacement

Reference thanks

  • Examples of common use of find in Linux
  • Shell Tools and Scripting
  • Linux find command use all summary, read not not use!