find file search command and grep file content search command under linux

find file search command and grep file content search command under linux

  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. Search 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 #The file httpd.conf in the /etc directory
    (3) find /etc -name '*srm*' #Use wildcards *(0 or any number). Indicates to find files with 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. Find according to file characteristics     

    (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 in the system
    (4) find / -group cat # Find files that belong to group cat in the system
    (5) find / -mmin -5 # Find the last 5 minutes in the system Modified files (modify time)
    (6) find / -mtime -1 #Find files that have been 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 files larger than 10000000 bytes (c: bytes, w: double words, k: KB, M: MB, G: GB)
    (9) find / -size -1000k # Find files smaller than 1000KB

    3. Use Hybrid Find to Find Files

    The 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 where the user is fred or george in the / directory
         (3) find /tmp ! -user panda #Find all files in the /tmp directory that do not belong 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: 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. Examples  

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

  (6) grep -w pattern files : only match the whole word , not part (such as matching 'magic' instead of 'magical'),

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326261175&siteId=291194637