Find file search command and grep file content search command under linux (reproduced)

When using linux, file lookups are often required. The main search commands are find and grep. The two commands are differentiated.

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

               (2) grep searches according to the content of the file, and matches and searches each line of the file according to the given pattern (patter).

  1. find command

    Basic format: find path expression

    1. Find by file name

    (1) find / -name httpd.conf #Find the file httpd.conf in the root directory, which means to find the entire hard disk
    (2) find /etc -name httpd.conf #File httpd.conf in the /etc directory
    (3)find /etc -name '*srm*' #Use wildcards *(0 or any number). Indicates to find the file containing the string 'srm' in the file name in the /etc directory
    (4) find . -name 'srm*' # Indicates that the current directory is to find the file whose file name starts with the string 'srm'

    2. Follow the file Feature search     

    (1) find / -amin -10 # Find files accessed in the system in the last 10 minutes (access time)
    (2) find / -atime -2 # Find files accessed in the system in the last 48 hours
    (3) find / -empty # Find files or folders that are empty on the system
    (4)find / -group cat # Find files belonging to group cat in the system
    (5)find / -mmin -5 # Find files modified in the system in the last 5 minutes (modify time)
    (6)find / -mtime -1 #Find files modified in the system in the last 24 hours
    (7)find / -user fred #Find files belonging to the user fred in the system
    (8)find / -size +10000c #Find out more than 10000000 Byte files (c: bytes, w: double words, k: KB, M: MB, G: GB)
    (9) find / -size -1000k #Find out files less than 1000KB

    3. Use mixed search method to find The file

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

    (1)find /tmp -size +10000c -and -mtime +2 #Find 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 #Find all files that do not belong to the panda user in the /tmp directory


2.

     basic format of the grep command: find expression

     1. Main parameters

    [options] main parameters:
    -c: Print only the count of matching lines.
    -i: case insensitive
    -h: do not display file names when querying multiple files.
    -l: When querying multiple files, only output file names that contain matching characters.
    -n: Display matching lines and line numbers.
    -s: Do not display error messages for nonexistent or no matching text.
    -v: Display all lines that do not contain matching text.

    The main parameters of pattern regular expression:
    \: Ignore the original meaning of special characters in the regular expression.
    ^: Matches the starting line of the regular expression.
    $: Matches the end line of the regular expression.
    \<: Starts on a line matching the regular expression.
    \>: to end of line matching regular expression.
    [ ]: A single character, such as [A] that A meets the requirements.
    [ - ]: Range, such as [AZ], that is, A, B, C up to Z all meet the requirements.
    .: All single characters.
    * : There are characters, the length can be 0.

    2. Example  

  (1) grep 'test' d* #Show all lines that contain test in files starting with d
  (2) grep 'test' aa bb cc #Show lines containing test in aa, bb, cc files
  ( 3) grep '[az]\{5\}' aa #Show all lines that contain strings with at least 5 consecutive lowercase characters per line
  (4) grep magic /usr/src #Display the files in the /usr/src directory (without subdirectories) that contain magic lines
  (5)grep -r magic /usr/src #Display the files in the /usr/src directory (including subdirectories) line containing magic

  (6) grep -w pattern files : only match whole words, not parts of strings (e.g. match 'magic', not 'magical')

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326803838&siteId=291194637