Home >Operation and Maintenance >Linux Operation and Maintenance >Introduction to the difference between find and grep and common commands under Linux

Introduction to the difference between find and grep and common commands under Linux

零下一度
零下一度Original
2017-07-16 11:45:541747browse

find

Function: Search for files in the directory structure and perform the specified operation. This command provides quite a lot of search conditions and is very powerful.
Syntax: find search location matching file name
Description: The find command starts from the specified starting directory, recursively searches its subdirectories, finds files that meet the search conditions and takes relevant operations.

grep

The full name of grep is Global Regular Expression Print.
A powerful text search tool that can use regular expressions to search text and print out matching lines.
Syntax: grep matches StringFile name

When using Linux, you often need to search for files. The main search commands include find and grep. There is a difference between the two commands.

Difference:

                                                                                                                                   (1) The find command searches based on the attributes of the file, such as the file Name, file size, owner, group to which it belongs, whether it is empty, access time, modification time, etc.

(2) GREP is searched according to the content of the file. Each line of the file will be matched according to the given mode (Patter).

  1.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 means searching the entire hard disk

                             iety through`` -name httpd.conf conf #The file httpd.conf

in the /etc directory   (3)find /etc -name '*srm*'   #Use the wildcard character * (0 or any number). Indicates that the file name contains string 'srm' in the /etc directory The file that is the string 'srm' Files (access time)

   (2)find/-atime -2  #Find the files accessed in the last 48 hours in the system

    (3)find/-empty  #Find the files accessed in the system in the last 48 hours Empty files or folders  (4)find / -group cat Files modified in the last 5 minutes in the system (modify time)

  (6)find / -mtime -1   #Find files modified in the last 24 hours in the system

  (7 )find / -user fred   #Find files belonging to the user fred in the system

  (8)find / -size +10000c  #Find files larger than 10000000 bytes (c: byte, w: double Words, k:KB, M:MB, G:GB)

   (9)find / -size -1000k  #Find files less than 1000KB  

  

 3. Use mixed search method to find files

The parameters are: ! ,-and(-a),-or(-o).

   (1)find /tmp -size +10000c -and -mtime +2   #Find files in the /tmp directory that are larger than 10000 bytes and modified within 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 #Find all files that are not in the /tmp directory Files belonging to the panda user ​

2. grep command

​ Basic format: find expression


1. Main parameters

 [options]Main parameters:

 -c: Only output the count of matching lines.
 -i: Case-insensitive
 -h: File names are not displayed when querying multiple files.
 -l: When querying multiple files, only the file names containing matching characters will be output.
  -n: Display matching lines and line numbers.
 -s: Do not display error messages that do not exist or have no matching text.
 -v: Display all lines that do not contain matching text.

PatternRegular expression Main parameters:

\: Ignore the original meaning of the special characters in the regular expression.
  ^: Matches the starting line of the regular expression.
  $: Matches the end line of the regular expression.
 \<: Start from the line matching the regular expression.
 \>: To the end of the line matching the regular expression.
 [ ]: A single character, such as [A] means A meets the requirements.
 [-]: Range, such as [A-Z], that is, A, B, C to Z all meet the requirements.
  .: All single characters.
  *: There are characters, and the length can be 0.

  2. Example

 (1)grep 'test' d* #Display all lines containing test in files starting with d

 ( 2) grep 'test' aa bb cc   #Display the lines containing test in the aa, bb, cc files

  (3) grep '[a-z]\{5\}' aa    #Display all lines containing each line Lines whose strings have at least 5 consecutive lowercase characters

 (4)grep magic /usr/src #Display the lines containing magic in the files in the /usr/src directory (excluding subdirectories)

 (5)grep -r magic /usr/src  #Display the lines containing magic in the files in the /usr/src directory (including subdirectories)

  (6)grep -w pattern files: Only match the entire word, not part of the string (such as matching 'magic', not 'magical'),

The above is the detailed content of Introduction to the difference between find and grep and common commands under Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn