grep Command - Linux Commands Guide
Table of Contents
grep stands for Global Regular Expression Print. grep command is available in Unix/Linux-based operating systems. The full form of the tool suggests that it is used to search any text or expression in the given file(s).
It is useful when you are searching for a line in a file that contains a specific keyword. grep command can also use options to advance the search query.
The basic syntax of the grep command –
grep expression filename
Also Read
The basic syntax of the command requires at least two inputs. The first input is the expression or keyword that you want to search for. The second argument is the file to search.
For demonstration purposes, I have created four files in a directory called ‘Files’. Each file contains some lines of text.
I will start from the basic usage of the grep command and further advance the query using the tools options.
grep argument_1 file.txt
As you can see, the above command has grabbed the line from file.txt that contains the expression.’argument_1
‘. It’s simple. Right?
grep command – search from multiple files
Similarly, we can also search for any expression in multiple files by passing files names separated with space.
grep expression_4 file.txt file1.txt
grep command – Search all files in a directory
You can also search all the files stored in a directory at once. Here is how you can do it –
grep argument_4 *
grep – Search files in all subdirectories (Recursive search)
If the files are stored in multiple subdirectories, then it is possible to search all the files in the main directory and subdirectories at the same time using -r
option.
grep -r argument_3 *
Print only a number of lines from results
By default, grep will print all the lines that contain the given expression. For example, if I search for the word “Hello” in a file, there is a possibility that the word “Hello” is mentioned multiple times in that file. So the command will print all the lines that contain the word Hello.
This behavior can be modified by providing the -m
option to the command.
grep -m 1 argument_1 file.txt
Print bytes offset with the line
To print the bytes offset with the found line, pass the -b
option to the command.
grep -b argument_1 file.txt
Print line number with line
You can also print the line number before each line that it finds.
grep -n argument_1 file.txt
Hide filename from lines
If you search multiple files, by default, the tool also prints the filename before each line. To hide the line number, use -h
argument.
grep -h argument_1 file.txt file1.txt
Print only matching expression
By default, the tool prints the whole line with the expression. If you only want to print the expression, use -o
option.
grep -o argument_2 file.txt file1.txt
grep command – Search files match the specific pattern
To only search files that match a specific pattern, use the -R
option in the command.
grep -R --include=*.txt argument_3 *
Exclude files & directories match the specific pattern
To exclude files and directories from the search that match the specific pattern, use -R --exclude
option in the command.
grep -R --exclude=*.txt argument_3 *
grep command – Exclude directories match the specific pattern
To exclude directories from the search, use -R --exclude-dir
option in the command.
grep -R exclude-dir=Files argument_3 *
grep command – Print only filenames without a match
To print filenames that do match the expression, use the -L
option in the command.
grep -L argument_3 file.txt file1.txt file2.txt
Print only filenames with a match
To only print filenames that match the expression, use -l
option in the command.
Combine options
If you want to pass multiple options in a single query, this is how you can do that –
grep -nohR --include=*.txt argument_3
Conclusion
So there you have it. grep command is useful when writing bash scripts or searching for an expression in a directory structure with hundreds of files. Just practice the command and try to combine options to filter the results further as you wish.
If you want more help, please refer to the man pages or comment below this article.
LinuxAndUbuntu Newsletter
Join the newsletter to receive the latest updates in your inbox.