Using Grep to Search Inside Files for Text in Linux

Last Updated May 31, 2023 by . First Published in 2016.

Introduction to Linux Series
  1. Installing Linux Step by Step
  2. 8 Essential Linux Tips and Tools for Beginners
  3. Beginners guide to Reading and Finding Files in Linux
  4. Using Grep to Search Inside Files for Text in Linux
  5. Understanding Linux File Permissions and Permission Calculator
  6. How to Archive, Compress and Extract files in Linux
  7. Linux Piping and Redirection Explained
  8. Hardlinks and Softlinks in Linux Explained
  9. How to Create and Use Bash Scripts in Linux
  10. Basic Data Recovery in Linux - Recover your Data after Failures
  11. Apache Web Server Administration Cheat Sheet for Linux
  12. Essential MariaDB and MySql Administration Tips on Linux
  13. Complete Guide to Switching from Windows to Linux
Using Grep to Search Inside Files for Text in Linux

Using Grep is an essential tool in Linux. It is a tool for finding text within files and is an essential command to master when using Linux.

Grep is an essential Linux/Unix tool used for finding text within files. Grep is very simple to use, much misunderstood and very powerful. It is an essential command to master when using Unix and Linux. Grep is the Linux equivalent of Windows Find in Files. Grep can use regular expressions to search files or output for text, it can also use plain text searches.

To search for files containing a particular string, it is as easy as typing in

grep "findme"

This will list out the files and the text surrounding the match. You can add the -r flag to search recursively.

grep -r "findme"

You can also search the output of other commands, for example, a list of the currently installed packages. To see a list of the currently installed packages is a simple command, however, the output is very large and can be difficult to locate all the installed Apache packages.

sudo apt --installed list

To make things easier, we can pipe this output to the grep command which will then search and show only the packages with Apache in the name.

sudo apt --installed list | grep apache
apache2/now 2.4.7-1ubuntu4.13 amd64 [installed,upgradable to: 2.4.7-1ubuntu4.15]
apache2-bin/now 2.4.7-1ubuntu4.13 amd64 [installed,upgradable to: 2.4.7-1ubuntu4.15]
apache2-data/now 2.4.7-1ubuntu4.13 all [installed,upgradable to: 2.4.7-1ubuntu4.15]
apache2-mpm-prefork/now 2.4.7-1ubuntu4.13 amd64 [installed,upgradable to: 2.4.7-1ubuntu4.15]
apache2-utils/now 2.4.7-1ubuntu4.13 amd64 [installed,upgradable to: 2.4.7-1ubuntu4.15]
libapache2-mod-php5/trusty-updates,trusty-security,now 5.5.9+dfsg-1ubuntu4.21 amd64 [installed]
libapache2-mod-svn/trusty-updates,trusty-security,now 1.8.8-1ubuntu3.2 amd64 [installed]
libapache2-svn/trusty-updates,trusty-security,now 1.8.8-1ubuntu3.2 all [installed]

Using Grep with Regular Expressions

Using regular expressions with grep allows us to search for text beginning or ending with a string. These commands can work on files or the piped output of a command. In these examples, I'm just working on a file for ease of demonstration.

You have to use the -E flag for enhanced search, which allows the use of regex. This command will show files starting with fig.

grep -E ^fig /usr/share/dict/words

This will show files ending with ion

grep -E ion$ /usr/share/dict/words

This will show the lines where the word toon is a word, that is preceded and followed by a word boundary (spaces, punctuations, carriage returns etc.)

grep -E '*toon*' /usr/share/dict/words

This shows matches which start with po, contain any two characters, and ends in ute.

grep -E '^po..ute$' /usr/share/dict/words

And this shows all matches which contain any 5 of the specified letters in the brackets

grep -E '[aeiou]{5}' /usr/share/dict/words


If you enjoyed reading this article, or it helped you in some way, all I ask in return is you leave a comment below or share this page with your friends. Thank you.

There are no comments yet. Why not get the discussion started?

We respect your privacy, and will not make your email public. Hashed email address may be checked against Gravatar service to retrieve avatars. This site uses Akismet to reduce spam. Learn how your comment data is processed.