Linux: find related commands find and grep

find

grammar

Find files and directories under the specified directory.

find [path] [expression]

path: It is the path of the directory to be searched, which can be a directory or file name, or multiple paths, separated by spaces. If no path is specified, it defaults to the current directory.
expression: is an optional parameter used to specify the search criteria, which can be file name, file type, file size, etc.

The options for expression are [Note: The more commonly used ones are listed here, not all of them]:

  • -name pattern: Search by file name, wildcard characters * and ? are supported.
  • -type type: Search by file type, which can be f (ordinary file), d (directory), l (symbolic link), etc.
  • -size [±]size[cwbkMG]: Search by file size, support to use + or - to indicate larger or smaller than the specified size, the unit can be c (byte), w (number of words), b (number of blocks), k (KB ), M (MB), or G (GB).
  • -mtime days: Search by modification time, support to use + or - to indicate before or after the specified number of days, days is an integer indicating the number of days.
  • -user username: Find by file owner.
  • -group groupname: Search by the group to which the file belongs.

example

Find a file named file.txt in the current directory:

find . -name file.txt

List all files with the suffix .c in the current directory and its subdirectories:

find . -name "*.c"

Find files larger than 1MB in the /home directory:

find /home -size +1M

insert image description here

grep

grammar

Find strings or regular expressions that match the conditions in the file

grep [options] pattern [files]

pattern - Indicates the string or regular expression to look for.
files - Indicates the name of the file to be searched. Multiple files can be searched at the same time. If the files parameter is omitted, the data will be read from the standard input by default.

options Options:
-i: Match regardless of case.
-v: reverse lookup, only print non-matching lines.
-n: Display the line number of the matching line.
-r: Recursively find files in subdirectories.
-l: Print only matching filenames.
-c: Only print the number of matching lines.

example

1. Find the string "xxx" in the file index.js and print the matching lines:

grep xxx index.js

insert image description here
2. Recursively search for lines matching the regular expression "pattern" in all files in the folder dir, and print the file name and line number where the matching line is located:

grep -n -r aaa .
grep -n -r aaa ./*.js

insert image description here
3. Look for the string "world" in the standard input and print only the number of matching lines:

echo "hello world" | grep -c world

insert image description here

the difference

As can be seen from the screenshot above:
grep searches for text, and find searches for files.

Generally, it can be used in combination, for example:
start from the root directory to find all text files with the extension .log, and find out the lines containing "ERROR":

find / -type f -name "*.log" | xargs grep "ERROR"

Guess you like

Origin blog.csdn.net/weixin_43972437/article/details/130601662