How to locate files in the file system quickly and efficiently? Linux provides us with some commands for file lookup classes, and we need to master the following commands:
http://blog.csdn.net/sailor201211/article/details/53290470
which
The command which will find the path to the executable file for an external command according to the user's path environment variable. Such as:
$ which gcc/usr/bin/gcc
Whereis
In contrast to the which command, the Whereis command can locate a binary file, source code file, and user manual document for an external command. Such as:
$ whereis gccgcc: /usr/bin/gcc /usr/lib/gcc /usr/bin/X11/gcc /usr/share/man/man1/gcc.1.gz
Locate
The above two commands (which and whereis) are used only to find files that correspond to external commands installed in the system, and for general user files such as personal documents, photos, and so on. At this point, you can use the Locate command to help us quickly locate any type of file. Such as:
$ locate CentOS-6.5-x86_64-bin-DVD1.iso/home/sailor-m/CentOS-6.5-x86_64-bin-DVD1.iso/home/sailor-m/VirtualBox VMs/CentOS-6.5-x86_64-bin-DVD1.iso
The command locate lookup file does not traverse the entire file system, but rather from a file information database that already exists in the system /var/lib/mlocate/mlocate.db , so the query is very fast. Distributions, such as RHEL6, typically schedule daily updates to the database in Scheduled tasks. However, if the user has just created the file, use the Locate command is not found. Such as:
$ touch ~/mystuff$ locate mystuff
If you update the database manually with the UpdateDB command, you can query:
$ sudo updatedb$ locate mystuff/home/sailor-m/mystuff
Find
The most frequently used and most powerful of the file lookup class commands is find, which can look up any type of file in the directory tree and set multiple search criteria. Command find is a real-time search for files, because it is slow to traverse the file system's directory tree. The syntax format for this command is:
Find [-h] [-l] [-p] [-D debugopts] [-olevel] [path ...] [Expression]
where -H , -L and -P three options are used to process symbolic links, -D debugoptions for diagnostics and debugging, and [-Olevel] for query optimization. These options are not commonly used, but you can refer to the man manual for specific usage. So the common form of find can be simplified to:
Find [path ...] [Expression]
- The directory path that the Path:find command looks for. For example, use. To represent the current directory, and/to represent the system root directory
- Expression:expression can be divided into--"-options [-print-exec-ok ...]"
- Common options for the-options,find command, which are generally specified for the search criteria, are given below for an example
- -print, output matched files to standard output
- -exec, executes the shell command given by the parameter to the matching file. The corresponding command is in the form of ' command ' {} \;, note the space between {} and \;
- -ok, which is the same as-exec, simply executes the shell command given by the parameter in a more secure mode, prompting the user to determine whether to execute before executing each command.
Someone also summarizes the structure of the Find command as:
Find [directory] [conditions] [action]
Here are some examples of find lookups:
(1) -name Search by file name
To find files with the file name ending with. conf in the/etc directory:
$ find /etc/ -name "*.conf"
(2) -user & -group Search by Owner or group of files
To find files in the current directory where the user owner is sailor:
$ find ./ -user sailor
It can also be omitted when looking for the directory as the current directory ./ .
(3) -type Find a file of a certain type, such as:
- B-block device files.
- D directory.
- C-character device file.
- P Pipeline file.
- L Symbolic link file.
- F Common Files.
To find all subdirectories in the/etc directory:
# find /etc/ -type d
(4) -size According to the size of the file to find
-sizeYou can follow a unit that represents a size, such as K, M, or G, or - a + number that is less than or greater than. The following command looks up a 0-byte file in the/tmp directory and lists its details:
# find /tmp -size 0 -exec ls -l {} \;
Find files larger than 1G in the/tmp directory and delete them:
# find /tmp -size +1G -exec rm {} \;
The effect of the above two commands is equivalent to:
# ls -l `find /tmp -size 0`# rm `find /tmp -size +1G`
(5) -mtime & -ctime & -atime Search by Time
Where Mtime is the modified time of the file, that is, the time when the content of the file was modified; CTime is the time when the file attribute was changed; Atime is the file access time. This option can be followed by the number "n" or +n "or"-N ", respectively, indicating that the condition is exactly n days from today, or more than n days, or less than n days.
If you find files that have not been accessed within 7th, and delete them:
./ -atime +7 -exec rm {} \;
(6) -perm Follow the file's permissions to find
To find the file with permission 0777 in the current directory:
$ find ./ -perm 0777
The permission number can be preceded by a number that indicates that at least one permission is satisfied. To find common files that are at least writable by the same group of users in the current directory:
$ find ./ -type f -perm -020
(7) -maxdepth Specify the search depth
The following can be followed by a number n indicating a maximum entry to the N-1 level subdirectory for lookup. To find files with the file name ending with ". conf" only in the/etc directory (not including its subdirectories):
# find /etc/ -name "*.conf" -maxdepth 1
File Lookup command Find also has many other useful options, such as -nouser and -nogroup can find files that are not valid and belong to a group. See Find man page can see dozens of screen of the document, it is powerful, we only mastered its common usage, the rest of the use of slowly experience groping can.
File Lookup class commands under Linux (reproduced)