- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to Find a Specific String or Word in Files and Directories in Linux
Many times we need to search for a particular string which may be present in multiple files. In this article we'll see which commands to use to find all the files that contains a particular string or Word.
Using grep
It is a powerful regular expression search tool. At a basic level , it will match an input string with the list of files that contain that string.Below is the syntax and the example.
grep 'string' directory-path/*.* #Example grep 'config' hadoop-2.6.5/etc/hadoop/*.*
Running the above code gives us the following result −
hadoop-2.6.5/etc/hadoop/capacity-scheduler.xml: <configuration> hadoop-2.6.5/etc/hadoop/core-site.xml:<configuration> hadoop-2.6.5/etc/hadoop/hadoop-policy.xml:<configuration> hadoop-2.6.5/etc/hadoop/hdfs-site.xml:<configuration> hadoop-2.6.5/etc/hadoop/httpfs-site.xml:<configuration> hadoop-2.6.5/etc/hadoop/kms-acls.xml:<configuration> hadoop-2.6.5/etc/hadoop/kms-site.xml:<configuration> hadoop-2.6.5/etc/hadoop/mapred-site.xml.template:<configuration> hadoop-2.6.5/etc/hadoop/ssl-client.xml.example:<configuration> hadoop-2.6.5/etc/hadoop/ssl-server.xml.example:<configuration> hadoop-2.6.5/etc/hadoop/yarn-site.xml:<configuration>
Using grep -r
In this case we mention the r switch, which allows for a recursive search along all the subdirectories of the path given.
grep -r 'string' directory-path/ $ grep -r 'done' hadoop-2.6.5/
Running the above code gives us the following result −
hadoop-2.6.5/sbin/slaves.sh:done hadoop-2.6.5/sbin/distribute-exclude.sh:done hadoop-2.6.5/sbin/yarn-daemon.sh:done hadoop-2.6.5/sbin/kms.sh:done hadoop-2.6.5/sbin/httpfs.sh:done hadoop-2.6.5/sbin/refresh-namenodes.sh: done hadoop-2.6.5/sbin/refresh-namenodes.sh: echo "Refresh of namenodes done." hadoop-2.6.5/sbin/mr-jobhistory-daemon.sh: done hadoop-2.6.5/sbin/hadoop-daemon.sh:done
Using egrep –r 'word1|word2'
We can also search for multiple words by using the egrep command with | character. In the below example we are searching for files containing either the word config or the word comma.
egrep -r 'word1|word2' directory-path/ #Example egrep -r 'config|comma' hadoop-2.6
Running the above code gives us the following result −
hadoop-2.6.5/etc/hadoop/capacity-scheduler.xml:<configuration> hadoop-2.6.5/etc/hadoop/capacity-scheduler.xml:</configuration> hadoop-2.6.5/etc/hadoop/core-site.xml:&tl;?xml-stylesheet type="text/xsl" href="configuration.xsl"?> hadoop-2.6.5/etc/hadoop/core-site.xml:<configuration> hadoop-2.6.5/etc/hadoop/core-site.xml:</configuration> hadoop-2.6.5/etc/hadoop/hadoop-policy.xml:<?xml-stylesheet type="text/xsl" href="configuration.xsl"?> hadoop-2.6.5/etc/hadoop/hadoop-policy.xml:<configuration> hadoop-2.6.5/etc/hadoop/hadoop-policy.xml: The ACL is a comma-separated list of user and group names. The user and hadoop-2.6.5/etc/hadoop/hadoop-policy.xml: The ACL is a comma-separated list of user and group names. The user and hadoop-2.6.5/etc/hadoop/hadoop-policy.xml: The ACL is a comma-separated list of user and group names. The user and hadoop-2.6.5/etc/hadoop/hadoop-policy.xml: The ACL is a comma-separated list of user and group names. The user and hadoop-2.6.5/etc/hadoop/hadoop-policy.xml: The ACL is a comma-separated list of user and group names. The user and hadoop-2.6.5/etc/hadoop/hadoop-policy.xml: <description>ACL for AdminOperationsProtocol. Used for admin commands. hadoop-2.6.5/etc/hadoop/hadoop-policy.xml: The ACL is a comma-separated list