Linux Find Files

Source: Internet
Author: User

Find directory-name "file name"
Find/-name "php.ini"

Locate file name
Locate PHP.ini


One: Locate command

The locate command is used to find a file that is faster than the search command, and it requires a database that is built by a daily routine work (crontab) program. Once we have established this database, we can easily search for the required files.

Run First: UpdateDB (in that directory can be placed in the crontab) after the SLOCATE.DB database generated under/var/lib/slocate/can be quickly found. Execute the #updatedb command directly at the command prompt:

Example: Find related words issue

$ locate Issue

/etc/issue

/etc/issue.net

/usr/man/man5/issue.5

/usr/man/man5/issue.net.5

Two: the Find command:

Function: Searches the directory structure for a file and performs the specified action. This command provides quite a few look-up conditions and is powerful.

Syntax: Find start directory looking for conditional actions

Description: The Find command starts with the specified starting directory, searches its subdirectories recursively, finds the file that satisfies the search criteria, and takes relevant action on it.

The search condition provided by this command can be a compound condition consisting of a logical operator not, and, or. The meaning of the logical operator and, or, is:

(1) and: Logic and, in the command with "-a", is the system default option, indicating that only when the given conditions are satisfied, the search condition is satisfied. For example:

$ Find–name ' tmp ' –xtype c-user ' inin '

This command looks for all the files that are satisfied with the three given criteria.

(2) or: logical OR, denoted by "-O" in the command. The operator indicates that the search condition is satisfied as long as one of the given conditions is satisfied. For example:

$ Find–name ' tmp ' –o–name ' mina* '

This command queries the file name ' tmp ' or all files that match ' mina* '.

(3) Not: Logical, in command "! Said The operator indicates that a file was found that did not meet the given criteria. For example:

$ find! –name ' tmp '

This command queries all files that have a file name that is not ' tmp '.

It is necessary to note that these options can be enclosed in parentheses when using a number of logical options. To avoid the shell itself being misunderstood, it is necessary to add the escape character "" to remove the meaning of the parentheses before the session number.

Example: $ find (–name ' tmp ' –xtype c-user ' inin ')

The search criteria have the following options:

First, the n values in each of the following options can be entered in three ways, assuming that N is 20:

+20 means after 20 (21,22,23 etc.)

-20 means before 20 (19,18,17, etc.)

20 means it's exactly 20.

1. Find by name and file attributes.

-the name ' string ' finds the file name that matches all the given string's files, and the wildcard character within the string * 、?、 [].

-lname ' string ' Find file name matches all symbolic link files of the given string, wildcard characters * 、?、 [] are available within the string.

-gid N finds all files belonging to the user group with ID number n.

-uid N finds all files belonging to a user with ID number n.

-group ' string ' finds all the files that belong to the user group named the given string.

The-user ' string ' finds all the files that belong to the user name of the given string.

-empty look for a directory or file of size 0.

The-path ' string ' lookup path name matches all the files given to the string, and the wildcard characters * 、?、 [] are available within the string.

-perm permissions find files and directories with the specified permissions, which can be represented as 711,644.

-size n[bckw] Finds files of the specified file size, the characters following n represent units, the default is B, which represents 512-byte blocks.

-type x finds a file of type X, and X is one of the following characters:

B-Block device files

C-Character device files

D catalog File

P Named Pipes (FIFO)

F Common Files

L Symbolic Link file (Symbolic links)

s socket file

-xtype x is basically the same as-type, but finds only symbolic link files.

2. Find time-based conditions

-Amin N finds all files that were accessed N minutes ago.

-Atime n finds all files that were accessed N days ago.

-Cmin n finds all files that have been modified for n minutes before the file status.

-CTime n finds all files that have been modified by the file status N days ago.

-Mmin n finds all files where the contents of the file have been modified before n minutes.

-Mtime n finds all files where the contents of the file have been modified before n days.

3. Actions that can be performed

-the EXEC command name {} Executes the given Linux command on a qualifying file without asking the user if the command needs to be executed. {} indicates that the argument to the command is the found file; the end of the command must end with ";".

-The OK command name {} performs the given Linux command on a qualifying file, unlike Exec, which asks the user if the command needs to be executed.

-LS lists all files found in detail.

-fprintf file name writes the found file name to the specified files.

-Print Displays the found file name on the standard output device.

-Please refer to the C language book for the format of the printf format.

Example 1: Finds all files in the current directory that begin with main and displays the contents of those files.

$ find. -Name ' main* '-exec more {};

Example 2: Delete A. Out or *.O file that has not been accessed in the current directory for a week.

$ find. (-Name A.out-o-name ' *.O ')

>-Atime +7-exec rm {};

The description is as follows:

The "." In the command Represents the current directory, where find starts with the current directory and finds the files in its subdirectories that meet the criteria specified later. (and) represents parentheses (), where "" is called an escape character. The reason for this is that for the shell, (and) there is a different meaning, rather than the use of the combination condition here. "-Name a.out" means to find a file named a.out; "-Name ' *.O '" means to find all files whose names end in. O. These two-name-O represents logic or (or), that is, find the file with the name A.out or the name ending with. O, and find the file under the current directory and its subdirectories, and then make a judgment to see if the last access time was 7 days ago (condition-atime +7). Execute the command RM (-exec rm{};) on the file. where {} represents the name of the file that is currently found to match the criteria, the syntax is required. The last of the first line in the preceding command is a continuation character. When the command is too long to write on a line, you can enter one, and then the system will display a, indicating that the user continues to enter the command.

Linux Find Files

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.