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 the 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 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 a comprehensive Linux command line course like Learn Linux in 5 Days and Level Up Your Career on Udemy 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 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 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, Linux Command Line Interface (CLI) Fundamentals course on Pluralsight is 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

Btw, you would need a Pluralsight membership to get access to this course, which costs around $29 per month or $299 annually (14% discount).

If you don't have Pluralsight membership, I encourage you to get one because it allows you to access their 5000+ online courses on all the latest topics like front-end and back-end development, machine learning, etc. It also includes interactive quizzes, exercises, and the latest certification material.

It's more like Netflix for Software Developers and Since learning is an important part of our job, Pluralsight membership is a great way to stay ahead of your competition.

They also provide a 10-day free trial without any commitment, which is a great way to not just access this course for free but also to check the quality of courses before joining Pluralsight.


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 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 Learning
Linux Command Line Basics
Linux Command Line Interface (CLI) Fundamentals
Learn Linux in 5 Days and Level Up Your Career
The Linux Command Line: A Complete Reference (Book)


Related UNIX Command Tutorials
If you are Java developer often working in Linux or UNIX environment then you will also find 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 directory 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 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