Find files using command in Linux

The find command
finds the file name or directory name under the absolute path
find /etc - name XX //absolute file name or directory name
find /etc -iname XX //case-insensitive file name or directory name search


Wildcard lookup
find /etc/ -name '*XX*' //* wildcard any character
find /etc/ -name 'XX?' //? wildcard a character
find /etc/ -name '[XX].sh' //[any character in the wildcard]
find /etc/ -name XX -user //Find files by belonging user and group
find /etc/ -name XX -group //Find files by their owning users and groups
find /tmp -uid uid //find files with uid 500
find /tmp -gid 500 //find files with gid 500
find /tmp /etc -name XX //Multiple paths are searched together
find /tmp -name 'XX.sh' -a -user root //Use -a to connect two && conditions
find /tmp -name 'XX.sh' -o -user root //Use -o to connect two || conditions
find /tmp -not -user root //Use -not to negate the condition


Find files by timestamp dimension
stat /etc/tmp

 #-atime //Last visit time
 #-mtime //The last content modification time
 #-ctime //The last attribute modification time
 #-amin //The unit is days
 #-mmin //The unit is minutes
 #-cmin //The unit is seconds

find / -name '*.xml' -a -amin -30 // means to find files that have been accessed within 30 minutes
find / -name '*.xml' -a -atime +5 //mark to find files that have been accessed more than 5 days ago


Find files by file type
-type
   -f // normal file
   -d //directory file
   -l // link file
   -b //block device file
   -c //Character device file
   -p //pipe file
   -s //socket file
 find /tmp -type s //find socket file

  
Find by file size
  -size
find /tmp -size 2M //Files with a size of 2M
find /tmp -size +2M //Find files larger than 2M in the /tmp directory
find /tmp -size -2M //Find files smaller than 2M in the /tmp directory


Find files based on file permissions
  -perm
find /tmp -perm 755 //Find files whose permissions are 755 in the /tmp directory
find /tmp -perm +222 //Indicates that as long as there is a matching write permission for a type of user (owner, group, others)
find /tmp -perm -222 //Indicates that all categories of users must have write permissions


Constraint behavior after find command
find /tmp -perm 755 -ls //Display it with ls after finding it
find /tmp -perm 755 -ok //When executing the command after the search, ask the user whether to execute it
find /tmp -name '*.sh' -exec chmod u+x {} \; //Do not ask the user when executing the command after the search, directly execute '{}' Here means the found file


Practical use of some tricks
find  /tmp  -name  '*.imi' |xargs chmod 700;
find /tmp -atime +30 –exec rm –rf {} \; //delete the found files that have not been accessed for more than 30 days

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326368997&siteId=291194637