"One day one Linux" find usage detailed notes

Source: Internet
Author: User
Tags uppercase letter file permissions

The Find command is powerful and has many options for finding files, so this is a very useful and common Linux command. But he has a drawback is that the search time is relatively slow. The opposite is a locate command.

    1. Command format for Find

Find Pathname-option [-print] [-exec-ok command] {} \;

  

2. Command parameters:

The directory path that the Pathname:find command looks for. Both relative and absolute paths are available.

The-print:find command outputs the matched file to standard output (the default is to output the found file to standard output)

-exec Command {} \: Find commands to execute command commands given by this parameter to the matching file, note the space between ' {} ' and ' \ '.

-exec OK {} \: Similar to the EXEC command parameter, there is only one more check that requires the user to confirm execution before executing each command.

3. Options for the command:

Common:

-name filename: Finding a file named filename (Common options)

File properties:

-perm xxxx: Follow the XXXX Representative's execution permissions to find

-user Username: Search by file Owner

-group groupname: Search by file group

-nouser find a file without a valid owner, that is, the master does not exist in the/etc/passwd

-nogroup find a file without a valid genus, that is, the master does not exist in the/etc/groups

File Time Properties:

-mtime-n +n In accordance with the file change time to find,-n means n days or less +n refers to n days ago

-atime-n +n Follow the file access time to find

-ctime-n +n Follow the file creation time to find

-newer file1! File2 find change time than file1 new but older than file2 file

File Type properties:

-type finding a file of a certain type

B-Block Device directory

D-Catalog

C-Character device files

P-Piping File

L-Symbolic Link file

F-Normal file

Other types

-size n [C]: Find files with a file length of n blocks, with C indicating that the length of the file is calculated in bytes.

-depth: Find the file first, look for the current directory, and then find 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 this file system.

-mount: Does not cross the file system mount point when locating files. (This sentence is not too understanding ha, wait for the next example to see.) )

-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 disk device.

-prune: Ignoring a directory

  

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

4. Usage examples

(1)-name option

Compare common options, and you can use a match or regular expression to match the file (the difference between the match and the regular is important to note). If a specific file name can be enclosed in quotation marks, you can also use quotation marks, but the file name matching pattern must be enclosed in quotation marks.

    

" Test.txt " of the file Find /-name  "test.txt"  -print

#匹配符与正则表达式的应用:
Find all "*.log" in the Directory
# Find. -name "*.log" -print

Find files that start with an uppercase letter in the directory
# Find/-name "[a-z]*"-print

Find a file that begins with a lowercase letter and ends with a number
# Find/

  

(2) File properties options

-perm xxx

Find file permissions of 755 files, can and-find /-perm  755 -print

Find a file the owner is root, the group is also root files (this alone is meaningless, this is a combination of some restrictions)
# Find/-user Root-group Root-print

And of course there are files that have been deleted by both the owner and the group
# Find. -nouser-nogroup-print

(3) File Time Properties

# Find files accessed within the last 48 hours $    Find/-atime  -2# finds files that have been modified in the last 24  hours $Find /-mtime-1

(4) Ignore a directory

Use the option-prune to ignore the directory. When using the-prune option, be careful not to use with-depth,-prune will be ignored.

#  Ignore the Hello directory under the test directory  $find test  "test/hello"

Description: Find [-path] [expression]

After the path list is an expression

Find test  -path "Test/hello"-prune-o-print
-A and-O are short-circuit evaluation in the shell, with && | | The results are similar.
-path "Test/hello" is true, then the evaluation-prune, then the logic is true, the subsequent-print does not need to execute, otherwise the evaluation-print (using the short-circuit characteristics of-O)
The shell process can be understood as follows:
If-path "Test/hello" Then-pruneelse-print

Ignore operations for multiple directories:

Find  Test  \ (-path test/test4-o-path test/test3 \)  -prune-  o  

Add an escape "\"

Find a certain file,-name and other options after-O

Command:

Find " *.log " -print

(5) Combined use: Add command after Operation find Pathname-option [-print] [-exec-ok command] {} \;

#查看当前目录下的所有普通文件并通过ls-L to show details $ find. -type f-exec ls-l {} \; #还可以再加一些条件, combined using the Find logs  -type f-mtime +5-exec-ok rm-rf {} #删除五日以前有更改的文件  This command is in operation. Very useful, when deleting some log files.

* There is a good application scenario: Find finds files and then grep to see if there are any fields in the file that we need

Look for *.h in #在/tmp, and look for the "TEST" field in these files $ find/tmp-name *.h | Xargs-n50 grep test$ Find/   tmp   -name "*.h"   -exec grep "TEST"   {}   \;-print# copy the found file to the specified place $ find/tmp  -name test-exec cp ' {} '/test '; ' Special files can be used with Cpio's $ find Dir-name filename-print | CPIO-PDV Newdir  (temporarily not later cpio usage, first recorded) #可以使用管道来处理find查找出来的 file list $ find./-name "*.php" | ls-l-full-time 2>/dev/nul l | grep "2016-07-03 22:39" find files changed in 2016-07-03 22:39

  

-----

Reference Links:

A Linux command every day find

Examples of Linux find usages

"One day one Linux" find usage detailed notes

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.