File Lookup
Find eligible files on the file system;
Implementation tools: Locate, find
Locate
Rely on the pre-built index library;
Automatic implementation of the system (periodic tasks);
Manually update the database (UPDATEDB);
Operating characteristics:
Fast search speed;
Fuzzy search;
Non-real-time lookup;
Locate [OPTION] ... PATTERN ...
-B: Matches only the base name in the path;
C: Statistics of the total number of eligible documents;
-r:bre (based on basic regular expressions)
Note: The index build process needs to traverse the entire root filesystem, consuming resources extremely;
Find
Real-time Lookup tool to complete file search by traversing the file system hierarchy under the specified starting path;
Operating characteristics:
The search speed is slightly slower;
Accurate search;
Real-time search;
Usage:
Find [OPTIONS] [Find starting path] [find condition] [processing action]
Find the starting path: Specify a specific search target starting path, default to the current directory;
Find criteria: The specified lookup criteria can be based on file name, size, type, affiliation, permissions, and so on, the default is to find all the files under the specified path;
Handling actions: Actions that are made on files that match the search criteria, such as deletions, which are output to standard output by default;
Search criteria:
Expressions: Options and tests
Test: The result is usually a Boolean ("true", "false")
Search by file name:
-name "pattern"
-iname "pattern"
supports GLOB-style wildcard characters;
*, ?, [], [^]
-regex pattern: Finds a file based on a regular expression pattern, which matches the entire path, not its name;
Find based on file affiliation:
-user USERNAME: Find all files belonging to the owner of the specified user;
-group GRPNAME: Finds all files belonging to the specified group of groups;
-uid uid: Finds all files that belong to the UID specified by the master;
-gid GID: Finds all files belonging to the specified GID of the group;
-nouser: Find files that are not owned by the master;
-nogroup: Find files without a group;
Find by File type:
-type Type:
F: Normal file
D: Catalog file
L: Symbolic Link file
B: Block device files
C: Character device file
P: Pipeline File
S: Socket file
Combination test:
With:-A, default combination logic;
Or:-O, as long as one of the two is compatible
Non:-not,!
Practice:
1, find out the/tmp directory subordinate to the main non-root all files;
2. Find the file name in the/tmp directory that does not contain the fstab string;
3, find out the/tmp directory subordinate Master is non-root, and the file name does not contain fstab string files;
! A-a! B =! (A-o B)
! A-o! B =! (A-a B)
Find by File Size:
-size [+|-] #UNIT
Common units: K, M, G default byte
#UNIT: (#-1, #)
-#UNIT: [0,#-1]
+ #UNIT: (#, OO)
Find by Time stamp:
In "Days" as the unit:
-atime [+|-]#
#:[#, #-1)
-#: (#, 0]
+#: (Oo, #-1)
-mtime
-ctime
In "Minutes" units:
-amin
-mmin
-cmin
Search by permissions:
-perm [/|-]mode
Mode: precise permission matching;
/mode: Any one (r,w,x) of the permissions of any class of users (U,g,o) is satisfied with the condition;
There is a "or" relationship between 9-bit permissions;
-mode: Each class of users (U,g,o) in the permissions of each bit (r,w,x) at the same time meet the condition is satisfied;
There is a "and" relationship between 9-bit permissions;
Handling actions:
-print: Output to standard output, default action;
-ls: Similar to the "ls-l" command for the found file, the output file details;
-delete: Delete the found file;
-fls/path/to/somefile: Saves the long format information of all files found to the specified file;
-ok COMMAND {} \; : Executes commands for each file that is found, and each operation is confirmed by the user;
-exec COMMAND {} \; : Executes commands for each file that is found;
Note: Find passes the file path found to the following command, it is first to find out all eligible file paths, and one-time pass to the following command;
However, some commands cannot accept too long arguments, at which point the command execution fails, and another way to circumvent this problem:
Find | Xargs COMMAND
Homework: Learn the use of XARGS commands;
Practice:
1. Find the main root of the/var directory, and belong to all files or directories of mail;
~]# find/var-user root-a-group mail-ls
2. Find all files or directories that do not belong to root, bin or hadoop under the/usr directory; in two ways;
~]# find/usr-not-user root-a-not-user bin-a-not-user Hadoop
~]# find/usr-not \ (-user root-o-user bin-o-user hadoop \)-ls
3, find in/etc directory in the last week of its content has been modified, and the owner is not the root user is not a Hadoop user files or directories;
~]# find/etc-mtime-7-a-not \ (-user root-o-user hadoop \)-ls
~]# find/etc-mtime-7-a-not-user root-a-not-user hadoop-ls
4. Find files or directories that are not in or belong to the current system and have been visited in the last week;
~]# Find/\ (-nouser-o-nogroup \)-atime-7-ls
5. Find all files that are larger than 1M and type ordinary files in/etc directory;
~]# find/etc-size +1m-type f-exec ls-lh {} \;
6, look for all the users in/etc directory do not have permission to write files;
~]# Find/etc-not-perm/222-type F-ls
7, Find/etc directory at least one class of users do not have permission to execute files;
~]# Find/etc-not-perm-111-type F-ls
8, find/etc/init.d/directory, all users have execute permission, and other users have write permission to all files;
~]# Find/etc-perm-113-type F-ls
Blog assignments: Find details
Linux Base File Lookup