Grep Command example to List only Filenames with Matching String in Linux

The grep command from Linux is one of the powerful commands to find files containing some text, but when you use grep it not only print the file name but also the line which is containing the matching text. This is ok for most of the situations but sometimes you only want to grep to show filename and line and not the matching text e.g. when you are searching for some configuration e.g. database hostname across all configuration files in your application host. Since many files contain the database references, it's possible that you might get a lot of data on output, so if you are only interested in all the files containing matching text, you can use grep -l option. This option of grep only shows filenames which contain matching text.


Here is what the grep -l command option does (from UNIX standard):

-l
(The letter ell.) Write only the names of files containing selected
lines to standard output. Pathnames are written once per file searched.
If the standard input is searched, a pathname of (standard input) will
be written, in the POSIX locale. In other locales, the standard input may be
replaced by something more appropriate in those locales.

and this is the explanation of grep -l command from grep man page:

-l, --files-with-matches
Suppress normal output; instead, print the name of each input
file from which output would normally have been printed. The
scanning will stop on the first match. (-l is specified by
POSIX.)


This option is often used with grep -iR, which recursively search for files containing matching text in sub-directories as well. The grep -i is for case insensitive search. I often use grep -iRl to print all the files across directories containing some matching text e.g. hostname or some configuration parameter.

When you normally search without grep -l it prints all files with matching text as well as shown in the following example:


$ grep -iR Intel *
2015/jan/cpu.txt:Intel i7 is best CPU
2015/motherboard.txt:Intel Motherboard is best you should always buy that.
hardware.txt:Intel Corei7

This option print all the files with a full path containing matching text as shown below:

$ grep -iRl Intel *
  2015/jan/cpu.txt
  2015/motherboard.txt
  hardware.txt


Btw, the path printed is the relative path from current directory and not the absolute path from the root or (/).


Here is the screenshot of grep command examples to list only filenames with matching String in Linux:

Grep Command example to List only Filenames with Matching String in Linux














That's all about the grep command example to print filenames containing matching String. You have also learned how to combine grep -l with grep -i and grep -R to recursively search for all files containing some matching text. You can just run this command from a top level directory e.g. home or / directory or may be a top-level directory of your application e.g. /home/appuser/app directory. It's one of the powerful tools to find out dependencies while migrating from one host to another host.

Further Reading
How Linux Works: What Every SuperUser should know
Linux Command Line Interface (CLI) Fundamentals
Practical Guide to Essential Linux Commands
10 Example of curl command in Linux
How to find all process listening on a port?
10 Examples of lsof command in Linux

Thanks a lot for reading this far. If you like this example of grep command to show only filenames without matching text then please share with your friends and colleagues. If you have any question or feedback then please drop a note.

No comments:

Post a Comment