
- 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 all files with names containing a string on Linux?
In order to be able to find all files with names containing a string in Linux command line, we will make use of the grep command, and at first we must understand what a grep command is and how to use it on Linux.
The grep command in Linux is used to filter searches in a file for a particular pattern of characters. It is one of the most used Linux utility commands to display the lines that contain the pattern that we are trying to search.
Normally, the pattern that we are trying to search in the file is referred to as the regular expression.
Syntax
grep [options] pattern [files]
While there are plenty of different options available to us, some of the most used are −
-c : It lists only a count of the lines that match a pattern -h : displays the matched lines only. -i : Ignores, case for matching -l : prints filenames only -n : Display the matched lines and their line numbers. -v : It prints out all the lines that do not match the pattern -R : stands for recurse, would go into subdirectories as well.
Now, let’s consider a case where we want to find a particular pattern in all the files in a particular directory, say dir1.
Syntax
grep -rni "word" *
In the above command replace the “word” placeholder with
For that, we make use of the command shown below −
grep -rni "func main()" *
The above command will try to find a string “func main()” in all the files in a particular directory and also in the subdirectories as well.
Output
main.go:120:func main() {}
In case we only want to find a particular pattern in a single directory and not the subdirectories then we need to use the command shown below −
grep -s "func main()" *
In the above command we made use of the -s flag which will help us to not get a warning for each subdirectory that is present inside the directory where we are running the command.
Output
main.go:120:func main() {}
Example
Command
grep -R "apples" .
Output
immukul@192 linux-dir1% grep -R "apples" . ./d1/file.txt:apples ./d1/file.txt:applesauce ./d1/file.txt:applesnits ./d1/file.txt:dapples ./d1/file.txt:grapples ./d1/file.txt:mayapples ./d1/file.txt:pineapples ./d1/file.txt:sapples ./d1/file.txt:scrapples ./d2/2.txt:orange apples is great together ./d2/2.txt:apples nto great ./d2/2.txt:is apples good
- Related Articles
- How to Recursively Search all Files for Strings on a Linux
- Move all files except one on Linux
- Find and Convert Files Ending With CRLF on Linux
- Find and tar Files on Linux
- How to unzip all zipped files in a Linux directory?
- How to Find a Specific String or Word in Files and Directories in Linux
- Select column names containing a string in MySQL?
- Find the Largest Top 10 Files and Directories On a Linux
- How to search contents of multiple pdf files on Linux?
- How to find and sort files based on modification date and time in linux
- How to find all files in a directory with extension .txt in Python?
- Move All Files Including Hidden Files Into Parent Directory in Linux
- Find all links for a specific file on Linux
- How to join lines of two files on a common field in Linux?
- How to download APK files from Google Play Store on Linux
