Find command details

Source: Internet
Author: User

Find command details

Find is one of the best commands in linux, and its query function is very powerful. Most of the usage of find is described below:
Working Method: it traverses down the File hierarchy, matches the files that meet the conditions, and performs corresponding operations.

 
 
  1. 1. Print the file and directory list
  2. $ Find.-print # print the file and directory list

When-print is used, '\ n' serves as the delimiter used to separate files.
-Print0 indicates that '\ 0' is used as the delimiter to print each matching file name. This method is very effective when the file name contains '\ n.
However, after actual tests on centos, find directly displays the matched files without the-print option.
2. Search by file name or regular expression
Option-name specifies the string with the required skin for the file name.

 
 
  1. $ Find.-name "*. txt"-print # print the file ending with .txt
  2. $ Find.-iname "*. txt"-print #-iname indicates that the matching file name is case-insensitive.

3. condition restriction, matching one of multiple conditions
-A: Indicates and
-O: represents or
-Not: Indicates not

 
 
  1. $ Find. \ (-name "*. txt"-o-name "*. pdf" \)-print the files whose names end with .txtor Suffix in the front directory
  2. $ Find. \ (-name "*. txt"-a-name "a *" \)-print the files starting with "a" and ending with ".txt" in the front directory
  3. $ Find.-not \ (-name "*. txt"-a-name "a *" \)-print a file that does not start with a or does not end with .txt in the current directory
  4. $ Find.-not \ (-name "*. txt"-o-name "a *" \)-print outputs a file that does not start with a and does not end with a. txt

-Not is derived from Morgan's law. For more information, see http://zh.wikipedia.org/wiki/%E5%BE%B7%E6%91%A9%E6%A0%B9%E5% AE %9A%E5%BE%8B.

-Path: this parameter is used to match the file path as a whole.

 
 
  1. $ Find/home/user-path "* sh"-print
  2. /Home/user/bin/1.sh
  3. /Home/user/bash
  4. -Regex: this parameter is similar to-path, except that it matches the file path based on a regular expression.
  5. $ Find.-regex ". * \ (\. py \ | \. sh \) $"

4. Negative Parameters
You can also use "!" to find As a negative parameter.

 
 
  1. $ Find .! -Name ". txt"-print matching all files not ending with .txt

5. Directory-based Deep Search
The find command traverses all subdirectories during use. We can use some parameters to limit the depth of traversal.

 
 
  1. -Maxdepth: Maximum depth limit. 1 indicates that it is only in the current directory, and 2 indicates that it is down two levels ......
  2. $ Find.-maxdepth 1-type f-print # Only list all common files in the current directory
  3. -Mindepth: Minimum depth limit, similar to the above usage.
  4. $ Find.-mindepth 2-type f-print # list all files in at least two subdirectories from the current directory

6. Search by file type
File Types in linux
File type parameters
Common file f
Symbol file l
Directory d
Character Device c
Block Device B
Socket s
Fifo (pipeline file) p

 
 
  1. $find . -type f -print
  2. $find . -type d -print

7. Search by file time
Each file in a linux file system has three timestamps:
Access time: the time when the file was last accessed.
Ctime -- change time: the time when the inode of the file changes (what is inode ?) When you add or delete data to or from a file and modify the file owner, the file ctime changes when the link changes.
Modification time (mtime -- modification time): the time when the object content was last changed.
-Atime,-ctime,-mtime can be used as the time parameter of find. The Unit is day. These values also include + or-: +, indicating that the value is greater than, and-indicates that the value is less.

 
 
  1. $ Find.-type f-atime-7-print # print the files accessed within 7 days
  2. $ Find.-type f-atime 7-print # print the file that was accessed 7 days ago
  3. $ Find.-type f-atime + 7-print # print files not accessed for more than 7 days

The above time is based on "day" and "Minute.
-Amin
-Mmin
-Cmin
The usage is similar to the above. I will not give an example again.
Another nice feature of find is the-newer parameter. We can specify a reference file for comparing timestamps and then find all the files updated than the reference file.

 
 
  1. $find . -type f -newer file.txt -print

8. Search by file size

 
 
  1. -Size: similar in usage
  2. $ Find.-type f-size + 2 k # search for files larger than 2 k
  3. $ Find.-type f-size 2 k # search for files equal to 2 k
  4. $ Find.-type f-size-2 k # search for files smaller than 2 k

In addition to k, there are other file size units
B: block (512 bytes ).
C -- byte.
W -- word.
K-kilobytes.
M -- MB.
G -- Jilin byte.
9. Delete matching files

 
 
  1. -The delete parameter can implement this function.
  2. $ Find.-type f-name "*. swp"-delete

10. Matching Based on file permissions and ownership

 
 
  1. -Perm:
  2. $ Find.-type f-perm 644-print # print the file with the permission of 644
  3. -User: the user name or uid can be used later.
  4. $ Find.-type f-user root-print # print the Files Owned by the root user

11. execute commands or actions in combination with find

 
 
  1. -Exec: this parameter is one of the most powerful find parameters.
  2. $ Find.-type f-name "*. c"-exec cat {}\;> file.txt program concatenates all c Programs and writes them into a single file, file.txt.
  3. $ Find.-type f-mtime + 10-name "*. txt"-exec cp {}/tmp \; copy the txt file 10 days ago to/tmp
  4. Note: {} indicates the matched file.

12. Let find Skip a specific directory

 
 
  1. $find . \(-name ".git" -prune \) -o \( -type f -print \)

13. Use find in combination with xargs

 
 
  1. Xargs: converts a standard input to a command line parameter.
  2. $ Find.-type f-name "*. txt"-print0 | xargs-0 rm-f # Delete the matched file, and xargs-0 use \ 0 as the input delimiter.
  3. $ Find.-type f-name "*. c"-print0 | xargs-0wc-l # count the number of rows in all c program 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.