You can use grep in your system to search for any particular expressions. It helps a user to look for certain files with the target text which further works in troubleshooting the system errors and finding particular files.
However, the “grep” command has a drawback: it creates separate entries for every line that contains the matching text. This often results in piling up the output screen with unnecessary text elements. So, in this quick blog, we will explain a simple way to use the “grep” command to print the file name only in Linux.
How to Use the Grep Command to Print the File Name Only
The “grep” command supports various options that serve different purposes. One of those is the “-l” option which instructs it to display only the name of the files with the matching content.
Replace the terms “search_expression” with the text that you want to find and “target_directory” with the directory it should search while looking for the same. Furthermore, to search the whole system, you can also put the “target_directory” as “*”.
For instance, let’s search for any file which consists of the “hello world” string.
Note that your target text might not be in the same title cases as your input in the “grep” command. Hence, always use the “-i” option to perform a case-insensitive search. Upon execution, the previous command displays the individual directories and the respective matches inside them.
Conclusion
Grep is a very potent command to do searches within the system to find the files that contain specific expressions. However, the problem arises when it fills your search results with repetitive text elements. Therefore, to get rid of it, this short blog explains using the “grep” command to print only the file name. This simple method involves using the “-l” option. Besides, you should also use the “-i” option to avoid the title case-related issues.