Non-real-time search (database search): locate
locate features:
- Fast search speed
- fuzzy search
- Non-real-time lookup
- Searches for the full path of the file, not just the file name
locate usage
-i case-insensitive search
-n N List only the first N matching items
-r uses basic regular expressions
Search for files containing “conf” in their name or path
locate conf
#Use Regex to search for files ending with “.conf”
locate -r ‘\.conf$’
[root@centos8 ~]#touch test2.log [root@centos8 ~]#locate test2.log [root@centos8 ~]#updatedb #Update database [root@centos8 ~]#locate test2.log /root/test2.log
Real-time search: find
find features:
- Search speed is slightly slower
- exact search
- Real time search
- Rich search conditions
- May only search directories where the user has read and execute permissions
Specify the search directory level
-maxdepth level Maximum search directory depth, files in the specified directory are level 1 -mindepth level Minimum search directory depth
Search according to files and inodes
-name “file name” #Supports the use of glob, such as: *, ?, [], [^], wildcards must be enclosed in double quotes
-iname “File name” #Does not distinguish between uppercase and lowercase letters
-inum n #Search by inode number
-samefile name #Files with the same inode number
-links n #File with n number of links
-regex “PATTERN” #match the entire file path with PATTERN, not the file name
Search according to owner and group
-user USERNAME #Find files owned by the specified user (UID)
-group GRPNAME #Find files whose group is the specified group (GID)
-uid UserID #Find the file whose owner is the specified UID number
-gid GroupID #Find files whose group is the specified GID number
-nouser #Find files without owners
-nogroup #Find files that do not belong to a group
Search by file type
The -type command tells
find
to execute the following xx commands on each file found.
TYPE can be of the following form:
f: ordinary file
d: directory file
l: symbolic link file
s: socket file
b: block device file
c: character device file
p: pipe file#View the directory of /home
find /home –type d -ls
And or not
And: -a, the default multiple conditions are AND relationships, so -a can be omitted
or: -o
Not: -not !
# Search for all files with .conf suffix in /etc/ except the /etc/security directory. find /etc -path '/etc/security' -a -prune -o -name "*.conf"
Search based on file size
-size [ + |-]#UNIT #Common units: k, M, G, c (byte), please note that it is case sensitive
#UNIT: # means (#-1, #], such as: 6k means (5k, 6k]
-#UNIT # represents [0,#-1], such as: -6k represents [0,5k]
+ #UNIT # means (#,∞), such as: + 6k means (6k,∞)
find / -size + 10G [root@centos8 ~]#find / -size + 10G /proc/kcore find: /proc/25229/task/25229/fd find /etc -type f -size + 1M -ls #Ordinary file, larger than 1M
Search with permissions
find -perm 755 will match files whose permission mode happens to be 755
find -perm /222 will match whenever anyone has write access
find -perm -222 will only match if everyone has write permissions
find -perm -002 will only match if other person (other) has write permission
Processing actions
-print: Default processing action, displayed on the screen
-ls: Similar to executing the “ls -dils” command format output on the found files
-fls file: Save the long format information of all found files to the specified file, equivalent to -ls > file
-delete: Delete the found files, use with caution!
-ok COMMAND {} \; Execute the command specified by COMMAND for each file found. Before executing the command for each file, it will
Interactively ask for user confirmation
-exec COMMAND {} \; Execute the command specified by COMMAND for each file found
{}: used to refer to the found file name itself
#Add .orig after the suffix .conf in etc find /etc/ -name "*.conf" -exec cp {} {}.orig \; -name "*.conf" is used to find files ending with ".conf". -exec cp {} {}.orig \; is used to copy the found files to the same directory and add the ".orig" suffix. #Prompt to delete temporary files of joe that exist for more than 3 days find /tmp -ctime + 3 -user joe -ok rm {} \; #Look for files in the home directory that can be written by other users find ~ -perm -002 -exec chmod o-w {} \; #Find ordinary files with permission 644 under /data and suffix sh, and add execution permissions find /data –type f -perm 644 -name "*.sh" –exec chmod 755 {} \;
Based on timestamp
#With “day” as the unit
-atime [ + |-]#
# # means [#,# + 1)
+ # # means [# + 1,∞]#Search based on permissions
-# # means [0,#)
The -mtime # option is used to search the file directory for the modification time of the file, and the unit is calculated in 24 hours;
-ctime # Used to search for file change time files or directories, the unit is calculated in 24 hoursThe time when file metadata (such as permissions, owners, links, etc.) was modified
#With “minutes” as the unit
-amin #Search for files or directories that have been accessed at the specified time, in minutes
-mmin #Find files or directories that have been changed at the specified time, in minutes
-cmin #Find files or directories that have been changed at the specified time;
find /var ! \( -user root -o -user postfix \) -mtime -7 -ls is not 7-day access
xargs is a filter for passing parameters to other commands
cat test.txt | xargs Multi-line input and single-line output: cat test.txt | xargs -n3 -n option multi-line output: a b c d e f g h i j k l echo "nameXnameXnameXname" | xargs -dX -d option can customize a delimiter: name name name name #When you use rm to delete too many files, you may get an error message: /bin/rm Argument list too long. Use xargs to avoid this problem: find . -type f -name "*.log" -print0 | xargs -0 rm -f #Count the number of lines of all php files in a source code directory: find . -type f -name "*.php" -print0 | xargs -0 wc -l #Find all jpg files and compress them: find . -type f -name "*.jpg" -print | xargs tar -czvf images.tar.gz #Combined with the -t option, you can print out the commands executed by xargs ls | xargs -t -I{} echo {} #-p option will pop up a confirmation when executing each command. You can use the -p parameter when you need to confirm each operation very accurately. For example, ## for example, to find the .log file in the current directory, each deletion requires confirmation. : find . -maxdepth 1 -name "*.log" | xargs -p -I{} rm {}
Multi-line input and single-line output: