While working with multiple files in a large system like Ubuntu, Administrators, and even Normal Users find it very challenging when searching for specific content inside a file. The usual way through GUI is by opening and accessing all the files one after the other, repeating the process until the desired content is found. This is an unreliable and time-consuming process. Thus, to make the process easier, Ubuntu offers the “grep” command which can be used in a bunch of different scenarios in the Terminal for a more efficient searching process.
This article explains the grep command and its use in different methods in Ubuntu 22.04 LTS.
What is the grep Command in Ubuntu/Linux?
The grep is a useful command for searching specific files for specific patterns or strings. The grep command accesses the files and searches for the provided query, and once the string or query is found, it returns the result in the Terminal. The grep command syntax is:
With the “options”, we can modify our grep search according to our needs. The Methods covering the use of the “grep” command in this article are:
- Searching a Single File
- Searching Multiple Files
- Searching for Multiple Words
- Searching Files with a Start Query
- Searching Files with an End Query
- Searching Files with a Start and End Query
- Searching for All Files in a Directory
- Searching Files Globally
- Searching Whole Words
- Searching Subdirectories
- Searching Files By Ignoring Case Sensitivity
- Searching Files By Excluding Set of Words
- Listing Names of Matched Files
- Listing Count Number of Matches
- Listing Line Number of Matches
- Listing Pattern Position of Matches
- Highlighting Matches with Color
- Listing Lines Containing Specific Characters
- Exporting Output into a File
A detailed explanation of each grep method is discussed below.
Installing the “grep” Command in Ubuntu
The “grep” Command in Ubuntu is pre-installed, but if it is unavailable on your Ubuntu System, use the command below to install it:
In our case, “grep” is already installed:
Searching a Single File With the “grep” Command
The grep command is usually used to search for a single file. Searching the File with a specified string to find a specific pattern through the Terminal is useful and more flexible for the Administrators as they do not have to manually open the file and then look for the string. The syntax of the “grep” command to search for a single file is:
In our case, the Documents Directory contains the “fileA.txt” text file and the file contains the following content:
To search for the string “LinuxWays”, the grep command will be:
The grep command will return the line containing the given string keyword, in our case, “LinuxWays”. The complete line returns as an output in the Terminal:
Similarly, to search for the line containing the string “Ubuntu”, the same grep command will be executed as follows:
As in our case, Ubuntu was written in two lines, both lines will be returned in the Terminal:
Using a similar method, you can search for a single string in a single file using the “grep” Command.
Searching Multiple Files With the “grep” Command
Apart from only searching a single file, multiple files can also be searched for a specific string using the grep command. Searching for Multiple files with the grep command involves the specified string to be searched for and all the files to be searched with a syntax:
In our case, we have two text files, “fileA.txt” and “fileB.txt” in our “Documents” Directory:
The grep Command can search both files for a specified string keyword. In our case, to search for the string “Ubuntu”, the grep command can be executed as follows:
The terminal displays the lines containing Ubuntu in both files. With the lines containing Ubuntu, the Terminal also indicates the file name containing that specific line:
Similarly, to search both the files “fileA.txt” and “fileB.txt” for the string “LTS”, the grep Command is
The Terminal will display the lines containing “LTS” in both files:
Thus, using a similar method, you can search for a desired string in Multiple Files using the “grep” Command.
Searching Files With Multiple Words Using the “grep” Command
The grep command can search Multiple Strings or queries at a time as well which is mostly beneficial for the system administrator. Instead of searching for one specific string and then searching for another string at a later time, both strings or queries can be searched simultaneously with the following syntax:
In our case, the text file “fileB.txt” has the following content in it:
To search and return only the lines having the words “articles” or “Linux”, the grep command with multiple words will be:
The Terminal will only display the lines either having the word “articles” or the word “Linux”. In our case, the Terminal will display the line numbers One, Two, and Four:
Similarly, to search for the words “Hello” and “Love” inside the text file “fileB.txt”, the grep command can be used:
The grep command will return the lines One and Four in Terminal from the Text File:
Using this approach, you can search for multiple words or matching patterns using the grep command inside various files.
Searching Files With a Start Query Using the “grep” Command
On some occasions, administrators require listing only the lines starting with a specific character or query. The grep command can be used to search for all the lines containing the specified character and return only the lines that match the search pattern, i.e. the provided starting character with the grep command:
In our case, to display the lines starting with the word “H” inside the Text File “fileB.txt”, the grep command will be:
This will display all the lines with the starting word “H”:
Similarly, to display the lines starting with the character “I” inside the Text File “fileB.txt”, the grep command will be:
In our case, the lines Three and Four will be displayed:
Using the above approach, multiple files can also be searched for querying the first character of a line inside the text files. In our case, to list the lines that start with the character “I” inside the Text Files “fileA.txt” and “fileB.txt”, the grep command will be:
The Terminal will display all the lines starting with “I” inside both the text files “fileA.txt” and “fileB.txt”:
This way the “grep” command can be utilized to search various files starting with a specified query or character.
Searching Files With an End Query Using the “grep” Command
Similar to the previous method, the end query or character can also be searched using the grep command. To do that, use the below-given syntax:
In our case, the Text File “fileB.txt” inside our Documents Directory contains the following lines:
Now, to display the lines ending with the character “n”, the grep command will be:
The lines ending with “n” will display in the Terminal:
Similarly, to search for the lines ending with the character or word “u” inside the text file “fileB.txt”, the grep command will be:
The line ending with “u” will be displayed in the Terminal:
Using the above approach, various files can be listed ending with a specified query.
Searching Files With a Start and an End Query Using the “grep” Command
Files can also be searched with a specific starting and ending query with the grep command. With this approach, only the lines that start and end with a specified starting and ending character or query will be searched inside the file and returned to the Terminal. The syntax to search for lines inside a file starting and ending with a specified query is:
In our case, using the text file “fileB.txt” in the previous example, we can search for lines starting with a specified query or character and ending with a specified query or character. The lines starting and ending with the specified characters will be displayed only. Using the “fileB.txt” file, to list the lines starting with the character or query “H” and ending with the character “!”, the grep command will be:
The First Line of the Text File “fileB.txt” will be displayed in the Terminal as it starts with “H” and ends with “!”:
Similarly, to list the line starting with “I” and ending with “u” inside the Text File “fileB.txt”, the grep command will be:
The grep Command will return the line Four from the “fileB.txt” file which starts with “I” and ends with “u”:
Using the above approach, various files can be searched for specific lines starting and ending with a specified query or character.
Searching All Files in the Directory with the “grep” Command
All the files inside a specific directory can also be searched at once with the grep command. Searching all files is useful when an administrator is dealing with multiple files. The grep command to search all the files inside a directory is:
In our case, the Documents Directory contains three text files named “fileA.txt”, “fileB.txt”, and “fileX.txt”:
Instead of using the grep to search for multiple files by typing all the file names, the asterisk “*” searches for all the files inside the Directory. The grep command in our case, to search for the string “LinuxWays” in all the files inside the Documents Directory will be:
The file names with the lines containing the string “LinuxWays” are displayed:
Similarly, to search for the string “like” inside all the files in our Documents Directory, the grep Command is:
In our case, only one file has the string “like”:
Searching Files Globally that match the Pattern with the “grep” Command
Files can be searched globally as well using the grep command. Searching for files globally can be beneficial if a file path is forgotten and the administrator now wants to search for a specific pattern inside the file. The grep command to search files for possible patterns or words match globally is
In our case, to search for the string “Ubuntu” globally, the grep command used will be:
All the files on the Ubuntu System containing the string “Ubuntu” will be returned:
Using the procedure above, various files can be searched for a pattern globally using the grep command.
Searching Whole Words Only in a File with the “grep” Command
When we search for a pattern or string inside a file, the grep command returns the sub-string as well, which usually might be irrelevant to our search. To avoid getting the sub-strings along with your results, the “-w” option is used with the grep command which ensures that the grep command should only search for Whole Words. The syntax of the grep command for searching whole words is:
Consider our case where the “fileX.txt” in the Documents Directory contains the following content inside
Notice the word UbuntuX on the first line of the “fileX.txt” text file. Here the Ubuntu exists as a sub-string to the string UbuntuX:
Using the usual grep command to list the string Ubuntu, the command used will be:
The Terminal also displays the sub-string “Ubuntu” of the string “UbuntuX”:
The complete String “UbuntuX” is displayed on the Terminal although we only searched for the string “Ubuntu”. Thus, the “-w” option is useful in returning only the whole word or string. In our case, to return only the Whole String Words, the grep Command used is
Now only the line containing the whole word “Ubuntu” is displayed instead of returning the “UbuntuX” too:
Similarly, you can search for a string pattern globally using the “-r” option along with the grep command. In our case, to search for the whole word string “Linux” globally the grep command is:
This will list all the lines containing the whole word “Linux” globally:
Using the similar method mentioned above, you can search for other whole words either in a single file or multiple files using the grep command.
Searching the Sub-Directories with the “grep” Command
Similar to the grep command with a global search option, to search for the string pattern in files inside subdirectories the grep command is used with the “-r” option and asterisk “*”:
In our case, to search for the string “LTS” inside the User Directory and sub-directories, the grep command used is
This displays all the string pattern “LTS” inside the files and sub-directories:
Searching Files by Ignoring Case Sensitivity with the “grep” Command
One of the problems that Ubuntu Users face while using the grep command is the case sensitivity of the string. The grep command is pre-designed to be case-sensitive and usually the Ubuntu User does not get the desired result if the string is incorrect in terms of capital or small alphabets. To avoid the issue of case sensitivity, the grep command is used with the “-i” flag which ignores all the case distinctions and the user can now search for a pattern or string without worrying about the capital or small alphabets. The grep command ignoring case sensitivity can be used as
In our case, using the usual grep command, to search for the string “Ubuntu” in the file “fileA.txt”, the grep command used was:
This returns the lines containing the word Ubuntu inside the text file “fileA.txt”:
Now if we change the search string from “Ubuntu” to “ubuntu”, we will get no output. The command line moves to the next line indicating no such file exists containing the word “ubuntu”:
Now, to ignore the case distinctions, the “-i” option with the grep command is used. Considering our previous example, to search for the word “ubuntu” in the file “fileA.txt”, the grep command used is
This will display the lines containing the word “Ubuntu” indicating the grep search ignored the case sensitivity:
Similarly, you can ignore the case distinction when searching for multiple files using the grep command. In our case, we will search for “ubuntu” among all the files in the Documents Directory by ignoring the case distinctions:
All the file names with the lines containing “ubuntu” or “Ubuntu”, in our case “Ubuntu”, are displayed. “Ubuntu” as a sub-string is also displayed:
Similarly, using the above procedure, you can use the “-i” flag to ignore case distinctions and search for various words in files.
Searching Files by Excluding a Set of Words with the “grep” Command
The usual grep command with string and file name displays the occurrence of the string in the file by showing the line. The Terminal shows the string that grep searches for and the complete line. The method can also be inverted where the Terminal will display the lines that do not include the given string with the grep command. To invert the search and only display the lines excluding the given string, the “-v” option is used with the grep command:
In our case, the “fileA.txt” when searched for the string “LTS” will display the following lines using the usual grep command:
Now to exclude the lines containing the string “LTS” in the “fileA.txt” file, the grep command used will be:
The lines inside the text file “fileA.txt” that do not include “LTS” are displayed:
Similarly, you can use the inverted option to search for words without case distinctions in multiple files with the command:
In our case, the command to display all the lines that exclude the word “LinuxWays” is:
The “-vi” will ignore the case distinction and will invert the search showing only the lines that do not contain the “LinuxWays” or “linuxways” words:
Thus, using the above process, you invert your grep search for various files.
Listing Names of Matched Files with the “grep” Command
Instead of returning the line-by-line matched patterns, the Ubuntu User can only display the files the match pattern is found in. It helps in identifying the matched files more easily and can be used as
In our case, to list all the files inside the Documents Directory that includes the word or string “LTS”, the grep command used is:
The file names will be displayed that include the word LTS in them:
Similarly, you can search for files that contain a specific pattern globally using the “-r” option. In our case, to search for files globally that include the word “Ubuntu”, the grep command is:
The “-i” option with the “-lr” will ignore case distinction while searching for the Ubuntu word:
The above method used in various other methods can get the files that include the specified word.
Listing the Count Number of Matches in a File with the “grep” Command
The grep command can also be useful in displaying the occurrences of the searched string in the Terminal. Usually, instead of displaying the whole matched line and then counting the searched string or query separately, the Terminal offers the use of the “-c” option which displays the number of occurrences of matched words in a file. To use the “-c” option with grep, the syntax will be
In our case, to find the number of occurrences of the word “Ubuntu” in the file “fileA.txt”, the grep command used will be:
The Terminal will display the numeric value of the occurrence of the specified word, in our case, Ubuntu:
Similarly, multiple files can also be searched for the occurrence of a specified word or string with the grep command:
In our case, to find the occurrence of the word “LTS” inside all the files in the Documents Directory, the grep command used will be:
This will display the numeric value of “LTS” occurrence in each file:
Thus, the above method can be used to find the occurrence of words in various different approaches.
Listing Matched Line Numbers in Files using the “grep” Command
The grep Command can also be used to display the line Number with matched words. The line that contains the specified word will have a “:” symbol before it whereas those lines that do not include the specified word will have the “–” symbol before it.
In our case, to list the matched line number of lines having the word “LinuxWays” in file “fileA.txt”, the grep command used will be:
The “-n” option will ensure showing the line number and the “-C” option is used for printing two lines before and after the match:
The lines that include the word “LinuxWays” have the “:” symbol at the start and the lines with the “–” symbol at the start indicate the word does not exist in that line. In our case, the “LinuxWays” word is in the line “2” and for that reason, the line “2” has the “:” symbol next to it:
Similarly, to list the Matched Line Numbers inside all the files containing the word “LTS” in the Documents Directory, the grep command used will be:
In our case, only Two lines include the word “LTS” and both line numbers have the symbol “:” next to them:
Using the method above, you can list matched line numbers of various files.
Listing Matched Pattern Position in Files with the “grep” Command
Ubuntu files usually consist of lengthy lines which the administrators and normal users may find challenging when looking for a specific string at some position. To make the search process more efficient, grep offers the use of “-o” and “-b” options which ensure printing only the matching offset of the queried string with grep:
In our case, the position of the matched pattern or word “Ubuntu” in the text file “fileB.txt” can also be found with the grep command:
To find the pattern position, the grep command will be:
The position of Ubuntu in the text file “fileB.txt” will be displayed in the Terminal:
Similarly, the positions of a word can also be found in multiple files. Considering our case where we have text files in our Documents Directory. To find the position of the word “Linux” inside all the text files in the Documents Directory, the grep command will be:
The Position of “Linux” inside all the files in the Directory will be displayed in the Terminal:
Thus using the same method, the position of matched patterns or words can be found in various files.
Using the “grep” Command to Export the Output into a File
Administrators are usually working with multiple files and while working with multiple files, they want all their results to be exported to another file for better accessibility. The grep command can export all the search results to another file as well with the command:
In our case, to export the result of the “Ubuntu” word search in all the files inside the Documents Directory, the command will be:
This will not show the result in the Terminal, instead, the result will move to the “output.txt” file:
To confirm the result, open the “output.txt” file with the command:
The Text Editor for the “output.txt” file will open and you can see the results are in the output file:
Thus, using the same method, you can export the result of multiple files into one file.
Highlighting the Matches with Color using the “grep” Command
The grep command can also highlight the specified string with the “–color” command. It helps in identifying the matched string or query more easily in the Terminal as one does not have to look for the specified string in the line again. To highlight the specified string in the file, the grep command used is
In our case, the Text File “fileB.txt” from our previous examples can be used to search and highlight a specified string. To search and highlight the string “Ubuntu” in the file “fileB.txt”, the grep command with color will be:
The lines containing the word “Ubuntu” will be highlighted in the terminal:
Similarly, words inside multiple files can also be searched and highlighted with the grep command. In our case, to search and highlight the word “Linux” from the Text Files “fileA.txt” and “fileB.txt”, the grep command will be
The grep Command will return the lines containing the string “Linux” and highlight them as well:
Multiple strings can also be highlighted with the grep command. In our case, to search and highlight the strings “Linux” and “Ubuntu” inside the “fileB.txt” file, the grep command will be:
Both the string “Ubuntu” and “Linux” will be highlighted in the Terminal:
Using the above method, various files can be searched and highlighted for various strings using the grep command.
Listing Lines Containing a Specified Set of Characters using the “grep” Command
Instead of highlighting the whole string, the user or administrator can also search for a set of characters using the grep command. To search for the specific set of characters inside a file, the grep command used is
In our case, using the “fileB.txt” from previous methods, a specific set of characters or words can be searched and highlighted using the grep command. To search and highlight “un” inside the file “fileB.txt”, the grep command will be
The highlighted sets of “un” as well as “u” and “n” individually inside the file “fileB.txt” will be highlighted on the Terminal:
Similarly, multiple files can also be searched for a specific set of characters. To search the characters “ed” inside the files “fileA.txt” and “fileB.txt”, the grep command will be
The Terminal will highlight all the specified characters, in our case “e” and “d”:
The above approach can be used in various files to search and highlight a specific character or set of characters.
Conclusion
The grep command is useful in searching for content inside a file. It can search a single file, multiple files, and global files, search based on queries, and can also list pattern positions, count the number of occurrences, and much more. This article explained all the methods used to search for patterns or strings inside a file using the grep command.