For a system administrator, working with text files is a common phenomenon. Maybe need to find a specific section from piles of log files for troubleshooting something? Or, need to find the document that contains essential information quickly?

In the case of Linux, there are numerous methods to find texts in files. It’s possible using both built-in tools and 3rd-party apps. Check out how to find texts in files in Linux.

Finding text in files

Depending on the number of files you have to perform a search on, there are two ways to perform the text search: automated or manual. If you have to work with a couple of text files, the manual search is more suitable. However, if there are hundreds of text files, then the automated search is the most efficient.

For automated search, we’ll be using grep. Grep comes pre-installed on any Linux distro. As for manual search, any modern text editor will do the job.

Find text in files using grep

In Linux, grep is the default tool for searching texts. Its name is derived from the ed command g/re/p that stands for “globally search for a regular expression and print matching lines.” It’s available on any modern Linux distro.

Grep is a command-line tool. Its command structure is as follows.

$ grep <option> <regular_expression> <file_path>

As the name of grep suggests, the pattern to search for is described using a regular expression. The regular expression is a special type of string that describes a pattern to match, locate, and manage. To learn more about grep and regular expression, check out using grep and egrep with regular expression.

For demonstration purposes, grab a sample text file. In this example, download the GNU General Public License v3.0 text file.

Basic search

The fundamental way of using grep is to search for a basic string.

Take a look at the following grep command. It’ll search for the word “GNU” in the text file.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/image1-24.png" data-lazy- height="598" src="data:image/svg xml,” width=”1294″>

To show the line number, use the “-n” flag.

$ grep -n “GNU” gpl-3.0.txt

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/image3-22.png" data-lazy- height="598" src="data:image/svg xml,” width=”1294″>

To perform a case-insensitive search using grep, use the “-i” flag.

$ grep -ni “gnu” gpl-3.0.txt

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/image2-27.png" data-lazy- height="571" src="data:image/svg xml,” width=”1295″>

You may not want to see the search matches but only the file name where the match happened in some situations. To print only the filename, use the “-l” flag. Here, the asterisk denotes to use all the text files in the current directory.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/image5-19.png" data-lazy- height="140" src="data:image/svg xml,” width=”1293″>

We can also pipe the output of other commands to grep.

$ cat gpl-3.0.txt | grep -n “GNU”

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/image4-23.png" data-lazy- height="599" src="data:image/svg xml,” width=”1293″>

Regular expression

Regex offers a smart way of fine-tuning the search. It has its own rules. However, different applications and programming languages implement regular expression differently. Here are a couple of examples that you can use with grep.

To define that the string is to be found at starting a line, use the caret (^) symbol.

$ grep -n “^GNU” gpl-3.0.txt

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/image7-14.png" data-lazy- height="161" src="data:image/svg xml,” width=”1295″>

To define that the string is to be found at the end of a line, use the dollar sign ($).

$ grep -n “to$” gpl-3.0.txt

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/image6-16.png" data-lazy- height="492" src="data:image/svg xml,” width=”1293″>

To describe that there can be any character at a certain location of the pattern, use the period character (.). For example, the expression “G.U” is valid if there’s any character between “G” and “U”.

$ grep -n “G.U” gpl-3.0.txt

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/image9-10.png" data-lazy- height="597" src="data:image/svg xml,” width=”1294″>

To describe that there can be a subset of characters at a particular location of the pattern, use the brackets ([]). For example, the expression “t[wo]o” tells that the match is valid for “two” and “too” only.

$ grep -n “t[wo]o” gpl-3.0.txt

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/image8-11.png" data-lazy- height="418" src="data:image/svg xml,” width=”1294″>

Extended regular expression

As the name suggests, an extended regular expression can do more complex things than basic regular expressions. To use extended regular expression with grep, you have to use the “-E” flag.

$ grep -nE <extended_regex> <file>

To search for two different strings, use the OR operators (|).

$ grep -nE “GNU|General|License” gpl-3.0.txt

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/image12-7.png" data-lazy- height="544" src="data:image/svg xml,” width=”1295″>

Finding text in files

Now comes the main part. Instead of manually telling grep the file to perform the search on, grep can do it automatically. In the following command, grep will use all the available text files in the current directory for searching the pattern.

If you want to grep to perform the search on a different directory, then you have to specify the location.

$ grep <regex> <directory_path>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/image10-10.png" data-lazy- height="527" src="data:image/svg xml,” width=”1295″>

If there are folders, grep doesn’t explore them by default. To tell grep to search recursively, use the “-R” flag.

$ grep -nR <regex> <directory_path>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/image11-10.png" data-lazy- height="451" src="data:image/svg xml,” width=”1296″>

Grep GUI

If you prefer to work with GUI but still want to enjoy grep’s features, then check out searchmonkey. It’s a front-end solution for grep. The package is available on almost all the major Linux distros.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/image13-7.png" data-lazy- height="559" src="data:image/svg xml,” width=”1293″>

Find text in files using nano

GNU Nano is a simple and powerful text editor that comes with any Linux distro. It has built-in features to search for text in a text file.

Note that in this method, you have to open the text file, and search manually. It’s doable if there’s only a handful of text files to work with. If there’s more, then using grep is the most optimal choice.

Open the text file in nano.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/image14-7.png" data-lazy- height="627" src="data:image/svg xml,” width=”1294″>

To search for a string match, press “Ctrl W”. After typing the string to search for, press “Enter”.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/image15-8.png" data-lazy- height="248" src="data:image/svg xml,” width=”1294″>

Find text in files using Vim

Vim is a well-known and reputed text editor. It’s the command-line equivalent of a modern text editor. Vim comes with numerous advanced features like plugins, macros, auto-completion, filters, etc.

Similar to nano, Vim works with a single file at a time. If you have multiple text files, then using grep is the most optimal way to go.

To search in a text file, first, open it in Vim.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/image16-7.png" data-lazy- height="623" src="data:image/svg xml,” width=”1294″>

Enter the following Vim command and hit “Enter”.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/image17-7.png" data-lazy- height="275" src="data:image/svg xml,” width=”1293″>

Find text in files using GNOME Text Editor

The GNOME Text Editor is the text editor that comes with the GNOME desktop. It’s a simplistic text editor with all the basic features you’d expect. It’s a nice alternative to the command-line text editors.

Similar to nano and vim, the same caution applies to this method. If the number of text files is large, then you better stick with grep.

Open the text file in Text Editor. Press “Ctrl F” to bring up the search bar.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/image18-5.png" data-lazy- height="429" src="data:image/svg xml,” width=”1292″>

Find text in files using VS Code

Visual Studio Code is a powerful text editor with tons of features. It’s optimized for programmers to be used as if it’s a full-fledged IDE. It’s available on almost all the major Linux distros.

Install the Visual Studio Code snap package.

$ sudo snap install code –classic

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/image19-5.png" data-lazy- height="161" src="data:image/svg xml,” width=”1292″>

Open the text file in VS Code. Press “Ctrl F” to start searching.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/image20-5.png" data-lazy- height="635" src="data:image/svg xml,” width=”1297″>

Final thoughts

There are numerous ways to search text in files. It’s an easy task to master. It’s strongly recommended to master the grep command because it offers the most value in terms of efficiency and ease-of-use.

If you prefer GUI, then there are numerous text editors to choose from. Any modern text editor will provide the text search option.

Happy computing!

About the author

<img alt="Sidratul Muntaha" data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/profile-photo-1-150×150.jpg5fe707c792d50.jpg" height="112" src="data:image/svg xml,” width=”112″>

Sidratul Muntaha

Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.