Linux Search File Command summary find, locate, Whereis, which, type__linux

Source: Internet
Author: User
Tags file permissions

We often need to look up a file in the system, so how can we accurately and efficiently determine the exact location of a file in the Linux system? I've summed up a few commands for finding files in a Linux system. 1. Find command

Find is the most common and powerful search command that can find files of any type.

The general format of the Find command is: Find < specified directory >< specified condition >< specified action, that is, find Pathname-options [-print-exec-ok]

Parameter explanation:

Pathname:pathname is the directory to search for and its subdirectories, by default, the current directory

Common option Options:

-name: Find files by file name

-user: Locating files According to the owner of the file

-group: Find a file by the group to which the file belongs

-perm: Find files by file permissions

-prune: Do not find in the current specified directory

For example: We know that there is a file named 1111 in the/directory,/etc,/user, and/user/bin directory, so let's take a look at the role of-prune

Find-name 1111-print to find a file named 1111 under the current directory and its subdirectories.

Find. -path./etc-prune-o-name 1111-print to find a file named 1111 under the current directory and its subdirectories (except for the/ETC directory and its subdirectories).

Find. -path./usr-prune-o-name 1111-print to find a file named 1111 under the current directory and its subdirectories (except the/USR directory and its subdirectories).

Find. \ (-path./usr/bin-o-path./etc \)-prune-o-name to find a file named 1111 in the current directory and its subdirectories (in addition to the 1111-print directory and its subdirectories;/usr/bin directory and its subdirectories). Because the command line does not recognize parentheses directly, there are spaces before and after \ (and \) to use the escape character \.

Note: The find command, without any arguments, indicates that the search path is the current directory and its subdirectories, and that the default action is-print, that is, not filtering any results, that is, outputting all the files.

-mtime-n +n: According to the file modification time to find the file,-n indicates that the file modification time is now n days, +n indicates the file modification time is now N days ago

-type: Find a type of file (b: block device file; d: catalog file; C: Character device file; p: Pipeline file; l: Link file; F: normal file)

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

-nouser to find a file that has no valid owner, that is, the owner of the file does not exist in/etc/passwd 2, locate command

The locate command is actually another way of writing "Find-name", but the search is different from find, which is much faster than finding. Because it does not search for a specific directory, it searches for the specified file in a database (/var/lib/locatedb). The secondary database contains all the information about the local file, which is created automatically by the Linux system, and the database is updated by the UpdateDB program, and UpdateDB is periodically established by Cron Daemon, which is updated daily by default. So using the Locate command you can't search for the latest updated files unless you manually update the database with the UpdateDB command before you use the Locate command to find the file.

Note : After each new file is updated and deleted, the file information saved in the database before updatedb will not change, that is, after a new file is added, the specified file cannot be searched with locate before updatedb. Similarly, when you delete a file that is already in the database, you can search for information about the file before updatedb with locate, although the file no longer exists. 3, Whereis command

The Whereis command can only be used to search binaries (-b), source code files (-s), description files (-m). All information is returned if the argument is omitted.

4, which command

The which command searches for the location of the specified system command in the path specified by the PATH variable. Use the Echo $PATH to display the value of the current path variable.

5. Type command

The type command is primarily used to distinguish whether a command is provided by a shell or an external, stand-alone binary file. If the shell is self-contained, it prompts the command for the shell Buildin, or it lists the location of the command. For example: The CD for the shell with the command, when looking with which, which will search according to the path variable settings, the result shows no CD in ... ; The CD is displayed as the shell Buildin command with the type CD. SSH is not a shell-with-command, and the path to SSH is displayed with type.

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.