Five search commands for files in linux summary _unix Linux

Source: Internet
Author: User
Tags builtin file permissions

There are 5 search commands for Linux, respectively, as follows:

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 distinguish whether a command is brought by a shell or provided by a separate binary file outside the shell. If a command is an external command, then the-P argument displays the path to the command, which is equivalent to the which command. typecommand is not actually counted as a search command;

whereis: only search for program name, and only binary (parameter-B), man description file (parameter-m) and source code file (parameter-s);

locate: Equivalent find -name , can quickly find files;

find: The most common and powerful search command, which you can use to find any file you are looking for.

Attention:

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

Often are used first whereis or locate to check, really can't find, only use Find search.

Because with the use of the whereis locate database to find data, so pretty fast, and there is no actual query hard disk, compared to save time.

whereisand locate The data sought is found in the data that has been created /var/lib/ . But the update to the database defaults to updating every day (different systems may vary), so when you create a new file or delete it, look for the file, whereis and locate tell you that the file is not found, because the database must be updated.

The way to manually update the database is simple, and you can enter it directly updatedb . updatedbcommand to go back to read the configuration in/etc/updatedb.conf, and then go to the hard drive to find the file name operation, and finally update the entire database file.

The following is a detailed explanation of the above 5 commands:

One, which: Find system command Location

whichcommand, in the path specified by the path variable, searches for the location of a system command and returns the first search result.

The format of the which command is as follows:

which [-A] command

The parameters for the which command are as follows:

-A: Lists all the commands that can be found in the path directory, not just the first one found.

Example:

[Root@www ~] # which ifconfig
/sbin/ifconfig

Second, Whereis: program name Search

whereisCommands can only be used for search of program names, and only binary files (parameter-B), man description files (parameter-m), and source code files (parameter-s) are searched.

The format of the Whereis command is as follows:

Whereis [-BMSU] file or directory name

The parameters for the Whereis command are as follows:

-B Find only binary format files

-M finds only files under the manual path of the description file

-S only recruit source files

-U find other special files that are not in the above 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

Third, locate: Use the database to find files

The format of the Locate command is as follows:

Locate [-ir] keyword

The parameters for the Locate command are as follows:

-I: ignores case differences;

-R: You can follow the way the regular expression is implemented.

For example:

[Root@www ~] # locate passwd
/etc/passwd
/etc/passwd-
/etc/news/passwd.nntp
/etc/pam.d/passwd

Find: Finding Any File

The Find command has the following format:

Find [PATH] [option] [action]

4.1 Find parameter: Search based on file name

The parameters related to the file name are as follows:

-name FileName: Finds a file named filename. FileName can be represented using regular expressions.
Example:

[Root@www ~] # Find/-name passwd

Finds a file with a file name of passwd.

4.2 Find parameter: Search based on file size

The parameters associated with the file size are as follows:

-size size: Finds files that have a file size that is exactly equal to

-size-size: Find files with a file size greater than size;

-size +size: Finds files that have a file size smaller than.

The units of size are:

C--byte, Byte;

w--Word (2 bytes);

B--bit, block (512 bytes);

k--thousand bytes;

m--trillion bytes;

g--gigabytes.

Example:

[Root@www ~] # Find. -type f-size +10k
search for files larger than 10KB
[root@www ~] # Find.-type f-size 10k
Search files equal to 10KB

4.3 Find parameter: Search based on file type

The parameters related to the file type are as follows:

-type Type: A file that finds the type of the file.

The parameter list for type is:

F: Ordinary documents;

L: Symbolic Connection;

D: Directory;

C: Character equipment;

B: block equipment;

S: Sockets;

P:fifo.

Example:

[Root@www ~] # Find/var-type S

Find all files of the type socket under the/var directory.

4.4 Find parameter: Search based on directory depth

The parameters related to the directory depth are as follows:

The-maxdepth n:n is a number indicating that the downward maximum depth limit is n;

-mindepth N:n is a number that searches for all files with a depth distance of at least N subdirectories of the current directory.

Example:

[Root@www ~] # Find. -maxdepth 3-type f
down maximum depth limit of 3
[root@www ~] # Find.-mindepth 2-type F
search for all files with a depth distance of at least 2 subdirectories in the current directory

4.5 Find parameter: Search based on time

Time-related parameters are-atime,-ctime,-mtime. Here's a-mtime description:

-mtime N:n is a number that lists the file names that have been changed in "One Day" before n days;

-mtime +n: Lists the file names that were changed before n days (excluding the nth day itself);

-mtime-n: Lists the file names that have been changed within n days (excluding the nth day itself);

-newer File:file is an existing file that lists the file names that are newer than file.

Example:

[Root@www ~] # find/etc-mtime 0
Find all files that have changed in the/ETC directory will be listed from now until 24 hours ago.
[root@www ~] # find/ect-newer/etc/passwd
Find all files that are newer than/etc/passwd files in the/etc directory.

4.6 Find parameter: Search based on user or user group name

The parameters related to the user or user group name are as follows:

-uid N:n is a number that represents the UID of the user;

-gid N:n is a number that represents the user's GID;

-user name:name for user account name;

-group Name:name is the user group name;

-nouser: Find the file owner does not exist in the/etc/passwd file;

-nogroup: All user groups looking for files do not exist in/etc/group files.

Example:

[Root@www ~] # Find/home-user Root

Find all files in the/home directory for the root user.

4.7 Find parameter: Search based on file permissions

The parameters related to file permissions are as follows:

-perm mode: Find files with a file right equal to mode;

-perm +mode: Find File Permissions "contains the permissions of any one mode" file;

-perm-mode: Find file Permissions "must have all permissions to include mode".

Example:

[Root@www ~] # Find/-perm +7000

Find files that must contain all three permissions for the---s--s--t.

Some other tips for 4.8 find command

4.8.1 Find all files of length zero:

Find. -empty

4.8.2 find files that have been modified in n minutes:

Find. -cmin-60

Find files with file status changes within one hours (i.e. within 60 minutes).

4.8.3 only find files that are not hidden (hidden files are not shown):

Find. \ (!-regex ". */\.) *" \)

Displays the files under the current directory and its subdirectories, and lists only the files that are not hidden.

Type: Displays the types of the specified command

typecommand is used to display the type of the specified command, whether the instruction is an internal or an external instruction.

The type command is formatted as follows:

 Type [option] [Commond]

The parameters for the type command are:

-T: Output "file", "Alias" or "builtin", respectively, indicating that the given instruction is "external instruction", "command alias" or "internal instruction";

-P: If the instruction given is an external instruction, the absolute path is displayed;

-A: Displays information about the given instruction, including the command alias, in the path specified by the environment variable "path".

There are several possible types of commands:

Alias: aliases;

Keyword: keywords, shell reserved words;

Function: functions, shell functions;

Builtin: built-in command, Shell built-in command;

File: Files, disk files, external commands;

Unfound: Not found.

Example:

[Root@www ~] # Type cd
CD is a shell builtin
[root@www ~] # type Date 
is/bin/date 
[root@www ~] # Typ e mysql
mysql is/usr/bin/mysql
[root@www ~] # type Nginx
-bash:type:nginx:not found
[root@www ~] # Typ E If
if is a shell keyword

Summarize

The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.

Related Article

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.