Recursive grep command example in Linux

Hello guys, one of the most common tasks while working in programming projects is finding files containing some specific text like you have your application deployed in the Linux server, and you are migrating your database from one server to another. Now, you want to file all config files and scripts which are referencing your old database using the hostname or IP address, so that you can replace them with an alias. Well, you should always use an alias to connect to the database or any other system, but sometimes it happens you have to use a hostname. Anyway, how do you find all those files containing hostname in your Linux machine? Well, the grep command is here to help you.

The grep command allows you to scan files to find any matching text and you can use the recursive option of grep command to find all files containing a reference to your old database hostnames, or a specific text in general.

You can combine the recursive option with the ignore case option (-i) to find a specific text like hostname by ignoring the case in your config files and scripts.

I frequently use the recursive grep command to search all files in different sub-directories for finding a reference to a specific text  like any host or URL which is going to decommission and needs to be migrated:

$ grep -iR text *

The -R option will recursively search files in sub-directories starting from the current directory, and -i will search for the text you're provide ignoring case.

Btw, if you are not familiar with basic Linux commands like grep, find, chmod, etc. then I suggest you to first go through a basic course like Linux Command Line Basics on Udemy. This will help you to learn more in a short time.

The Recursive grep command to find all files containing specific text in Linux

By default will print both the matching text as well as the name of the file containing matching text, if you are just interested in file names then you can also use the grep command with option -l as shown below:

$ grep -iRl text *

Here is an example of the recursive search with grep command which prints both file and matching text and only file names:

How to find all files containing specific text in Linux? Use grep command

If you also want to search for the entire word, instead of just matching a part of the text, then you cause add option -w, which matches the whole word like

$grep -iRlw '-Xms' *

will search for exact "-Xms" text in all files, it is could to find the scripts where you are specifying your JVM arguments.

You can also use options like --exclude, --include, --exclude-dir, and --include-dir for more efficient and fast searching. For example, the following command will only search for text in Java files:

$ grep --include=\*.java -iRw text *

You can even use the regular expression to provide for a more sophisticated search like the following command will search for specified text on .c and .h files

$ grep --include=\*.{c,h} -iRw text *

This command will exclude searching all the files ending with .svn extension:

$ grep --exclude=*.svn -iRw '/path/to/file/' -e "pattern"

As I have said before, It's not just important to learn new commands but also new options of the command you are familiar with to make the most of Linux power. If you want to improve your Linux skills I suggest you go through Linux Command Line Interface (CLI) Fundamentals course on Pluralsight, which is quite comprehensive and practical.

Though, you would need a Pluralsight membership to get access to this course, which costs around $29 per month or $299 annually. Fortunately, they are providing a 33% discount now, and you can save almost $100 if you buy the membership on the current offer.

If you don't have Pluralsight membership, I encourage you to get one because it allows you to access their 5000+ online courses on all the latest topics like front-end and back-end development, machine learning, etc.

It's like Netflix for Software Developers, and Since learning is an essential part of our job, Pluralsight membership is a great way to stay ahead of your competition.

They also provide a 10-day free trial without any commitment, which is a great way to not just access this course for free but also to check the quality of courses before joining Pluralsight.

Important Points to Remember about grep command in Linux

1) The grep -r or -R option is used for recursive search

2) The grep -w is used for word search like it will match to the exact word

3) The grep -i is used for ignoring case search e..g it will match to "Java", "JAVA", "javA" and other variants.

4) The grep --include is used to search only for certain types of files.

5) The grep --exclude is used to exclude non-interesting files e.g., log files or .svn files.

That's all about how to search for matching text in all files in UNIX and Linux. This command is very powerful and one of the useful tools to find out files referencing direct hostnames instead of aliases. They are handy for host migrations and database migration. You can even use them while troubleshooting issues.

Further Learning
Linux Command Line Basics
Linux Command Line Interface (CLI) Fundamentals
Learn Linux in 5 Days and Level Up Your Career

Other UNIX command tutorials you may like to explore
  • 10 example of the networking command in Unix (example)
  • My favorite Linux courses for beginners (Linux online courses)
  • 10 Example of tar commands in Unix (example)
  • 6 Free Online course to learn Bash (free course)
  • 5 Example of kill commands in Unix and Linux (example)
  • Top 5 Bash Scripting course for Beginners (bash courses)
  • Top 10 Courses to Learn Linux commands (courses)
  • VI Editor examples and tips for beginners (example)
  • How to use the netstat command to find which process is listening on a port? (example)
  • How does nslookup command work in UNIX? (answer)
  • 10 examples of lsof command in Linux? (examples)
  • 10 books to learn essential commands of Linux or UNIX? (list)
  • How Linux works? What Every SuperUser should know? (book)
  • 10 Examples of cURL command in Linux? (examples)
  • How to use the CUT command in Linux (tutorial)
  • How to exit from the telnet command in UNIX? (example)
  • How to send an HTTP request to call a web service from the command line in Linux? (example)

Thanks for reading this article so far. If you like this article, then please share it with your friends and colleagues. If you have any questions or feedback, then please drop a comment.

P. S. - If you are looking for some free courses to learn or improve your Linux skills, you should check out this list of Free Linux Courses for Programmers and Developers.

1 comment:

  1. Be warned that §grep§ chokes on a long recursion, so you can't recurse into too big tree, which is avoided by using §find§ with syntax like this:

    find FOLDER -type f -name 'FILENAME_OR_BRACKET_EXPANSION' -exec grep TEXT '{}' ';'

    or with multiple filenames

    find FOLDER -type f '(' -name 'FILENAME_OR_BRACKET_EXPANSION' -o -name 'FILENAME_OR_BRACKET_EXPANSION' ')' -exec grep TEXT '{}' ';'


Feel free to comment, ask questions if you have any doubt.