find command details

Posted by andrew_ww on Wed, 22 Dec 2021 17:32:56 +0100

Command format

find path -option [-print] [-exec -ok command] {} \;

Command parameters

  • -Path: the directory path found by the find command. For example, use To represent the current directory and / to represent the system root directory
  • -The print: find command outputs the matching file to standard output
  • -exec: the find command executes the shell command given by this parameter on the matching file. The form of the corresponding command is' command '{} \;, Note {} and \; Space between
  • -ok: it has the same function as - exec, but executes the shell command given by this parameter in a safer mode. Before executing each command, a prompt will be given to let the user determine whether to execute it. Example: find- name . svn | xargs rm -rf

options

-name filenameFind a file named filename
-permFind by execution permission
-user usernameFind by file owner
-group groupnameFind by group
-mtime -n +nFind files by file change time, - N refers to within n days and + n refers to before n days
-atime -n +nCheck gin by file access time: 0px ">
-ctime -n +nFind files by file creation time, - N refers to within n days and + n refers to before n days
-nogroupCheck the file without valid group, that is, the group of the file does not exist in / etc/groups
-nouserCheck that there is no valid owner of the file, that is, the owner of the file does not exist in / etc/passwd
-newer f1 !f2Find file, - N refers to within n days and + n refers to before n days
-ctime -n +nFind files by file creation time, - N refers to within n days and + n refers to before n days
-nogroupCheck the file without valid group, that is, the group of the file does not exist in / etc/groups
-nouserCheck that there is no valid owner of the file, that is, the owner of the file does not exist in / etc/passwd
-newer f1 !f2Look up files that are newer than f1 but older than f2
-type b/d/c/p/l/fQuery is a block device, directory, character device, pipeline, symbolic link, ordinary file
-size n[c]Look up a file with a length of n blocks [or n bytes]
-depthMake the search complete the directory before entering the subdirectory
-fstypeLook up files that are newer than f1 but older than f2
-type b/d/c/p/l/fQuery is a block device, directory, character device, pipeline, symbolic link, ordinary file
-size n[c]Look up a file with a length of n blocks [or n bytes]
-depthMake the search complete the directory before entering the subdirectory
-fstypeLook up files located in a certain type of file system, which can usually be found in / etc/fstab
-mountDo not cross the file system mount point when querying files
-followIf a symbolic link file is encountered, the file to which the link refers is tracked
-cpio %;Look up files located in a certain type of file system, which can usually be found in / etc/fstab
-mountDo not cross the file system mount point when querying files
-followIf a symbolic link file is encountered, the file to which the link refers is tracked
-cpioUse the cpio command on the matching files and back them up to a tape device
-pruneIgnore a directory

Command example

$find ~ -name "*.txt" -print #Check in $HOME txt file and display
$find . -name "*.txt" -print #Look it up in the current directory txt file and display
$find . -name "[A-Z]*" -print #Look up files that start with capital letters
$find /etc -name "host*" -print #Look up files starting with host
$find . -name "[a-z][a-z][0–9][0–9].txt" -print #Look up txt files that start with two lowercase letters and two numbers
$find . -perm 755 -print #Check files with permission of 755
$find . -perm -007 -exec ls -l {} \;  #Check the files that all users can read, write and execute, the same as - perm 777
$find . -type d -print #
$find . ! -type d -print #
$find . -type l -print #
$find . -size +1000000c -print #Check files with a length greater than 1Mb
$find . -size 100c -print # Check the file with length of 100c
$find . -size +10 -print #Check the documents whose length exceeds 10 expired and invalid blocks (1 block = 512 bytes)
$find /etc home apps -depth -print | cpio -ivcdC65536 -o /dev/rmt0
$find /etc -name "passwd*" -exec grep "cnscn" {} \; #See if there are cnscn users
$find . -name "yao*" | xargs file
$find . -name "yao*" | xargs echo "" > /tmp/core.log
$find . -name "yao*" | xargs chmod o-w

$find -name april* #Find files starting with april in the current directory
$find -name april* fprint file #Find the file starting with april in the current directory and output the result to file
$find -name ap* -o -name may* #Find files that start with ap or may
$find /mnt -name tom.txt -ftype vfat # Find the name Tom. Under / mnt Txt and the file system type is vfat
$find /mnt -name t.txt ! -ftype vfat #Find the name Tom. Under / mnt Txt and the file system type is not vfat
$find /tmp -name wa* -type l #Under / tmp, find a file with a name beginning with wa and a type of symbolic link
$find /home -mtime -2 #Check the files changed in the last two days under / home
$find /home -atime -1 #Check the files accessed within 1 day
$find /home -mmin +60 #Check the files changed 60 minutes ago under / home
$find /home -amin +30 #Check the files accessed in the last 30 minutes
$find /home -newer tmp.txt #Under / home, check the update time ratio TMP Txt file or directory
$find /home -anewer tmp.txt #Check the access time ratio TMP. At / home Txt file or directory
$find /home -used -2 #List the files or directories that have been accessed within 2 days after the file or directory has been changed
$find /home -user cnscn #Lists the files or directories belonging to the user cnscn in the / home directory
$find /home -uid +501 #List the files or directories with user ID greater than 501 in / home directory
$find /home -group cnscn #Lists the files or directories in / home that are grouped as cnscn
$find /home -gid 501 #List the files or directories with group id 501 in / home
$find /home -nouser #Lists files or directories in / home that are not local users
$find /home -nogroup #Lists files or directories in / home that do not belong to the local group
$find /home -name tmp.txt -maxdepth 4 #List TMP in / home Txt the maximum depth of time query is 3 layers
$find /home -name tmp.txt -mindepth 3 #Start from the second floor
$find /home -empty #Find files or empty directories of size 0
$find /home -size +512k #Check files larger than 512k
$find /home -size -512k #Check files less than 512k
$find /home -links +2 #Check the files or directories with more than 2 hard connections
$find /home -perm 0700 #Check the file or directory with permission of 700
$find /tmp -name tmp.txt -exec cat {} \;
$find /tmp -name tmp.txt -ok rm {} \;
$find / -amin -10 #Find files accessed in the last 10 minutes in the system
$find / -atime -2 #Find files accessed in the last 48 hours in the system
$find / -empty #Find files or folders that are empty in the system
$find / -group cat #Find files belonging to groupcat in the system
$find / -mmin -5 #Find the files modified in the last 5 minutes in the system
$find / -mtime -1 #Find the files modified in the last 24 hours in the system
$find / -nouser #Find files belonging to obsolete users in the system
$find / -user fred #Find the files belonging to the FRED user in the system
$find . -type f -exec ls -l {} \; #Check all ordinary files in the current directory and list them with the ls -l command in the - e x e c option
$find logs -type f -mtime +5 -exec   -ok   rm {} \; #Find files in the / logs directory that were changed before the 5th and delete them
$find ./ -mtime -1 -type f -exec ls -l {} \; #Query the files modified on that day
$find ./ -mtime -1 -type f -ok ls -l {} \;   #Query the file and ask if you want to display it
$find /tmp -name "*.h" -exec grep "SYSCALL_VECTOR" {} \; -print #Find all *. In / tmp h. Find "SYSCALL_VECTOR" in these files, and finally print out all file names containing "SYSCALL_VECTOR" 
$find . -size +3000k -exec ls -ld {}; #Find files larger than 3M on disk
$find *.c -exec cp '{}' /tmp ';' #Copy the found thing to another place