- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 perform grep operation on all files in a directory?
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
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() {}
Another command that we can make use of is the find command.
Command
find . -name "*.go" -exec grep -H "func main" {} \;
Output
./main.go:func main() {
- Related Articles
- How to grep a string in a directory and all its subdirectories in Linux?
- How to delete all files in a directory with Python?
- How to list all files in a directory using Java?
- How to unzip all zipped files in a Linux directory?
- How to read data from all files in a directory using Java?
- Java program to delete all the files in a directory recursively (only files)
- How to list all files (only) from a directory using Java?
- Java program to List all files in a directory recursively
- How to get all the files, sub files and their size inside a directory in C#?
- How to find all files in a directory with extension .txt in Python?
- How do I list all files of a directory in Python?
- How to list down all the files available in a directory using C#?
- Java program to List all files in a directory and nested sub-directory - Recursive approach
- How to read all excel files under a directory as a Pandas DataFrame ?
- PHP: Unlink All Files Within A Directory, and then Deleting That Directory
