Detailed explanation of the five search commands for Linux files

Source: Internet
Author: User
Tags builtin regular expression file permissions

I. Linux search command overview

There are five Linux search commands:

Which: searches for the location of a system command in the PATH specified by the PATH variable and returns the first search result;
Type: used to identify whether a command is provided by a shell or an independent binary file outside the shell. If a command is an external command, use the-p parameter to display the path of the command, which is equivalent to the which command. The type command cannot be regarded as a search command;
Whereis: it can only be used for searching program names, and only binary files (parameter-B), man description files (parameter-m), and source code files (parameter-s) are searched );
Locate: similar to find-name, which allows you to quickly find files;
Find: The most common and powerful search command. You can use it to find any file you want.
Note:

Usually find is not very common, because the speed is slow!

Generally, whereis or locate is used to check whether it can be found.

This is because whereis and locate use databases to find data, so it is quite fast and there is no actual hard disk query, saving time.

The data searched for by whereis and locate is found by the created data in/var/lib. However, database updates are updated once a day by default (different systems may be different). Therefore, when a file is created or deleted, search for the file, whereis and locate will tell you the file "not found" because the database must be updated.

You can simply enter updatedb to update the database manually. The updatedb command reads the configuration in/etc/updatedb. conf, searches for the file name in the hard disk, and updates the entire database file.

The following describes how to use the above five commands:

II. which: find the system command location

The which command searches for the location of a system command in the PATH specified by the PATH variable and returns the first search result.

The which command format is as follows:

Which [-a] command
The which command parameters are as follows:

-A: list all commands that can be found in the PATH directory, instead of the first command that is found.
Example:

[Root @ www ~] # Which ifconfig
/Sbin/ifconfig
3. whereis: Program Name Search

The whereis command can only be used for searching program names, and only binary files (parameter-B), man description files (parameter-m), and source code files (parameter-s) can be searched ).

The format of the whereis command is as follows:

Whereis [-bmsu] file or directory name
The parameters of the whereis command are as follows:

Parameter description
-B only searches for files in binary format.
-M: only search for files in the manual path of the description file.
-S: only source files
-U: find other special files that are not included in the preceding three options.
Example:

[Root @ www ~] # Whereis ifconfig
Ifconfig:/sbin/ifconfig/usr/share/man/man8/ifconfig.8.gz
[Root @ www ~] # Whereis-m ifconfig
Ifconfig:/usr/share/man/man8/ifconfig.8.gz
4. locate: use the database to find files

The format of the locate command is as follows:

Locate [-ir] keyword
The parameters of the locate command are as follows:

-I: Case sensitivity differences are ignored;
-R: The implementation method followed by a regular expression.
For example:

[Root @ www ~] # Locate passwd
/Etc/passwd
/Etc/passwd-
/Etc/news/passwd. nntp
/Etc/pam. d/passwd
5. find: find any file

The format of the find command is as follows:

Find [PATH] [option] [action]
5.1 find parameter: file name-based search

Parameters related to file names are as follows:

-Name filename: find the file named filename. Filename can be expressed using a regular expression.
Example:

[Root @ www ~] # Find/-name passwd
Find the file named passwd.
5.2 find parameter: search based on file size

Parameters related to file size are as follows:

-Size: find the file with the SIZE equal to the SIZE;
-Size-SIZE: Searches for objects whose sizes are greater than the SIZE;
-Size + SIZE: Searches for objects whose sizes are smaller than the SIZE.
The unit of SIZE is:

C -- byte, in bytes;
W -- word (2 bytes );
B -- bit, block (512 bytes );
K-kilobytes;
M -- MB;
G -- Jilin byte.
Example:

[Root @ www ~] # Find.-type f-size + 10 k
Search for files larger than 10 KB
[Root @ www ~] # Find.-type f-size 10 k
Search for files equal to 10 KB
5.3 find parameter: file type-based search

Parameters related to the file type are as follows:

-Type TYPE: find a file whose TYPE is type.
The parameter list of TYPE includes:

F: common file;
L: symbolic connections;
D: Directory;
C: Character device;
B: block devices;
S: socket;
P: FIFO.
Example:

[Root @ www ~] # Find/var-type s
Search for All socket-type files in the/var directory.
5.4 find parameter: Directory-based Deep Search

Parameters related to directory depth are as follows:

-Maxdepth n: n indicates that the maximum downward depth is n;
-Mindepth n: n is a number, and all files whose depth is at least n subdirectories from the current directory are searched.
Example:

[Root @ www ~] # Find.-maxdepth 3-type f
The maximum downward depth is 3.
[Root @ www ~] # Find.-mindepth 2-type f
Search for all objects whose depth is at least two subdirectories from the current directory
5.5 find parameter: search by time

Time-related parameters include-atime,-ctime, and-mtime. The following uses-mtime description:

-Mtime n: n is a number that lists the file names that have been changed within one day before n days;
-Mtime + n: list the file names that have been changed before n days (excluding the nth day itself;
-Mtime-n: name of the file that has been changed within n days (excluding the nth day itself;
-Newer file: file is an existing file that lists new file names than file.
Example:

[Root @ www ~] # Find/etc-mtime 0
Find all the files in the/etc directory that have been modified from now until 24 hours ago.
[Root @ www ~] # Find/ect-newer/etc/passwd
Search for all files updated in the/etc/passwd directory.
5.6 find parameter: search by user or user group name

Parameters related to user or user group names are as follows:

-Uid n: n indicates the UID of the user;
-Gid n: n indicates the user's GID;
-User name: name indicates the user account name;
-Group name: name indicates the user group name;
-Nouser: searches for files whose owner does not exist in/etc/passwd;
-Nogroup: all user groups looking for files do not exist in/etc/group files.
Example:

[Root @ www ~] # Find/home-user root
Search for all the files in the/home directory of the root user.
5.7 find parameter: search based on file permissions

Parameters related to file permissions are as follows:

-Perm mode: find the file with the same permission as the mode file;
-Perm + mode: find the file with the "permission containing any mode" permission;
-Perm-mode: find the file with the "all must include the mode permission" permission.
Example:

[Root @ www ~] # Find/-perm + 7000
Search for all three files that must contain --- s -- t.
5.8 Other tips for the find Command

5.8.1 search for all files with zero length:

Find.-empty
5.8.2 search for files modified within n minutes:

Find.-cmin-60
Find the file whose status changes within one hour (that is, within 60 minutes ).
5.8.3 only search for non-hidden files (hidden files are not displayed ):

Find .\(! -Regex ".*/\..*"\)
Displays the files in the current directory and its subdirectories, and lists only non-hidden files.
VI. type: display the type of the specified command

The type command is used to display the type of a specified command and determine whether the command is an internal command or an external command.

The type command format is as follows:

Type [option] [commond]
The parameters of the type command include:

-T: Outputs "file", "alias", or "builtin", indicating that the given commands are "external commands", "Command alias", or "internal commands" respectively ";
-P: If the command is an external command, the absolute path is displayed;
-A: in the PATH specified by the environment variable "PATH", display the information of the given command, including the command alias.
The command type may be as follows:

Alias: alias;
Keyword: keyword, reserved Shell words;
Function: function, Shell function;
Builtin: built-in commands and Shell built-in commands;
File: file, disk file, external command;
Unfound: not found.
Example:

[Root @ www ~] # Type cd
Cd is a shell builtin
[Root @ www ~] # Type date
Date is/bin/date
[Root @ www ~] # Type mysql
Mysql is/usr/bin/mysql
[Root @ www ~] # Type nginx
-Bash: type: nginx: not found
[Root @ www ~] # Type if
If is a shell keyword

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.