How to Recursively Search all Files for Strings on a Linux


The grep command is used to search text or scans the given record for lines containing a match to the given strings or words. Use grep to search for lines of text that match one or many regular expressions, and outputs only the matching lines. Using the grep command, we can recursively search all files for a string on a Linux.

Syntax of is shown as below-

$ grep -r "word"

For example, for searching “Linux” word in Downloads directory. The command should be like this

~/Downloads$ grep -r "Linux"

The sample output should be like this –

zookeeper_installation.htm:<li><p><b>Any of Linux OS</b> − Supports development and deployment. It is preferred for demo applications.</p></li>
Linux Howtos.html:1. How to add a New Disk Drive to a Linux System?
Linux Howtos.html:http://www.yolinux.com/TUTORIALS/LinuxTutorialAdditionalHardDrive.html
Linux Howtos.html:2. How to create a new virtual disk for an existing Linux virtual machine?
Linux Howtos.html:4. How to Increase the size of a Linux LVM by adding a new disk
Linux Howtos.html:5. How to format a Linux Hard Disk?
Linux Howtos.html:6. How to partition and format a new drive in Linux System?
Linux Howtos.html:7. How to mount NTFS Drives on a Linux System?

To ignore case distinctions, use the following command –

$ grep -ri "linux" .

The sample output should be like this –

zookeeper_installation.htm:<li><p><b>Any of Linux OS</b> − Supports development and deployment. It is preferred for demo applications.</p></li>
zookeeper_installation.htm:<p>The latest version (while writing this tutorial) is JDK 8u 60 and the file is “jdk-8u60-linuxx64.tar.gz”. Please download the file on your machine.</p>
zookeeper_installation.htm:$ tar -zxf jdk-8u60-linux-x64.gz

To display print only filenames with GNU grep, use the following command –

$grep -r -l "linux"

You can also specify directory name, by using the following command –

$ grep -r -l "linux" /path/to/dir/*.c

Congratulations! Now, you know “How to Recursively Search all files for a string on a Linux”. We’ll learn more about these types of commands in our next Linux post. Keep reading!

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 20-Jan-2020

11K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements