How to Use grep on All Files in a Directory

Files are a key part of any operating system as they can store and manipulate crucial data for that system. To perform efficient editing on the files, the user must be able to search through the file for certain keywords. Therefore, the “grep” command comes into work. This command is used to search for certain keywords inside a specified file.

This article will provide knowledge on how to use the “grep” command on all files in a directory.

How to Grep Command Works?

We have learned that the “grep” command searches for terms inside a file. The advanced usage of “grep” command is to search for a keyword in all the fields of a specific directory. This section demonstrates two examples of using “grep” on all files in the directory.

Example 1: Navigate to the Specific Directory to Use grep on All Files

In this example, the target directory is approached using the “cd” command, and then the “grep” command is applied. The syntax of the “grep” command in such a situation is provided below:


$ grep keyTerm *

The “keyTerm” represents the term that you want to search, and the “*” represents that the search will be carried out throughout all the files in that directory.

The first step in this process is to navigate to the required directory using the “cd” command as shown below:

$ cd Desktop/textDocs

Once you are inside your required directory, the next step is to run the grep command alongside the key term that you need to search and then add the “*” sign at the end which states that all the files need to be searched for that term. A sample code for this is shown below:

$ grep file *

All the sentences with the term “file” are displayed alongside the name of their respective files. The file names are displayed in the purple font on the left-hand side.

Example 2: Using the Absolute Path of the Directory to Use grep on All Files

Another way of achieving this is to utilize the direct path (absolute path) of the directory instead of only the “*” sign.

In such a case, the syntax of the “grep” command would be:


$ grep keyTerm /path/of/directory/*

The “keyTerm” will be searched inside a specific directory whose path is provided.

Let’s see how it works using the command provided below. The keyword “text” will be searched on a path “/home/itslinuxfoss/Desktop/textDocs/*”.

$ grep text /home/itslinuxfoss/Desktop/textDocs/*

The snippet above shows how the direct path of the directory is used with the “*” sign at the end and all the lines containing the text in all files.

That’s it from this guide!


There are two methods to execute the grep command on your system to search through all the files in a directory. The first method is to navigate to the directory using the “cd” command and then using the “*” sign to search through all the files. The second method is to utilize the directory path in the command and then search through the files. This article has a detailed demonstration of the “grep” command on all files in a directory.