Linux is the most widely used and versatile computer operating system in the world. It is used for general computing, server and desktop computers, operating system development, web server applications, and packaging software.
What are Linux commands?
We need to use another command like the Grep command to pipe the result back and forth if this happens. What the Grep command does is to find the exact match of a string to a regular expression.
A regular expression is a collection of different words and can be matched against a pattern.
So if you want to find all the files containing the keyword “test” in them, you would type in the command line like this, “grep -f test.”
What is Grep Command?
The term “grep” stands for “Global regular expression print“. It is a command-line utility and a powerful search engine for finding exact matches of a string or pattern.
The more familiarly known is the search engine, but grep is more versatile.
It is more complex than it was and is used for search results by a user or a program. Commands that work with keywords in a Linux environment are not only a series of words or strings, and the dots also separate them.
How to grep files in a Directory Recursively?
In this tutorial, we will learn about grepping files in a directory recursively with the help of the grep command in Linux.
We have to use the-Roption to grep all files in a directory recursively.
grep -R string /directory
The Linux grep command will search for the given string within the specified directory and any subdirectories if- R options are used.
If there is no folder name, the grep command will search for the string within the current functioning directory.
grep -R error /var/log/
The Linux grep command searches for the string “error” in the /var/log/ folder.
Case Insensitive Recursive Search
If you want to make it case-sensitive, you can combine the -R option with the -I option.
grep -Ri error /var/log/
The command I mentioned above will grep the files within the/var/log/directory.
Returning the Filename
If you grep All Files within a Directory Recursively (also known as Filename), both the matching lines and Filename are returned as output. If you use the -l option, however, -l will return the Filename.
grep -R -l error /var/log
In the above case, we used the -l option for the grep search recursively. The grep command searches for the string “error” and returns the files containing that string.
The –exclude-dir is used to exclude files from the directory when the search files in a directory recursively.
Example Number 1
grep -R -l –exclude-dir=mine error /var/log/
According to the above command, grep will exclude the “mine” folder from the recursive search.
Example Number 2
grep -R -l –exclude-dir=httpd –exclude-dir=mine error /var/log/
In the above example, the command will grep all files except both filesHTTPdandmine.
Files without a match – Inverse Recursive Search in grep
Another beneficial option is to return files that do not match the specified text pattern when you grep All Files within a Directory.
-L can easily do this option by using the -L or “files-without match” option in the grep search.
grep -R -L error /var/log/
The above example shows that the grep command will return all files within the /var/log/ directory that do not contain the text ‘error.’
This is how Linux Operating System allows us to grep files within a folder recursively.
Now you must know the usage of command lines in the Linux operating system and how commands like grep can help us.
So, we learned in Linux operating system how we could grep files in a specified directory recursively.