Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Find a Specific String or Word in Files and Directories in Linux
Finding specific strings or words across multiple files in Linux is a common task for developers and system administrators. This article explores several Linux commands to efficiently search for text patterns across files and directories.
Using grep
The grep command is a powerful regular expression search tool that matches text patterns in files. At its basic level, it searches for a string within specified files ?
grep 'string' directory-path/*.*
Example
grep 'config' hadoop-2.6.5/etc/hadoop/*.*
The output shows all files containing the word "config" ?
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 (Recursive Search)
The -r flag enables recursive search through all subdirectories within the specified path. This is useful for searching through complex directory structures ?
grep -r 'string' directory-path/
Example
grep -r 'done' hadoop-2.6.5/
The recursive search finds "done" across all subdirectories ?
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 for Multiple Words
The egrep command allows searching for multiple words simultaneously using the pipe (|) operator for OR logic ?
egrep -r 'word1|word2' directory-path/
Example
egrep -r 'config|comma' hadoop-2.6.5/
This searches for files containing either "config" or "comma" ?
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:<?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: The ACL is a comma-separated list of user and group names. hadoop-2.6.5/etc/hadoop/hadoop-policy.xml: The ACL is a comma-separated list of user and group names.
Common grep Options
| Option | Description | Example |
|---|---|---|
-i |
Case-insensitive search | grep -i 'CONFIG' files/* |
-n |
Show line numbers | grep -n 'error' log.txt |
-l |
List filenames only | grep -l 'pattern' files/* |
-c |
Count matches | grep -c 'word' files/* |
Conclusion
Use grep for basic string searches, grep -r for recursive directory searches, and egrep with pipe notation for multiple word patterns. These tools provide efficient file content searching across Linux systems.
