Common query commands in Linux (which, Whereis, find, Locatae)

Source: Internet
Author: User

We often find a file in Linux, but do not know where to put it, you can use some of the following commands to search:

Which view the location of the executable file.

Whereis View the location of the file.

Locate to view the file location with the database.

Find actual search hard disk query file name

which

The purpose of the which command is to search for the location of a system command in the path specified by the path variable , and return the first search result. That is, with the which command, you can see whether a system command exists, and which commands are executed in which location.

1. Command format: which executable file name

2. Command function: The which directive searches for the location of a system command in the path specified by the path variable, and returns the first search result.

Example 1:

Displays the alias information when you look for an alias command

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s5.51cto.com/wyfs02/m00/82/0d/wkiom1djnciqceidaaamvsazs-e129.png "title=" Image1.png "alt=" Wkiom1djnciqceidaaamvsazs-e129.png "/>

For example: In the path specified by the environment variable, search for the location of the LS command

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s1.51cto.com/wyfs02/m02/82/0c/wkiol1djnthw809aaaan8zr-lke127.png "title=" Screenclip2.png "alt=" Wkiol1djnthw809aaaan8zr-lke127.png "/>

3. Command parameters:

-n Specifies the length of the file name, which must be greater than or equal to the longest file name in all files.

-P is the same as the-n parameter, but the path to the file is included here.

-W Specifies the width of the field at the output.

-V Display version information

Which-v

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/82/0D/wKiom1dJNeuTEm9gAAAb1lFwGq4407.png "title=" Screenclip3.png "alt=" wkiom1djneutem9gaaab1lfwgq4407.png "/> 650" this.width=650; "src="/e/u261/themes/default/ Images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px Solid #ddd; "alt=" Spacer.gif "/>


Whereis directive:

The Whereis command can only be used for program name searches, and only binary files (parameter-B), man description file (parameter-m), and source code file (parameter-s) are searched.

If the argument is omitted, all information is returned.

Whereis looks very fast when compared to find, because the Linux system records all the files in the system in a single database file, and when you use Whereis and the locate described below, the data is looked up from the database, not like the Find command. By traversing the hard drive to find, the efficiency will naturally be very high.

However, the database files are not updated in real time, and are updated once a week by default, so when we use Whereis and locate to find files, we sometimes find data that has been deleted, or just created the file, but cannot find it because the database file was not updated.

1. Command format:

Whereis [-BMSU] [BMS directory name-f] File name

2. Command function:

The Whereis command locates the location of the executable file, the source code file, and the Help file in the file system. The properties of these files should belong to the original code, binary files, or Help files. The Whereis program also has the ability to search for source code, specify alternate search paths, and search for unusual items.

3. Command parameters:

-B locates the executable file.

-M locate the help file.

-S Locate the source code file.

-U searches the default path for files other than executables, source code files, and Help files.

-b Specifies the path to the search executable file.

-m specifies the path to the search Help file.

-s Specifies the path of the search source code file.

4. Usage examples:

Whereis–b grep to locate the executable file.

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s2.51cto.com/wyfs02/m00/82/0c/wkiol1djnv2blivcaaakea5pd5s493.png "title=" Image4.png "alt=" Wkiol1djnv2blivcaaakea5pd5s493.png "/>

Whereis-m grep to locate the Help file.

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s1.51cto.com/wyfs02/m01/82/0d/wkiom1djnhtdlf8laaanloslf78392.png "title=" Image5.png "alt=" Wkiom1djnhtdlf8laaanloslf78392.png "/>

Whereis-s grep locates the source code file.

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s5.51cto.com/wyfs02/m00/82/0c/wkiol1djnxvj_2ytaaanloslf78913.png "title=" Image6.png "alt=" wKioL1dJNxvj_ 2ytaaanloslf78913.png "/>

Whereis-u grep searches the default path for files other than executables, source code files, Help files

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s3.51cto.com/wyfs02/m01/82/0c/wkiol1djnybzsoccaaajsnsdcvm905.png "title=" Image7.png "alt=" Wkiol1djnybzsoccaaajsnsdcvm905.png "/>


-b Specifies the path to the search executable file.

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s2.51cto.com/wyfs02/m01/82/0d/wkiom1djnjvzvp3eaaawq5-6lue135.png "title=" Image8.png "alt=" Wkiom1djnjvzvp3eaaawq5-6lue135.png "/>

-m specifies the path to the search Help file.

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s1.51cto.com/wyfs02/m02/82/0d/wkiom1djnkicnbueaaalh0_wgze472.png "title=" Image9.png "alt=" Wkiom1djnkicnbueaaalh0_wgze472.png "/>

-s Specifies the path of the search source code file.

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s4.51cto.com/wyfs02/m01/82/0c/wkiol1djn0ujsizeaaanr3d4pv0082.png "title=" Image10.png "alt=" Wkiol1djn0ujsizeaaanr3d4pv0082.png "/>


Find command:

The Linux Find command searches the directory structure for files and performs the specified actions. The Linux Find command provides quite a lot of search criteria and is powerful. Because find has powerful features, it has a lot of options, most of which are worth taking the time to look at. Even if the system contains a network file system (NFS), the Find command works equally well in the file system, and you only have the appropriate permissions. When running a very resource-intensive find command, many people tend to put it in the background because it can take a long time to traverse a large file system (this refers to a file system with more than 30G bytes).

1. Command format:

Find pathname -options [-print-exec-ok ...]

2. Command function:

Used to locate files in the file tree and make appropriate processing (possibly accessing the disk)

3. Command parameters:

The directory path that the Pathname:find command looks for. For example, use. To represent the current directory, and/to represent the system root directory

The-print:find command outputs the matched file to standard output.

The-exec:find command 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: The same as-exec, except that the shell command given by this parameter is executed in a more secure mode, and before each command is executed, a prompt is given to let the user determine whether to execute

Use cases:

Find the Test.cpp in the current directory and output

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/82/0C/wKioL1dJODSRJawlAAAM2K-DswA345.png "title=" Image1.png "alt=" Wkiol1djodsrjawlaaam2k-dswa345.png "/>

Find test.c in current directory and show permissions

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/82/0D/wKiom1dJN1LTU6u0AAATkUcp9fY642.png "title=" Image2.png "alt=" Wkiom1djn1ltu6u0aaatkucp9fy642.png "/>

Find test.c in current directory and show permissions

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/82/0C/wKioL1dJOFjAAl9rAAAawb_FAiA150.png "title=" Image3.png "alt=" Wkiol1djofjaal9raaaawb_faia150.png "/>

4. Command options:

-name find files by file name (exact case sensitive)

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/82/0D/wKiom1dJN2-js-aUAAAN4oC8HzE516.png "title=" Image4.png "alt=" Wkiom1djn2-js-auaaan4oc8hze516.png "/>

-iname find files by file name (case insensitive)

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/82/0C/wKioL1dJOHnBMER0AAATpM1YcBs476.png "title=" Image5.png "alt=" Wkiol1djohnbmer0aaatpm1ycbs476.png "/>

-perm follow file permissions to find files

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/82/0D/wKiom1dJN43xh-gVAAAlHn-7iLk367.png "title=" Image6.png "alt=" Wkiom1djn43xh-gvaaalhn-7ilk367.png "/>

-user find files according to their genus.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/82/0C/wKioL1dJOJOiF0lNAAAPEvsICb4656.png "title=" Image7.png "alt=" Wkiol1djojoif0lnaaapevsicb4656.png "/>

-group Find files according to the group to which the files belong

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/82/0D/wKiom1dJN6zQwnf7AAAM5hMvX3U877.png "title=" Image8.png "alt=" Wkiom1djn6zqwnf7aaam5hmvx3u877.png "/>

-mtime-n +n to find files by the time the file was changed

-n means that the file change time is less than n days from now

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/82/0C/wKioL1dJOLKxy6GBAAALOZHlrJA001.png "title=" Image9.png "alt=" Wkiol1djolkxy6gbaaalozhlrja001.png "/>

+ N means file change time is now N days ago

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/82/0C/wKioL1dJOMSDKCdZAAANuUBRjWU659.png "title=" Image10.png "alt=" Wkiol1djomsdkcdzaaanuubrjwu659.png "/>

The Find command also has the-atime and-ctime options, but they are all the same as the-m time option

-nogroup finds a file that does not have a valid owning group, that is, the group to which the file belongs does not exist in/etc/groups

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/82/0E/wKiom1dJN9jgAfzXAAAL_mvoCfo106.png "title=" Image11.png "alt=" Wkiom1djn9jgafzxaaal_mvocfo106.png "/>

-nouser finds a file without a valid owner, that is, the owner of the file does not exist in the/etc/passwd.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/82/0E/wKiom1dJN-OAhscGAAAI0NX3qHo612.png "title=" Image12.png "alt=" Wkiom1djn-oahscgaaai0nx3qho612.png "/>

-newer file1! File2 find changed time than file File1 new but older file file2 than file

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/82/0C/wKioL1dJOOqjUtIdAAAWvGhGUKI275.png "title=" Image13.png "alt=" Wkiol1djooqjutidaaawvghguki275.png "/>

-type find a file of a certain type, such as

B-Block device files

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/82/0E/wKiom1dJOAHS261IAAAGhHqrLbQ502.png "title=" Image14.png "alt=" Wkiom1djoahs261iaaaghhqrlbq502.png "/>

D-Catalog

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/82/0C/wKioL1dJOQbAioB3AAAayuNaP_U870.png "title=" Image15.png "alt=" Wkiol1djoqbaiob3aaaayunap_u870.png "/>

C-Character device files

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/82/0E/wKiom1dJOBnCg89vAAAGpd-CTn8522.png "title=" Image16.png "alt=" Wkiom1djobncg89vaaagpd-ctn8522.png "/>

P-Piping File

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/82/0C/wKioL1dJOR2BAiAaAAAGrEBlqHE021.png "title=" Image17.png "alt=" Wkiol1djor2baiaaaaagreblqhe021.png "/>

L-Symbolic Link file

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/82/0E/wKiom1dJODLiiTLtAAAe0vv-ZvY203.png "title=" Image18.png "alt=" Wkiom1djodliitltaaae0vv-zvy203.png "/>

F-Normal file

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/82/0C/wKioL1dJOTaiDO7FAAAbxX14YK0051.png "title=" Image19.png "alt=" Wkiol1djotaido7faaabxx14yk0051.png "/>

-size N:[c] finds files with a file length of n blocks, with C indicating the length of the file in bytes.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/82/0E/wKiom1dJOFCwUjm1AAAM1KZ8z6w538.png "title=" Image20.png "alt=" Wkiom1djofcwujm1aaam1kz8z6w538.png "/>


650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/82/0C/wKioL1dJOV3C-b21AAAHXNVfFFU658.png "title=" Image21.png "alt=" Wkiol1djov3c-b21aaahxnvfffu658.png "/>

-depth: When looking for a file, first find the file in the current directory, and then look in its subdirectories

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/82/0C/wKioL1dJOWrD_JO8AAAMwdt6NjI693.png "title=" Image22.png "alt=" Wkiol1djowrd_jo8aaamwdt6nji693.png "/>

-fstype: Find files located in a file system of a certain type, these file system types can usually be found in the configuration file/etc/fstab, which contains information about the file system in the system

-mount: Does not cross the file system mount point when locating files.

-follow: If the find command encounters a symbolic link file, it tracks to the file that the link points to

-cpio: Use the cpio command for matching files to back up these files to the tape device.

In addition, the following three differences:

-Build time to find files,-n means less than n days, +n refers to n days ago

Example:

1. In the current directory, look for files that change time before 5th and delete them:

Find. -type f-mtime +5-ok rm {} \;

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/82/0C/wKioL1dJOXnyc6otAAAjt5X8Kc4413.png "title=" Image23.png "alt=" Wkiol1djoxnyc6otaaajt5x8kc4413.png "/>

2. Linux using the Find command to find File size xx file method

For example: A file of 5556 bytes can be written like this

That is, to find files larger than 5555 bytes less than 5557 bytes, the above is the/root directory

Find/root-size-5557c-size +5555c-exec ls-ld {} \;

Find files that are less than 500K and larger than 50K

Find/root-size-500k-size +50k-exec ls-ld {} \;

Practice:

Find/directory directory with file name test

$ find/-name Test-print

Find and sort all current directories

Ascending

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/82/0E/wKiom1dJOJbSqyRoAAAeDo2gVks925.png "title=" Image24.png "alt=" Wkiom1djojbsqyroaaaedo2gvks925.png "/>

Descending

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/82/0E/wKiom1dJOKDzYntDAAAahsCWA3A180.png "title=" Image25.png "alt=" Wkiom1djokdzyntdaaaahscwa3a180.png "/>

Find directories in all directory names that end with ". Log"

]$ Find/-name ' *.log '-print


Find the directory you visited in the last 5 minutes of your system

Find files accessed in the system for the last 5 days

Find/-mtime-5

Find all files under the current directory with permissions of 775 and set to 777

Find. -perm 775-ok chmod 777 {} \;

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/82/0C/wKioL1dJOafR7XtjAABCxEaagyQ172.png "title=" Image26.png "alt=" Wkiol1djoafr7xtjaabcxeaagyq172.png "/>

Find files with a file size of 10k and delete

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/82/0E/wKiom1dJONniuVAXAAAJ0XqEeos314.png "title=" Image27.png "alt=" Wkiom1djonniuvaxaaaj0xqeeos314.png "/>


Locate directive:

Usage rights: All users

How to use: Locate [-Q] [-d] [--database=]

Locate [-R] [--regexp=]

Locate [-QV] [-O] [--output=]

Locate [-E] [-f] <[-l] [-c]

Locate [-VH] [--version] [--help]

Description

Locate allows users to quickly search the file system for specific files. The method is to set up a database that includes all the file names and paths within the system, and then simply query the database when looking for it, rather than actually going deep into the file system.

In the general distribution, the database is set up in the Contab to be executed automatically. General users in use as long as the # locate Your_file_name type on it.

Parameters:

-U

-U

To create a database,-u will start with the root directory, and-u can specify where to start.

-L

If it is 1. The security mode is started. In safe mode, the user does not see files that the permissions cannot see. This slows down because the locate has to get access to the files in the actual file system.

-F

To exclude specific file systems, for example, we have no reason to put the files in the proc file system in the database.

-Q

Quiet mode, no error messages are displayed

-N

Display up to n outputs

-R

Conditions for finding using regular expressions

-O

Specify the name of the data inventory

-D

Specify the path to the repository

-H

Show auxiliary messages

-V

Show more messages

-V

Displays the version message of the program

Example:

Locate Chdrv: Find all the files called Chdrv

Locate-n a.out: Look for all files called a.out, but show only 100

Locate-u: Building a Database


Common query commands in Linux (which, Whereis, find, Locatae)

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.