
- Kali Linux Tutorial
- Kali Linux - Home
- Installation & Configuration
- Information Gathering Tools
- Vulnerability Analyses Tools
- Kali Linux - Wireless Attacks
- Website Penetration Testing
- Kali Linux - Exploitation Tools
- Kali Linux - Forensics Tools
- Kali Linux - Social Engineering
- Kali Linux - Stressing Tools
- Kali Linux - Sniffing & Spoofing
- Kali Linux - Password Cracking Tools
- Kali Linux - Maintaining Access
- Kali Linux - Reverse Engineering
- Kali Linux - Reporting Tools
- Kali Linux Useful Resources
- Kali Linux - Quick Guide
- Kali Linux - Useful Resources
- Kali Linux - Discussion
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
- Related Articles
- How to Protect Files and Directories from Deleting in Linux
- Delete empty files and directories in Linux
- How to move a file, group of files, and directories in Linux?
- Find the Largest Top 10 Files and Directories On a Linux
- How to remove files and directories in the Linux operating system using the terminal?\n
- How to compare the files available in two directories using diff command in Linux?
- How to find all files with names containing a string on Linux?
- How to Copy a File to Multiple Directories in Linux?
- Listing out directories and files in Python?
- How to copy a file, group of files, or directory in Linux?
- How to Search and Remove Directories Recursively on Linux?
- How to list non-hidden files and directories in windows using Python?
- Compare two directories in Linux?
- How to parse for words in a string for a specific word in java?
- How to split or break large files into pieces in Linux?
