The philosophy of Linux one is that everything is file, and everything in Linux exists in the form of files. It is difficult to find and deal with so many files. Therefore, some commands are needed to locate and locate files.
File lookup: Search for files by file name or file attributes
Locate
For fuzzy search:
Based on a dedicated database for lookups, the database should be created in advance and updated regularly
You can manually update the locate database by using the UpdateDB command
Find faster, less accurate
Find
Exact search:
Find high precision and slow speed
A traversal scan of a file name or property at the specified location, and a find operation on the root directory is not recommended
Real-time Search
Use the Find command to search only the directory where the current user has read and Execute permissions
Find
Search for files in a directory hierarchy
Find [OPTIONS] [Find path] [find condition] [processing action]
Find path: Default to the current working directory, you can specify a specific directory path
Find criteria: The criteria for this search, can be file name, file size, file type, file permissions; default is all files in the specified directory
Handling actions: Processing operations on eligible files; default is to output the lookup results to the display
Search by file name:
-name file name, which supports the use of wildcards (*?). [] [^])
-iname file name, ignoring letter case, wildcard characters supported
Search based on the inode number of the file:
-inum inode Number: Hard links have the same inode number: find the corresponding file name and path by the given inode number
-samefile name: Finds the corresponding inode number by the given file name, and then identifies the file with the same inode number
and path
-link N: Find all files with number of connections n
Match according to regular expression:
-regex pattern: Matches the entire file path string with pattern, not just the name of the given file
Search according to the file's owner and owner group:
-user uname: Search According to the user name of the owner for the specified user
-uid UID: Search by Owner for a UID
-group Gname
-gid GID
-nogroup: There is no corresponding group name on the amount group of the file
-nouser: The owner of the file does not have a corresponding user name
Find by File Reason
-type File types
B: Block device
C: Character device
D: Catalog file
F: Normal file
L: Symbolic Link file
P: Normal file
S: Socket file
-xtype file type: symbol link file matching needs to be matched with other options
To find based on a timestamp:
-atime [+|-]n: Search based on access time, in days
-ctime [+|-]n: Search by change time
-mtime [+|-]n: Find based on modified time
N:[N-N+1)
A long time before +n:[n+1-)
-n:[n-now)
-amin [+|-]n: In minutes
-cmin [+|-]n
-mmin [+|-]n
Example: Find files that have been modified in the last week of the/etc directory and that are not root and suse users;
Find/etc-ctime-7-a-user root-a-user SuSE
In the/L o G S directory, look for files that change time before 5th and delete them:
Find Logs-type f-mtime +5-exec-ok rm {} \;
Find based on the size of the file
-SIZE[+|-]N[CWBKMG]
N: (N-1,n]
-N:[0,N-1]
+n: (N,zhengwuqiong)
Example:
Find-size +2k
All files larger than 2KB in the current directory;
find-size 2k
Files between all 1kb-2kb in the current directory;
find-size-2k
All files less than 1KB in the current directory;
Combination conditions:
-A: Logic and default can be omitted
-O: Logical OR
-not,| : Logical Non-
Logical combination condition follows Demogan law
Example: Find files that do not belong to root, Bin, CentOS and other users in/usr directory;
Find/usr-not-user root-a-not-user bin-a-not-user CentOS
Search by permissions:
-perm [/|-]mode
Mode: Exact Match of specified permissions
/mode: implies a logical or relationship, any permission bit of the permissions in the right to a single match is true
-mode: Implies a relationship of logic and each permission bit must contain the specified permission bit
Example: Find/etc/rc.d/init.d directory, all users have execute permission and other users have write permission files;
Find/etc/rc.d/init.d-prem/111-a-perm-002
All have to take back any one No
! (A and B and c) =!a or!b or!c
All are not taken against any one that has
! (!a and!b and!c) = A or B or C
All have to take back any one No
! (A and B and c) =!a or!b or!c
All are not taken against any one that has
! (!a and!) B with! c) = A or B or C
Handling actions:
-print: Output to display screen, default action
-ls: The Ls-li command is displayed for the results of the lookup trail
-exec COMMAND {} \; Non-interactive
-ok COMMAND {} \; Interactive
Executing command commands on found results
{}: Placeholder for information that refers to all files found by the Find command
-exec and-ok substitution operations
chmod a-r $ (find-perm-444-type f)
Find-perm-444-type F | Xargs chmod a-r
Xargs
Linux Beginner's File search