Linux Find,grep Commands

Source: Internet
Author: User

Find using:

List all files and folders in the current directory and sub-directories

Find.

/homefind filenames ending in. txt under directory

" *.txt "

Ditto, but ignores case

" *.txt "

Find all files ending in. txt and. pdf under the current directory and subdirectories

" *.txt " " *.pdf "  "*.txt" "*.pdf "

Match file path or file

" *local* "

Matching file paths based on regular expressions

" . *\ (\.txt\|\.pdf\) $ "

Ditto, but ignores case

" . *\ (\.txt\|\.pdf\) $ "
Negative parameters

Find files that are not ending with. txt under/home

! -name "*.txt"
Search by File type
Type parameter

Type parameter list:

    • F Common Files
    • l Symbolic connection
    • Catalog D
    • C -character device
    • b -block equipment
    • s socket
    • P Fifo
Deep search based on directory

Down maximum depth limit is 3

-maxdepth 3 -type F

Search for all files with a depth distance of at least 2 subdirectories in the current directory

-mindepth 2 -type F
Search by file timestamp
Time stamp

The Unix/linux file system has three timestamps per file:

    • Access Time (-atime/days,-amin/minutes): The user's last access time.
    • modification Time (-mtime/days,-mmin/minutes): The last time the file was modified.
    • Change Time (-ctime/days,-cmin/minutes): File data elements (such as permissions, and so on) last modified time.

Search for all files that have been visited in the last seven days

-atime-7

Search all files that were visited exactly seven days ago

-atime 7

Search for all files that have been accessed for more than seven days

-atime +7

Search for all files with access time exceeding 10 minutes

-amin +10

Find all files that have been modified longer than file. log

-newer File.log
Match based on file size
File size Unit

File size unit:

    • B --block (512 bytes)
    • c --byte
    • W --word (2 bytes)
    • k --Kbytes
    • M --MBytes
    • G --gigabyte

Search for files larger than 10KB

-size +10k

Search for files less than 10KB

-size-10k

Search for files equal to 10KB

-size 10k
Delete a matching file

Delete all. txt files in the current directory

-delete
Match by file permissions/ownership

Search for files with permission 777 under current directory

-perm 777

Find php files with permissions not 644 in the current directory

! -perm 644

Find all files that the current directory user Tom owns

-user Tom

Find all files owned by the current directory user group sunk

-group sunk
Use -execOptions are used in conjunction with other commands

Locate all root files in the current directory and change ownership to user Tom

chown Tom {} \;

In the example above,{} is used in conjunction with the -exec option to match all files and then be replaced with the corresponding file name.

Find all the. txt files in your home directory and delete them

RM {} \;

In the example above,-ok and -exec behave the same, but it gives a hint as to whether to perform the appropriate action.

Find all. txt files in the current directory and stitch them together to write to the All.txt file

Cat {} \;> All.txt

Move the. log file from 30 days ago to the old directory

CP {} old \;

Find all. txt files in the current directory and print them as "file: File name"

Find. -type f-name "*.txt"-exec printf "File:%s\n" {} \;

Because multiple commands cannot be used in the-exec parameter in a single-line command, the following methods can be implemented to accept multiple commands after-exec

./text.sh {} \;
Search but jump out of the specified directory

Find all. txt files in the current directory or sub-directory, but Skip subdirectories SK

-prune -o-name "*.txt"-print
Find other tips for collecting

To list all files of zero length

-empty

1486451

Http://man.linuxde.net/find

Linux Find,grep Commands

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.