How to Find Specific Text using grep
on Linux. grep
is a short form for Global Regular Expression Print. It is one of the most useful tools in Linux to search for a specific string over files recursively or on a single file.
In this guide you are going to learn how to use the grep
command for various use cases on your Linux machine.
Use Cases
- Normal search on a file.
- Recursive search over directories.
- Excluding files.
- Including files.
- Regular Expression search.
- Using grep with other commands to filter output.
grep Command Syntax
grep [OPTIONS] STRING [FILE(S)]
OPTIONS are optional and include various methods to perform the search.
STRING is required which uses the particular string to search for.
FILE(S) You can add files or directory to perform the search or just the .
to search in the current directory.
By default grep is case sensitve. If you want to ignore case you can use the -i
option which ignores case sensitive and outputs the content which has lowercase and uppercase.
Normal Search Inside File
This is the most basic usage of grep command. Just use the command with the string you need to search for and the file name.
grep string filename
This command will search for the given text inside the provided file and output the results.
Recursive Search Including Directories
To search through all the files that are inside the directory you need to use the -r
option to perform a recursive search within the given folder.
The -r
option don’t follow the symbolic links. If you wish to search over symbolic links you need to use the capital -R
option instead of small -r
option.
grep -r string folder-name #Include Symbolic link files grep -R string folder-name
Exclude Files while using grep
You can use the --exclude
option to exclude directories, specific files or files with specific extensions.
grep -r string --exclude=filename --exclude=*.png --exclude=folder/* folder-name
Include Files while using grep
You can use the --include
option to include only the specific directories, specific files or files with specific extensions.
grep -r string --include=filename --include=*.php --include=folder/* folder-name
Regular Expression Search
Use the ^
(caret) symbol to match expression at the start of a line. In the following example, the text string
will match only if it occurs at the very beginning of a line.
grep "^string" filename
Use the $
(dollar) symbol to match expression at the end of a line. In the following example, the text string
will match only if it occurs at the very end of a line.
grep "string$" filename
Use the .
(period) symbol to match any single character. For example, to match anything that begins with string1
then has two characters and ends with the text string2
, you could use the following pattern.
grep "string1..string2" filename
Use [ ]
(brackets) to match any single character enclosed in the brackets. For example, find the lines that contain boat
or “coat
, you could use the following pattern.
grep "[bc]oat" filename
To escape the special meaning of the next character, use the \
(backslash) symbol.
Use grep with Other Commands
You can also use grep to filter output which is provided using another command.
ps -ef | grep mysql | grep -v grep
This will filter out the output to show only the ones that are with mysql
.
Conclusion
Now you have learned how to perform a search using grep command with various methods on your Linux machine.
Thanks for your time. If you face any problem or any feedback, please leave a comment below.