Linux Lookup Locate find

Source: Internet
Author: User

1, locate

Locate command and find find files similar function, but locate is through the update program to the hard disk all the files and directory data first set up an index database, in the execution of LOACTE directly to find the index, query faster, the index database is generally managed by the operating system, However, you can also directly release the update to force the system to immediately modify the index database.

Command parameters:

-E will be excluded from the scope of the search.

-1 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 must get access to the file in the actual file system.

-F excludes 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 message is displayed.

-N displays at most x outputs.

-R uses the normal arithmetic to do the searching condition.

-o Specifies the name of the data inventory.

-d Specifies the path of the repository

-H Display Auxiliary message

-V Displays the version message of the program

Usage examples:

Locate/etc/sh

2. Find

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, there are many options, and even if the system contains a network file system (NFS), the Find command works equally well in the file system with 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 find files in the file tree and to make appropriate processing

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 the parameter is executed in a more secure mode, prompting the user to determine whether to execute before executing each command.

4. Command options:

-name finds files by file name.

-perm to find files according to file permissions.

-prune Use this option to have the Find command not be found in the currently specified directory, and if you use the-depth option at the same time,-prune will be ignored by the Find command.

-user Search for files according to the owner of the file.

-group finds files according to the group to which the files belong.

-mtime-n +n The file changes time to find the file,-n means that the file change time is now less than n days, + n means that the file change time is now N days ago. The Find command also has the-atime and-ctime options, but they both and 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.

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

-newer file1! File2 look for a file that changes time than the file File1 new but older than the file file2.

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

B-block device files.

D-Directory.

C-character device file.

P-Pipeline file.

L-Symbolic link file.

F-Normal file.

-size N:[c] finds files with a file length of n blocks, with C indicating the length of the file in bytes. -depth: When looking for a file, first find the file in the current directory, and then look in its subdirectories.

-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 this 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:

-amin N Find the last n minutes of files accessed in the system

-atime N Find the last n*24 hour Access file in the system

-cmin n Find files in the last n minutes of the system changed file status

-ctime n Find files that have changed file status in the last n*24 hours of the system

-mmin n Find files that have changed file data in the last N minutes of the system

-mtime n Find files that have changed file data for the last n*24 hours in the system

Finds files that have been modified in a specified time find-atime-2

Finds find by keyword. -name "*.log" finds files that end with. Log in the current directory. ". " Represents the current directory

Find files by directory or file permissions find/opt/soft/test/-perm 777 Find a file with permission 777 in/opt/soft/test/directory

Finds find by type. -type f-name "*.log"

Find all current directories and sort find. -type D | Sort

Finds files by size find. -size +1000c-print find files larger than 1K in the current directory

exec of Find

Find is a very common Linux command, but what we generally find out is not just to look at it, there will be further operations, this time the role of exec is revealed.

The-exec parameter is followed by command, which terminates with a; for the end of the flag, so the semicolon behind this command is indispensable, considering that the semicolons in each system have different meanings, so precede the backslash.

When using find, just write the desired action in a file, you can use the exec to match find find, very convenient. In some operating systems, only the-EXEC option is allowed to execute commands such as L s or ls-l. Most users use this option to find old files and delete them. It is recommended that you take a look at the LS command before you actually execute the RM command to delete files, confirming that they are the files you want to delete. The EXEC option is followed by the command or script that you want to execute, followed by a pair of {}, a space and a \, and finally a semicolon. In order to use the EXEC option, you must use the Print option at the same time. If you verify the Find command, you will see that the command outputs only the relative path and file name from the current path.

Instance:

The ls-l command is placed in the-exec option of the Find command.   -type f-exec ls-l {} \; The find command matches all normal files in the current directory and lists them using the Ls-l command in the-exec option.

Find files in the directory that change time before n days and delete them find. -type f-mtime +14-exec rm {} \;

Before the shell can delete files in any way, you should check the corresponding files, be careful! You can use the Safe mode of the-EXEC option when using a command such as MV or RM. It will prompt you before you work on each file that is matched to it.

Find the files in the directory that changed the time before n days and delete them, giving a hint to find before deleting them. -name "*.log"-mtime +5-ok rm {} \;

-exec using the grep command find/etc-name "passwd*"-exec grep "root" {} \;

Find files to move to the specified directory find. -name "*.log"-exec mv {}..    \; Delete after CD: ll be able to find

Use the EXEC option to execute the CP command find. -name "*.log"-exec cp {} test3 \;

Linux Lookup Locate find

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.