Linux Comm Grep
[ Linux Command Manual](#)
The Linux `grep` (global regular expression) command is used to search for strings or regular expressions that match a pattern within files.
The `grep` command searches for files containing a specified pattern. If a file's content matches the specified pattern, `grep` will, by default, display the line containing that pattern. If no file names are specified, or if the file name given is `-`, `grep` reads data from standard input.
### Syntax
grep pattern or grep [files or directories...]
* pattern - Represents the string or regular expression to search for.
* files - Represents the file names to search. Multiple files can be searched simultaneously. If the `files` parameter is omitted, data is read from standard input by default.
**Common Options:**:
* `-i`: Ignore case distinctions in both the pattern and the input files.
* `-v`: Invert match. Select non-matching lines.
* `-n`: Prefix each line of output with the 1-based line number within its input file.
* `-r`, `--recursive`: Read all files under each directory, recursively.
* `-l`, `--files-with-matches`: Suppress normal output; instead print the name of each input file from which output would normally have been printed.
* `-c`, `--count`: Suppress normal output; instead print a count of matching lines for each input file.
**More Parameter Descriptions**:
* **-a or --text**: Process a binary file as if it were text.
* **-A or --after-context=**: Print lines of trailing context after matching lines.
* **-b or --byte-offset**: Print the 0-based byte offset within the input file before each line of output.
* **-B or --before-context=**: Print lines of leading context before matching lines.
* **-c or --count**: Print only a count of matching lines per input file.
* **-C or --context= or -**: Print lines of output context.
* **-d or --directories=**: If an input file is a directory, use to process it. By default, is `read`, which means that directories are read just as if they were files. If is `skip`, directories are silently skipped. If is `recurse`, `grep` reads all files under each directory, recursively.
* **-e or --regexp=**: Use as the pattern. This is useful to protect patterns beginning with `-`.
* **-E or --extended-regexp**: Interpret as an extended regular expression.
* **-f or --file=**: Obtain patterns from , one per line.
* **-F or --fixed-regexp**: Interpret as a list of fixed strings, separated by newlines, any of which is to be matched.
* **-G or --basic-regexp**: Interpret as a basic regular expression.
* **-h or --no-filename**: Suppress the prefixing of filenames on output when multiple files are searched.
* **-H or --with-filename**: Print the filename for each match.
* **-i or --ignore-case**: Ignore case distinctions in both the pattern and the input files.
* **-l or --files-with-matches**: Suppress normal output; instead print the name of each input file from which output would normally have been printed.
* **-L or --files-without-match**: Suppress normal output; instead print the name of each input file from which no output would normally have been printed.
* **-n or --line-number**: Prefix each line of output with the 1-based line number within its input file.
* **-o or --only-matching**: Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.
* **-q or --quiet or --silent**: Quiet; do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected.
* **-r or --recursive**: Read all files under each directory, recursively.
* **-s or --no-messages**: Suppress error messages about nonexistent or unreadable files.
* **-v or --invert-match**: Select non-matching lines.
* **-V or --version**: Print version information and exit.
* **-w or --word-regexp**: Select only those lines containing matches that form whole words.
* **-x --line-regexp**: Select only those matches that exactly match the whole line.
* **-y**: Synonym for `-i`.
[ Linux Command Manual](#)
### Examples
1. Search for the string "hello" in the file `file.txt` and print the matching lines:
grep hello file.txt
2. Recursively search all files in the directory `dir` for lines matching the regular expression "pattern", and print the filenames and line numbers of the matches:
grep -r -n pattern dir/
3. Search for the string "world" in standard input and only print the count of matching lines:
echo "hello world" | grep -c world
4. In the current directory, find files with the suffix `file` that contain the string "test", and print the lines containing that string. You can use the following command:
grep test *file
The result is as follows:
$ grep test test* #Search for files with the prefix "test" containing the string "test" testfile1:This a Linux testfile! #List lines in testfile1 containing the string "test" testfile_2:This is a linux testfile! #List lines in testfile_2 containing the string "test" testfile_2:Linux test #List lines in testfile_2 containing the string "test"
5. Search for files containing a specific string recursively. For example, to find all files in the directory `/etc/acpi` and its subdirectories (if any) that contain the string "update", and print the content of the lines containing that string, use the following command:
grep -r update /etc/acpi
The output is as follows:
$ grep -r update /etc/acpi #Recursively search "etc/acpi" #for files containing "update" /etc/acpi/ac.d/85-anacron.sh:# (Things like the slocate updatedb cause a lot of IO.) Rather than /etc/acpi/resume.d/85-anacron.sh:# (Things like the slocate updatedb cause a lot of IO.) Rather than /etc/acpi/events/thinkpad-cmos:action=/usr/sbin/thinkpad-keys--update
6. Invert match. The previous examples searched for and printed lines that matched the condition. Using the `-v` parameter allows you to print the content of lines that do *not* match the condition.
To find lines that do *not* contain "test" in files whose names contain "test", use the following command:
grep -v test *test*
The result is as follows:
$ grep-v test* #Find lines that do not contain "test" in files whose names contain "test" testfile1:helLinux! testfile1:Linis a free Unix-type operating system. testfile1:Lin testfile_1:HELLO LINUX! testfile_1:LINUX IS A FREE UNIX-TYPE OPTERATING SYSTEM. testfile_1:THIS IS A LINUX TESTFILE! testfile_2:HELLO LINUX! testfile_2:Linux is a free unix-type opterating system.
YouTip