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.
- 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.
-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
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
^ (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
$ (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
. (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
[ ] (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
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.
Leave a Reply