Find file search commands in linux and grep file content search commands, grep Content Search

Source: Internet
Author: User

Find file search commands in linux and grep file content search commands, grep Content Search

When using linux, you often need to find files. The search Commands include find and grep. The two commands are partitioned.

  Differences(1) The find command is based onFile AttributesSearch, such as file name, file size, owner, group, whether it is empty, access time, modification time, etc.

(2) grep is based onFile ContentSearches for each row of the file according to the specified patter.

  I. find command

Basic Format:Find path expression

    1. Search by file name

(1) find/-name httpd. conf # find the file httpd. conf in the root directory, which indicates searching the entire hard disk
(2) find/etc-name httpd. conf # file httpd. conf In The/etc directory
(3) find/etc-name '* srm *' # Use wildcard * (0 or any number ). Searches for files with the 'srm 'character string in the/etc directory.
(4) find.-name 'srm * '# indicates the file whose name starts with 'srm' in the current directory.

    2. Search by file features

(1) find/-amin-10 # find the file accessed in the last 10 minutes in the system (access time)
(2) find/-atime-2 # find the file accessed in the last 48 hours in the system
(3) find/-empty # search for files or folders that are empty in the system
(4) find/-group cat # find the cat-group file in the system.
(5) find/-mmin-5 # find the file modified in the last 5 minutes in the system (modify time)
(6) find/-mtime-1 # search for files modified in the last 24 hours in the system
(7) find/-user fred # search for files belonging to the user fred IN THE SYSTEM
(8) find/-size + 10000c # find Files larger than 10000000 bytes (c: byte, w: Dual word, k: KB, M: MB, G: GB)
(9) find/-size-1000 k # find a file smaller than KB

3. Search for files using hybrid search

Parameters include :!, -And (-a),-or (-o ).

    (1) find/tmp-size + found C-and-mtime + 2 # search for files larger than 10000 bytes in the/tmp directory and modified in the last 2 minutes
(2) find/-user fred-or-user george # find Files in the/directory where the user is fred or george.
(3) find/tmp! -User panda # search for all files not belonging to the panda user in the/tmp directory

Ii. grep command

Basic Format:Find expression

1. Main Parameters

    [Options] main parameters:
-C: only counts matching rows are output.
-I: case insensitive
-H: When querying multiple files, the file name is not displayed.
-L: When querying multiple files, only names containing matching characters are output.
-N: displays matching rows and row numbers.
-S: the error message that does not exist or does not match the text is not displayed.
-V: displays all rows that do not contain matched text.

    Main Parameters of the regular expression pattern:
\: Ignore the original meaning of special characters in regular expressions.
^: Match the start line of the regular expression.
$: Matches the end row of the regular expression.
\ <: Starts from the row that matches the regular expression.
\>: Ends with the row that matches the regular expression.
[]: A single character. For example, [A] indicates that A meets the requirements.
[-]: Range, such as [A-Z], that is, A, B, C Until Z all meet the requirements.
.: All single characters.
*: It can contain 0 characters.

    2. Instance

(1) grep 'test' d * # display all lines containing test in files starting with d
(2) grep 'test' aa bb cc # lines that contain test in aa, bb, and cc files
(3) grep '[a-z] \ {5 \}' aa # displays all rows of strings containing at least five consecutive lowercase characters in each line.
(4) grep magic/usr/src # displayFile (excluding subdirectories)Rows containing magic
(5) grep-r magic/usr/src # displayFile (including subdirectories)Rows containing magic

(6) grep-w pattern files: match onlyEntire wordInstead of string 1.Part(For example, match 'Magic 'instead of 'magical '),

For more information, see: http://www.cnblogs.com/end/archive/2012/02/21/2360965.html

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.