Linux Learning Note four: File Search command for Linux

Source: Internet
Author: User

1. File Search Command which

Syntax: which [command name]

Example: $which ls lists the directory where the LS command is located

[email protected] ~]$ which lsalias ls= ' ls--color=auto '          /bin/ls

Another command: Whereis [name name], you can also list the directory where the command is located.

[Email protected] ~]$ Whereis Lsls:/bin/ls/usr/share/man/man1/ls.1.gz/usr/share/man/man1p/ls.1p.gz

Their difference is that which provides the alias information for the command, and Whereis provides the help information for the command.

As in the above "Alias ls = ' ls--color=auto '" is the alias information of the LS command, it means that we press the LS command equivalent to execute "LS--color=auto" command.

The files in the output section of the Whereis ls.1.gz and ls.1p.gz refer to the Help file for the LS command.

2. File Search command Find

Syntax: Find [Search Path] [options] [Search keywords]

Options

-name Search by file name (* matches any character, including 0 characters.) Match a single character)

Example:

Find/etc-name init  finds files with the name init in the/etc directory Find/etc-name init*  Find all files starting with init in the/etc directory find/etc-name init??? in/ The ETC directory looks for all the text that starts with Init and has 3 characters after Init [email protected] desktop]$ lshello.txt  linuxqq-v1.0.2-beta1.i386.rpm  screenshot.png  test13  test.txt[[email protected] desktop]$ find-name test\*    <!--Here The asterisk is to be shifted with a right slash, Otherwise there will be an error-->./test13./test13/testfile./test.txt

-size by file size (+ means greater than specified size,-indicates less than specified size, = equals specified size)

The unit is block,1 Block512byte, that is, 1kb=2block,1kb equals two blocks of data.

Example:

Find/-size +2048 looking for files larger than 100MB in the root find/-size-2048 find files under the root directory size less than 100MB Find/-size +2048 find files in the root directory that are exactly equal to 100MB (typically not used, Because it can't be exactly equal to 100MB)

-user based on file owner lookup

Example:

Find/home-user Chan search for the file owner Chan under/home

-Search by Time

There are a total of two groups of 6 options, namely:

Ctime/atime/mtime in days

Cmin/amin/mmin in minutes

c denotes change, which refers to the properties of the file (owner, group, permissions, etc.) that have been modified

A represents accesss, which means that it has been visited

M represents modify, indicating that the contents of the file have been modified

Where there is-within the representation, + means more than

Example:

Find. -mmin-120 finds files in the current directory that have been modified within 120 minutes. -mmin +60 files that have been modified for more than 60 minutes in the current directory (except for files that have been modified within 60 minutes) find. -mtime-2 find files that have been modified within two days find. -atime-2 find files that have been accessed within two days

-type by file type (f for binary file, L for soft link file, D for directory)

Example:

Find. -type F Find all binaries in directory

-inum Based on I node (i node is a unique identifier for files in Linux)

Example:

Find the I node of the Hello.txt file is 781951[[email protected] desktop]$ ls-i781951 hello.txt 782880                      screenshot.png  782291 test.txt782823 linuxqq-v1.0.2-beta1.i386.rpm  782587 test13//based on I-node lookup [[email protected] desktop]$ find.-inum 781951./hello.txt

Typically used to find files with files named special characters, because these special file names are not recognized for Linux (such as "a B" space with spaces)

3. Find Connector

The Find connector is needed when we want to use file size as a query condition.

-a logic with a connector,-o logic or connector,-exec connection executor,-ok connection executor (-ok and-exec are the difference between OK after finding the target will ask you to do the previous command set the action, and-exec will not be asked)

Example:

--"-exec connector" finds Hello.txt and displays it directly [[email protected] desktop]$ find. -name hello.txt-exec Cat {} \; First row, second row, third row, line 2nd, hello, I ' m in CentOS system to write a introduce file to you.--find Hello.txt, inquire Ask the user for a corresponding action, enter Y/n[[email protected] desktop]$ find. -name hello.txt-ok Cat {} \;< cat .... /hello.txt >? Y first line the second row the third row the 2nd line Hello, I ' m in the CentOS system to write a introduce file to you.

Note: the "{}" in the command line means that the object found is the function of a reference, followed by "\;" is fixed.

3. Document Search Instruction Locate

Syntax: Locate [search keywords]

Example:

[Email protected] desktop]$ Locate Hello.txt/home/chanshuyi/desktop/hello.txt

The locate command differs from the Find command in that the locate lookup is based on the system's file directory database, and find is directly to the hard drive.

Therefore, locate cannot be found for the file or directory that was just created. (Because the System file directory database has not been updated).

You can also use UpdateDB to manually update the system's file directory database. However, the locate command generally exists only on Linux systems and does not exist in other Unix-type systems.

Note: The UPDATEDB directive is only available to root users. You can use the SU command to switch to the root user.

4. Search the file for string matching lines and output grep

Syntax: grep [Specify string] [source file]

Example:

grep ftp/etc/services searches the services file for the FTP field and outputs the row

Next: Linux Learning note four: Linux help commands

Linux Learning Note four: File Search command for Linux

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.