How to find Files with Matching String in Linux? grep -l command Example

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 including the matching text. This is actually required and needed in most situations. Still, sometimes you only want to grep to show just filename and path and not the matching text. For example, when you are searching for some configurations like a Linux or database hostname across all configuration files in your application host, then you just want to see which file has contained those references.

Since many files contain database references, 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 the grep -l option. This option of grep only shows filenames that contain matching text.

Btw, a good knowledge of essential Linux commands like find, grep, awk, and sed goes a long way in improving your Linux skills as well as improving your productivity. Hence, I also suggest you join one of these online Linux command line courses to learn Linux fundamentals in depth.

Linux has already survived for more than 40 years, and I am sure it will remain relevant in the coming years as well.  This means any investment you made in learning Linux commands like grep will be useful for many years to come.


grep - l example


Anyway, let's understand what does -l option on grep command does; 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 searches 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 like hostname, IP Address, 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 the current directory and not the absolute path from the root or (/). If you want to learn more about the Linux directory structure, these free Linux online courses are a good starting point.



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 maybe a high-level list of your application like /home/appuser/app directory. It's one of the powerful tools to find out dependencies while migrating from one host to another host.


Related UNIX Command Tutorials
If you are a Java developer often working in Linux or UNIX environment then you will also find the following tutorials useful:
  • 10 examples of find command in UNIX (examples)
  • 10 examples of xargs command in Linux (examples)
  • 10 examples of tar command in UNIX (examples)
  • 10 examples of Vim in UNIX (examples)
  • How to create, update and delete soft link in UNIX (command)
  • 10 examples of grep command in UNIX (examples)
  • 10 examples of date command in Linux (examples)
  • How to get an IP address from the hostname and vice-versa in Linux (command)
  • How to delete empty files and directories in UNIX (solution)
  • 5 examples of sort command in Linux (examples)
  • 5 examples of kill command in Linux (examples)
  • 10 examples of chmod command in UNIX (examples)
  • How to make a directory tree in one command? (example)
  • How to how long argument of a process in Solaris (command)
  • UNIX command to find out how long a process is running? (answer)
  • UNIX command to find the size of the file and directory? (command)
  • 10 tips for working fast in UNIX? (tips)

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

P. S. - If you want to learn Linux and looking for some free resources like books and online courses, then you can also check out this list of free Linux courses for Programmers and IT Professionals. This list contains some of the best free courses from Udemy, Pluralsight, Coursera, Codecademy, and other online platforms.

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.