Linux Find command, find, grep summary

Source: Internet
Author: User

Find command

Syntax: Find search path Match expression

Function: This command is used to find eligible files in the specified path, the search path can be multiple directories, and different directories are separated by spaces

(1) Matching expression 1

-name FileName: The name of the file to find. You can use the wildcard character "*", "?", but enclose the file name in double quotation marks

Example: root# find–name "h*" –print

Translation: Find files starting with h in the current directory

Example: root# find/–name Host-print

Translation: Find files named hosts throughout the file system

-user Username: Find files belonging to username users

Example: root# find/home–user user1–print

Translate: Find all files belonging to user User1 under/home

-group groupname: Finding files belonging to the GroupName group

-print: Show file path name found

(2) Matching expression 2

-exec Command {}: Executes the listed command against the found matching file, without asking the user whether to execute these commands, the parameter {} is found by the Find current text

The end of the command line must have "\;".

Example: root# find/home–user user1–exec Cat {}\;|more

Translate: Find all files belonging to user User1 under/home and display their contents

Example: root# find/home–user user1–exec rm–r {} \;

Translate: Find all files belonging to user User1 under/home and delete

-ok Command {}: Same as-exec, asking the user whether to execute the command before executing the command

(3) Matching expression 3

-atime N: Find files accessed in the first n days (only n days of the day)

-atime +n: Finds files that were accessed before n days; n indicates the first nth days after

Example: root# find/home–atime +365–print

Translate: Find out which files the user visited a year ago

Example: root# find $home –user user1–atime +3–exec rm–r {} \;

Translate: Find out which users have accessed files and delete them before the first 3 days of their User1 home directory

(4) Matching expression 4

-type filetype: Specifies the type of file to find

FileType can be: B-block files; C-character device files; D catalog files; F General documents

Example: root# find–type d–print

Translate: Find all subdirectories under the current directory

Example: root# find–type f–print

Translate: Find all the normal files in the current directory

(5) Matching expression 5

-size number and-size Numberc: Search by file size. Numberc is expressed in bytes, otherwise the block (typically 512 bytes) is the unit

。 -number (or-NUMBERC) represents a file that is smaller than this value, and +number (or-NUMBERC) is looking for a file that is larger than the value

Example: root# find–size-10–print

Translation: Find all files under the current directory with a length of less than 10 blocks

Example: root# find–size-10c–print | Ls–l

Translate: Find all files under the current directory with a length of less than 10 bytes and display file information in long format

Example: root# find–size +100–size-200–exec ls–s {} \;

Translate: Find 100~200 block long files in the current directory and show the actual blocks of the files

grep command

Syntax: grep "parameter" lookup mode filename "file name"

function: The grep command is used to find the rows in the specified file that match the pattern and to display the matching rows on the standard output. If no file is specified, read from standard input

Take. When you look in multiple files, the file name is preceded by the output of each line. Wildcard characters are allowed in the file name of the search.

Parameters:

-C: Show only the number of rows matching rows

-I: matching is case-insensitive and case-sensitive by default

-H: The file name is not displayed before the output line when looking in multiple files

-N: Add the line number of the row that matches the string before the output (the first line of the file is the line number 1)

-V: Displays only rows that do not contain a matching string.

-F filename: Gets the search pattern from the specified file, one search pattern entry per line

Example: root# grep printf*.c

Translate: Find printf strings in all C files in the current directory

Example: root# grep user1/ect/password

Translation: Displays the lines in the system account that contain User1 files.

When the search pattern contains spaces, enclose the search pattern in single quotation marks.

Example: root# cat > Dialog

Your name is:

Linux

<Ctrl+d>

root# grep ' Your name ' dialog

Your name is:

When there are multiple search modes, you can write these search patterns to the file and use the-f parameter to read the search pattern entries from the file.

Example: root# cat > Mode.txt

Name

lin*

<Ctrl+d>

root# grep–f Mode.txt Dialog

Your name is:

Linux

This article from "Tiger's Technical Homeland" blog, declined reprint!

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.