Search for commands, find, and grep in linux to find the desired file

Source: Internet
Author: User
Find command syntax: find search path matching expression function: this command is used to find files that meet the conditions in a specified path. The search path can be multiple directories, different directories are separated by spaces (1) matching expression 1-namefilename: name of the file to be searched. You can use...

 

Find command

Syntax: find search path matching expression

Function: this command is used to find eligible files in a specified path. The search path can be multiple directories separated by spaces.

(1) matching expression 1

-Name filename: name of the file to be searched. You can use the wildcard "*", "?", However, the file name must be enclosed in double quotation marks.

Example: [root @ localhost tmp] # find-name "h *"-print

Translation: search for files starting with h in the current directory

Example: [root @ localhost tmp] # find/-name host-print

Translation: find the file named hosts in the entire file system

-User username: searches for files belonging to the username user.

Example: [root @ localhost tmp] # find/home-user user1-print

Find all files belonging to User user1 in/home

-Group groupname: searches for files belonging to the groupname group.

-Print: the file path name is displayed.

(2) matching expression 2

-Exec command {}: execute the listed commands on the matched files, without asking the user whether to execute these commands. the parameter {} is replaced by the path name of the current file found by find, the command line must end with "\;".

Example: [root @ localhost tmp] # find/home-user user1-exec cat {}\; | more

Find all the files belonging to User user1 in/home and display their contents

Example: [root @ localhost tmp] # find/home-user user1-exec rm-r {}\;

Find all the files belonging to User user1 in/home and delete them.

-OK command {}: The usage is the same as that of-exec. before executing the command, ask the user whether to execute the command.

(3) matching expression 3

-Atime n: find the files accessed in the previous n days (only on the nth day)

-Atime + n: Search for the files accessed before n days ago.-n indicates that the files accessed after n days ago

Example: [root @ localhost tmp] # find/home-atime + 365-print

Find the files that the user visited a year ago

Example: [root @ localhost tmp] # find $ home-user user1-atime + 3-exec rm-r {}\;

Find out the files that have been accessed and deleted in the user's user1 home directory three days before.

(4) matching expression 4

-Type filetype: specifies the file type to be searched.

Filetype can be B files, c-character device files, d directory files, and f General files.

Example: [root @ localhost tmp] # find-type d-print

Find all subdirectories in the current directory

Example: [root @ localhost tmp] # find-type f-print

Translation: find all common files in the current directory

(5) matching expression 5

-Size Number and-size Numberc: search by file size. Numberc indicates that the value is in bytes. Otherwise, the value is in block (generally 512 bytes. -Number (or-Numberc) indicates to search for files smaller than this value, and + Number (or-Numberc) indicates to search for files larger than this value.

Example: [root @ localhost tmp] # find-size-10-print

Translation: find all files in the current directory with a length less than 10

Example: [root @ localhost tmp] # find-size-10c-print | ls-l

Translation: searches for all files with a length less than 10 bytes in the current directory and displays the file information in a long format.

Example: [root @ localhsot tmp] # find-size + 100-size-200-exec ls-s {}\;

Translation: Search for 100 ~ in the current directory ~ 200 long files and display the actual number of files

Grep command

Syntax: grep [parameter] find mode file name [file name]

Function: The grep command is used to find the row matching the pattern in the specified file and display the matched row on the standard output. If no file is specified, it is read from the standard input. When you search for multiple files, the file name is added before the output of each row. Wildcard characters are allowed in the searched file name.

Parameters:

-C: only display the number of matched rows

-I: the matching time is case insensitive. by default, the matching time zone is case sensitive.

-H: when multiple files are searched, the file name is not displayed before the output row.

-N: add the row number of the matching string to the output (the row number of the first line of the file is 1)

-V: only display rows that do not contain matching strings.

-F filename: Gets the search mode from the specified file. each row contains one search mode item.

Example: [roo @ locallost user1] # grep printf *. c

Find the printf string in all C files in the current directory

Example: [roo @ locallost user1] # grep user1/ect/password

The system account file contains user1 lines.

If the search mode contains spaces, enclose the search mode in single quotes.

Example: [root @ localhso user1] # cat> dialog

Your name is:

Linux

[Root @ localhost user1] # grep 'Your Name' dialog

Your name is:

When there are multiple search modes, you can write these search modes into the file and use the-f parameter to read the search mode items from the file.

Example: [root @ localhost user1] # cat> mode.txt

Name

Lin *

[Root @ localhost user1] # grep-f mode.txt dialog

Your name is:

Linux

 

From the column of youngerhao

Related Article

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.