Text lookup: grep egrep fgrep
File Lookup: Locate, find
The difference between locate and find
Locate has a self-maintained database (Linux self-built, associated with all files), Linux will periodically update it every day, and locate in this database to find, so the query is very fast, but the shortcomings are obvious, cannot be found in real time (for example, this command is not available when Linux is just installed), and there are few supported lookups.
The pros and cons of find are relative to locate, and find iterates through the specified path to find a file, so the more files you specify in the path, the less efficient the find lookup is, but find supports a very large number of formats, such as search by file name, meta-attribute status lookup, support for regular, and so on.
Locate
Non-real-time, fuzzy matching, lookup is based on the system-wide file database;
Fast speed
#updatedb, generate the file database manually
Find
Realtime
Accurate
Support for many search criteria
Traverse all files in the specified path for lookup, slow
Find Find Path Lookup criteria finds a future operation
Find path: Default to Current path
Find (Match) criteria: default to all files under the specified path
Processing operation: Default is Display
Matching Criteria:
-name ' filename ': Exact Match of file name
File name wildcard:
*: Any character of any length
? : Any character of a single length
[]:
...
Ignore case when-iname ' filename ' matches
-regex pattern: Matching files based on regular expressions
Pattern must be contained with an absolute path
-user Username: Based on owner Lookup
-group groupname: Search by genus Group
-uid UID: Search by UID
-gid GID: Search by GID
-nouser: Finding files that are not owned by the master
-nogroup: Finding files that are not owned by a group
-type:
P: Normal file
S:socket file
D: Catalog file
P: Pipeline File
L: Link File
...
-size
[+|-] #k: Find [greater than | less than] #k的文件
[+|-] #M: Find [greater than | less than] #M的文件
[+|-] #G: Find [greater than | less than] #G的文件
Note: The find command inside the search file size is not accurate, it will be greater than the previous number to less than the number of the next number as the current number, such as we look for files equal to 10k, then 9.2k, 9.31k, 10.1k and so will be treated as 10k and be matched to, and 9k 11.1k and so on will not
Combination conditions:
-A: With
-O: Or
-not: Non-
-mtime: According to the modified (modify) time
-atime: Accessing (Access) time
-ctime: Changing (change) time
[+|-]#:+ represents a time before--represents the day by default.
(Atime and CTime differences: atime modifying data, CTime modifying meta-attributes)
-mmin:
-cmin:
-amin:
[+|-]#:
Corresponds to TIME, Min stands for minutes
-perm mode: Exact match mode, e.g. 755 must match exactly 755
/mode: Partial matching MODE, as long as a match can be, such as R--RW----, as long as the other one of the three positions are matched to it, such as can be matched by: R--------、 RW-------, etc.
-mode: Fully contains the match, the matching mode must be equal to or can contain mode, such as R--RW----, then r--rwx---、 rw-rw----can match, and---rwxrwx, rwxr-xrwx, etc. can not match
Find uses regular matching examples:
[Email protected] ~]# find/etc/-regex "\<pas.*"
[Email protected] ~]# find/etc/-regex "/etc/\<pas.*"
/etc/passwd
/etc/passwd-
Processing operations:
-print: Display
-ls: ls-l-like format showing details of matching files
-ok COMMAND {} \; User confirmation is required for each operation
-exec COMMAND {} \;
{} represents a matching file, \; is a fixed terminator
Xargs and find-exec examples
[[email protected] ~]# find/tmp/-size-1m-exec echo {} >>tmp.txt \;
[email protected] ~]# cat Tmp.txt
/tmp/mysql.sock
/tmp/sess_e538a6cd81b0fed9d5c49f1b2f84dd80
/tmp/sess_c1f3bd3446ca4407d9a2a05bb4897030
[Email protected] ~]# find/tmp/-size-1m |xargs echo {} >>tmp1.txt;
[email protected] ~]# cat Tmp.txt
/tmp/mysql.sock
/tmp/sess_e538a6cd81b0fed9d5c49f1b2f84dd80
/tmp/sess_c1f3bd3446ca4407d9a2a05bb4897030
[Email protected] ~]#
Example:
1. Find all files that are root and belong to the group Mail for/var subordinate
find/var/-user Root-group Mail
2. Find files that do not belong to root, bin or student in/usr
find/usr/-not-user root-o-not-user bin-o-not-user Student
find/usr/-not \ (-user root-o-user bin-o-user student \)
3. Find files that have been modified in the last week of/etc and are not root and student users
find/etc/-mtime-7-not-user root-a-not-user Student
find/etc/-mtime-7-not \ (-user root-o-user student\)
4. Find files that are not owned or owned by the current system and have been visited in the last 1 days, and change their genus to root
Find/-nouser-o-nogroup-a-ctime-1-exec chown root:root {} \;
5. Find files larger than 1M under/etc/and write to/tmp/1.txt
find/etc/-size +1m-exec echo {} >>/tmp/1.txt \;
6, find/etc/All users do not have permission to write files, and display their details
find/etc/-not-perm/222-ls
Note that this is with/222 instead of-222
This article is from the "single Season rice" blog, please be sure to keep this source http://linzb.blog.51cto.com/5192423/1735061
Linux file Lookup