Grep && find command usage

  

Search for file content under Linux is commonly used grep, search file information using find
1, grep

Search text command
Command format: grep [options ]... pattern [file]...Command Format: grep Find Rule... Regular Expression View File 1.1 Find Rule
options
Explanation
-i Not Dimensible Write (only for single characters) -r traversal match -w whole word match -l When querying multiple files, only output the file name containing the matching characters -c only output the count of matching lines -n display matching lines and line numbers -s no Error message showing no or no matching text -h does not display file name when querying multiple files -v All lines are shown do not contain text matching regular expression 1.2

pattern regex main parameters:

  • \\: ignore regular expressions in the original meaning of special characters.
  • ^: Match the start line of the regular expression.
  • $: Matches the end line of a regular expression.
  • <: Start with a line that matches the regular expression.
  • >: End of the line that matches the regular expression.
  • [ ]: A single character, such as [A] that A meets the requirements.
  • [ - ]: Range, such as [A-Z], that is, A, B, C all the way to Z meet the requirements.
  • . : All single characters.
  • * : There are characters, the length can be 0.

    1.3 Instances

  • Ignore case search
    grep -i "androiD" Logcat.txt //from the logcat.txt file, search for text lines containing android, not case sensitive

  • traverse search, and no match information is displayed
    grep -rs "android" //From the current directory, iterate through all the files and search for the text line containing android

  • The whole word matches the search grep -w “android” logcat.txt //from the logcat.txt file In, search for the text line containing the word android grep -w “android |  ios & rdquo; logcat.txt //from logcat.txt file, search containing the word android or ios lines of text

  • list the file names
    grep -l " android "

  • Statistics occurrences
    grep -c "android" .

  • Display the line where the characters appear
    grep -n "android“ .



































    p>(1) If not, the default is the current path; (2) Support multiple paths, the directory is directly separated by spaces;
    find . -name demo2.2 Find rules
    2.2.1 According to the file name ( Name)

    -name //Search by file name, case sensitive -iname //Search by file name, case insensitive

    Wildcard Description: (1)* Matches any number Characters (2)? Match any single character (3)[] Match any character in parentheses
    find /data -name dalv I*find /data -name dalvik?cachefind /data -name dalvik-cach[abe]2.2.2 Depending on the file type (type)

  • f normal file
  • d directory file
  • l link file
  • b block device file
  • c character device file
  • p pipe file
  • s socket file

    Example:
    find -type f //View file type 2.2.3 Depending on the directory depth

  • -maxdepth n: Find the maximum depth is n
  • -mindepth m: Find the minimum depth is m

    2.2.4 According to file size (size)

    Unit: c (lowercase), k (lowercase), M (uppercase), G (uppercase)

    -size +10M: Find files larger than 10M -size -2k: Find files smaller than 2k -empty: Find files with size 0 or empty directories
    2.2.5 Based on files Permissions (perm)

    Example:
    find -perm 777 //Find files with permissions 777 2.2.6 Depending on the user and group to which the file belongs

  • -user: The main to find files
  • -group: Find files according to the group

    2.2.7 According to uid and gid

  • -uid 500: Find uid is 500 File
  • -gid 1000: Find files with gid of 1000

    2.2.8 According to time

    You can use the stat command to view the time of the file. The following are the files according to each. Time to find files:

  • -mtime -n +n: According to the change time, -n means less than n days, +n means n days ago
  • -atime -n +n: According to the access time, -n means less than n days, +n means n days ago
  • -ctime -n +n: According to the creation time, -n means n days Within, +n means n days ago
  • -mmin -n +n: According to the change (modify) time, -n means less than n minutes, +n means n minutes ago
  • -amin - n +n: According to the access time, -n means less than n minutes, +n means n minutes ago
  • -cmin -n +n: According to the creation time, -n means less than n minutes , +n means n minutes ago

    2.2.9 Multiple conditional connections

  • -a: Both conditions satisfy (and)
  • -o: two The condition satisfies one (or)
  • -not: inverts the condition (not)

    For example, look up the current path, start with a, and exclude the end with b Document or Folder:
    find -name a* -not -name *b2.3 Execute

  • -print Match file output to standard output, default action
  • -ls Find the result, display it as ls
    find -name app -ls

  • -ok [command] After the search is complete, execute the command and ask for execution
    find -name App -ok cat {} \\; //Note: There are spaces before and after {}

  • -exec [command] After the search is complete, execute the command and execute
    find -name app -exec Ls {} \\;


  • Copyright © Windows knowledge All Rights Reserved