In many cases, we need to use conditions to find the desired file. Flexible use of the file search tool can get twice the result with half the effort.
Content:
File Search:
1. Locate:
Non-real-time, fuzzy match, search is performed based on the entire system file database;
# Updatedb: manually generate a file database
Advantage: fast
2. Find:
Precision
Supports multiple search criteria
Traversing all files in the specified directory to complete the search is slow;
Search Path: the current directory by default
Search criteria: All files in the specified path by default
Processing operation: Display by default
-Name 'filename': exact match of file names
Wildcard File Name:
*: Any character of any length
?
[]
-INAME 'filename': the file name is case insensitive when matching.
-RegEx pattern: file name matching based on regular expressions. Note: Regular Expressions all match each other,
For example: Find-/etc-name 'passwd'
/Etc/PAM. d/passwd
/Etc/passwd
Find/etc-RegEx 'passwd' does not match any content.
Find/etc-RegEx '/etc/passwd.
/Etc/passwd
-User Username: search by owner
-Group groupname: search by group
-UID uid: search by UID (when the user corresponding to the UID is deleted, only the UID number is left, which is convenient for finding the residual files of the deleted users .)
-Gid: search by GID (same as above)
-Nouser: Search for files with no owner
-Nogroup: Search for files without groups
-Type
F: Common File
D: Directory
C: character file
B: Block devices
L: Link file
P: MPS queue
S: Socket
# K (for example: 10 K, So 9-10 K will be displayed, he will think that more than 9 K is also 10 K)
# M
# G
-
-O
-Not
/Tmp directory, which is not a directory and cannot be a socket-type file
Note: this can be combined for example: Find/tmp/-not \ (-type D-O-type S \) is equivalent
Find/TEP/-not-type D-not-type S
In the/tmp/test directory, the owner is neither user1 nor user2;
By days
-Mtime: modification time
-Ctime: Change Time
-Atime: Entry Time
[+ |-] #
By minutes:
-Mmin
-Cmin
-Amin
[+ |-] #
-Perm mode: exact match
/Mode: any matching condition is met.
-Mode: The File Permission can fully include this mode.
-644
644: RW-r --
755: rwxr-XR-x
750: rwxr-x ---
Find./-Perm-001
-Print: Display
-Ls: displays the details of each file in a format similar to LS-L.
-OK command {} (here the placeholder represents the query result) \; each operation requires user confirmation
-Exec command {}\;
Exercise:
1. Find all the files whose owner is root and whose group is mail in the/var directory;
Find/var-User Root-group mail
2. Search for files that do not belong to root, bin, or student under the/usr directory;
Find/usr-not-User Root-a-not-user bin-a-not-user student
Find/usr-not \ (-User Root-o-user bin-o-user student \)
3. Search for files in the/etc directory that have been modified in the last week and do not belong to the root and student users;
Find/etc-mtime-7-not \ (-User Root-o-user student \)
Find/etc-mtime-7-not-User Root-a-not-user student
4. Search for files with no owner or group on the current system that have been accessed in the last day, and change their owner group to root;
Find/\ (-nouser-o-nogroup \)-a-atime-1-exec chown root: Root {}\;
5. Search for a file larger than 1 MB in the/etc directory and write the file name to the/tmp/etc. largefiles file;
Find/etc-size + 1 m>/tmp/etc. largefiles
Find/etc/-size + 1 m-exec echo {}>>/tmp/etc. largefiles \;
6. Find the file in the/etc directory where all users do not have the write permission and the detailed information is displayed;
Find/etc-not-Perm/222-ls
The xargs command can always be followed by the find command (no placeholder is required, and the output result is not one in each line, but separated by spaces)
For example, find/etc-size + 1 M | xargs echo>/tmp/etc. largefiles
Conclusion: The find function is still very powerful.
This article is from the "Tiandao reward diligence" blog and is not reposted!
Linu Study Notes 8 (find files)