Here's the result: Recursive search with -r option of grep command Here's the recursive search I performed in the previous example to do a grep search in the current folder: grep -r simple. With this option, grep will look into all the files in the current (or specified) directory and it will also look into all the files of all the subdirectories. Grep provides a -r option for the recursive search. Grep recursive search in all subdirectories of a directory Now that you know that, let's see how you can perform a recursive search with grep so that it also looks into the files in the subdirectories. If you are not in the same directory where you want to perform, you can specify the directory path and end it with /* grep search_term directory_path/*īasically, you are using the wild card to expand on all the elements (files and directories) of the given directory. Search in all files of a directory with grep Since you cannot directly grep search on a directory, it will show "XYZ is a directory" error along with search results. This will search in all the files in the current directories, but it won't enter the subdirectories. The wild card actually substitutes with the name of all the files and directories in the current directory. To search for the word 'simple' in all the files of the current directories, just use wild card (*). Except empty.txt, all files contain the term 'simple' on which I'll perform the grep search. Here's the directory structure I am going to use in this example. Let me show you all this in details with proper examples so that it is easier for you to understand. You may also specify the directory path if you are not in the directory where you want to perform the search: grep -r search_term directory_path You can make grep search in all the files and all the subdirectories of the current directory using the -r recursive search option: grep -r search_term. It only searches in all the files in the current directory. If you want to search all the files in a directory with grep, use it like this: grep search_term * Usually, you run grep on a single file like this: grep search_term filename main.Grep is an excellent tool when you have to search on the content of a file. 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. In the above command replace the “word” placeholder withįor that we make use of the command shown below − grep -rni "func main()" * Now, let’s consider a case where we want to find a particular pattern in all the files in a particular directory, say dir1. v : It prints out all the lines that do not match the pattern
n : Display the matched lines and their line numbers.
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 Normally, the pattern that we are trying to search in the file is referred to as the regular expression. It is one of the most used Linux utility commands to display the lines that contain the pattern that we are trying to search. The grep command in Linux is used to filter searches in a file for a particular pattern of characters.