Linux Common Search command

Source: Internet
Author: User
Tags file size numeric numeric value

1. Find


find is the most common and powerful search command you can use to locate any file you're looking for. The Find command makes it easy to locate the specific file you need in the use and management of Linux systems.

The use format of
find is as follows:


$ find < Specify directory > < specify conditions > < specify action >


-< Specify directory: the directory to search for and all of its subdirectories. The current directory is assumed by default. Can be a list of directories, separated by spaces between directories.


-< Specify conditions: The characteristics of the file to search for.


expression


Description


-name File


tell find what to look for; the file to be found is enclosed in quotation marks, and you can use the wildcard character (* and?). )


-perm Mode


matches files of all modes to a specified numeric pattern value. Not only read, write and execute, all patterns must match. If the minus sign (-) is in front of the pattern, it means that all patterns except this mode are used.


-type x


matches all files of type X. X is C (character special), B (Block special), D (directory), p (known pipe), L (symbolic Connection), s (socket file) or F (General file).


-links N


matches all files with a number of connections of N.


-user User Number


matches all user serial numbers that are previously specified in the user serial number, and can be a numeric value or a user login name.


-atime N


matches all files that have been accessed in the first n days.


-mtime N


matches all files that have been modified in the first n days.


-newer File


Matches all files that have been modified in time than the file file update.


-size N


matches all files of size n block (512 byte block, 1K byte block if K is after N).


-print


displays the entire file path and name. In general, use-print, if there is no this parameter, the Find command to do the required search is not displayed results.


-< Specifies that the search results are handled specifically.


If nothing is added, find defaults to search the current directory and its subdirectories, and does not filter any results (that is, all files are returned) and displays them all on the screen.


Find use Example:


$ find. -name ' my* '


searches the current directory (with subdirectories, below) for all files whose filenames start with my.


$ find. -name ' my* '-ls


searches the current directory for all files whose filenames start with my, and displays their details.


$ find. -type f-mmin-10


searches the current directory for all normal files that have been updated in the last 10 minutes. If you do not add the-type f parameter, search for normal files + special files + directories.


1 knows the file name of a file but does not know which directory it is in, at which point the file can be found by looking at the command as follows:


# Find/-name httpd.conf–print


2) Find
based on partial filename

when you are looking for a file, you do not know the full name of the file, only know that the file contains a few specific letters, the Search command is also available to find the appropriate file. In this case, the wildcard character "*", "" "when looking for a filename? ”。 For example, or find the file "httpd.conf", but only remember that the file name contains an "http" string, you can use the following command to find:


#find/–name *http*-print


3) According to the characteristics of the file to find


If you only know the size of a file, the modification date, and so on, you can use the Find command to locate the file. For example, knowing that a file size is less than 2500bytes, you can use the following command to find:


#find/etc–size-2500c–print


The following is the parameter of the Find command function with the time feature:


Amin N finds all files that have been accessed before n minutes.


atime N finds all files that have been accessed before n days.


cmin N finds all files where the file state was modified before n minutes.


CTime n finds all files where the file state was modified before n days.


mmin N finds all files where the contents of the file were modified before n minutes.


mtime N finds all files where the contents of the file were modified before n days.


2. Locate

The
Locate command is another way of writing "Find-name", but it is much faster than the latter because it does not search for a specific directory, but rather searches for a database (/var/lib/locatedb) that contains all the local file information. The Linux system automatically creates this database and updates it automatically every day, so you won't be using the locate command to find the most recently changed files. To avoid this, you can use the UpdateDB command to manually update the database before using locate.

Usages of the
locate command:


$ locate/etc/sh


search for all files in the ETC directory that start with SH.


$ locate ~/m


searches the user's home directory for all files beginning with M.


$ locate-i ~/m


searches the user's home directory for all files that start with M and ignores case.


3. Whereis


The Whereis command can only be used for the search of program names, and only binary files (parameter-B), man description file (parameter-m), and source code files (parameter-s) are searched. If the argument is omitted, all information is returned.

Usages of the
whereis command:


$ whereis grep


4. Which

The function of the
which command is to search the location of a system command in the path specified by the PATH variable and return the first search result. That is, by using the which command, you can see whether a system command exists, and which location is executing the command.

Usages of the
which command:


$ which grep


5. Type

The
type command is not really a lookup command, it is 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, equivalent to the which command.

Usages of the
type command:


$ type cd

The
system prompts that the CD is the Shell's own command (build-in).


$ type grep

The
system prompts that grep is an external command and displays the path to the command.


$ type-p grep


with the-p parameter is equivalent to the which command.

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.