Linux grep Command
The Linux grep command is used to search for a string of characters in a specified file.
The grep command searches files for content that matches a specified pattern. If it finds a match in a file, the default behavior of grep is to print the line containing the pattern. If no file names are specified, or if the file name is -
, grep reads from the standard input device.
Syntax
grep [-abcEFGhHilLnqrsvVwxy][-A<number-of-lines>][-B<number-of-columns>][-C<number-of-columns>][-d<action>][-e<pattern>][-f<pattern-file>][--help][pattern][file or directory...]
Parameters:
--a or --text: Do not ignore binary data.
--A<number-of-lines> or --after-context=<number-of-lines>: Display the line matching the pattern and the lines after it.
--b or --byte-offset: Display the byte offset of the line containing the match.
--B<number-of-lines> or --before-context=<number-of-lines>: Display the line matching the pattern and the lines before it.
--c or --count: Count the number of lines that match the pattern.
--C<number-of-lines> or --context=<number-of-lines> or -<number-of-lines>: Display the line matching the pattern and the lines before and after it.
--d <action> or --directories=<action>: Specify the action to take if a directory is specified instead of a file.
--e<pattern> or --regexp=<pattern>: Specify a pattern to search for in the file content.
--E or --extended-regexp: Use extended regular expressions for the pattern.
--f<rule-file> or --file=<rule-file>: Specify a file containing rules (one per line) to match against the file content.
--F or --fixed-regexp: Treat the pattern as a fixed string list.
--G or --basic-regexp: Use basic regular expressions for the pattern.
--h or --no-filename: Do not display the file name for matching lines.
--H or --with-filename: Display the file name for matching lines.
--i or --ignore-case: Ignore case distinctions.
--l or --file-with-matches: List the names of files containing matches.
--L or --files-without-match: List the names of files not containing matches.
--n or --line-number: Display the line number for matching lines.
--o or --only-matching: Display only the matched parts of the lines.
--q or --quiet or --silent: Do not output any information.
--r or --recursive: Equivalent to specifying "-d recurse".
--s or --no-messages: Do not display error messages.
--v or --invert-match: Display lines that do not match the pattern.
--V or --version: Display version information.
--w or --word-regexp: Display lines that match whole words.
--x --line-regexp: Display lines that match the entire line.
--y: Equivalent to specifying "-i".
Examples
- Search for the string "test" in files with a "file" suffix in the current directory and print the lines containing the string. Use the following command:
grep test *file
Output:
$ grep test test* # Search for files with the prefix "test" containing the string "test"
testfile1:This a Linux testfile! # Lines from testfile1 containing "test"
testfile_2:This is a linux testfile! # Lines from testfile_2 containing "test"
- List the lines in the file
testfile_2
that contain the character string "test" using the command:
grep test testfile_2
- Recursively search for files containing the string "update" in the specified directory
/etc/acpi
and its subdirectories (if any), and print the lines containing the string. The command used is:grep -r update /etc/acpi
The output is as follows:
$ grep -r update /etc/acpi # Recursively search for files in "etc/acpi" 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
- Inverse search. The previous examples searched for and printed lines that matched the condition. Using the
-v
parameter, you can print lines that do not match the condition.
To find lines in files containing "test" in their names that do not contain "test", use the command:
grep -v test *test*
The results are as follows:
$ grep -v test *test* # Find lines in files containing "test" in their names that do not 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.