Saturday, November 9, 2019

Grep Ascendency Representative To Listing Exclusively Filenames Amongst Matching String Inwards Linux

The grep ascendance from Linux is ane of the powerful commands to find files containing unopen to text, but when you lot role grep it non solely impress the file advert but also the trace of piece of job which is containing the matching text. This is ok for most of the situations but sometimes you lot solely desire to grep to demo filename too trace of piece of job too non the matching text e.g. when you lot are searching for unopen to configuration e.g. database hostname across all configuration files inward your application host. Since many files comprise the database references, it's possible that you lot mightiness acquire a lot of information on output, thence if you lot are solely interested inward all the files containing matching text, you lot tin role grep -l option. This selection of grep solely shows filenames which comprise matching text.


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

-l
(The missive of the alphabet ell.) Write solely the names of files containing selected
lines to criterion output. Pathnames are written in ane trial per file searched.
If the criterion input is searched, a pathname of (standard input) will
be written, inward the POSIX locale. In other locales, the criterion input may be
replaced past times something to a greater extent than appropriate inward those locales.

too this is the explanation of grep -l ascendance from grep human page:

-l, --files-with-matches
Suppress normal output; instead, impress the advert of each input
file from which output would unremarkably convey been printed. The
scanning volition halt on the starting fourth dimension match. (-l is specified by
POSIX.)


This selection is oftentimes used amongst grep -iR, which recursively search for files containing matching text inward sub-directories every bit well. The grep -i is for instance insensitive search. I oftentimes role grep -iRl to impress all the files across directories containing unopen to matching text e.g. hostname or unopen to configuration parameter.

When you lot unremarkably search without grep -l it prints all files amongst matching text every bit good every bit shown inward the next example:


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

This selection impress all the files amongst a sum path containing matching text every bit shown below:

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


Btw, the path printed is the relative path from electrical flow directory too non the absolute path from the root or (/).


Here is the screenshot of grep ascendance examples to listing solely filenames amongst matching String inward Linux:

 The grep ascendance from Linux is ane of the powerful commands to  Grep Command illustration to List solely Filenames amongst Matching String inward Linux














That's all most the grep ascendance illustration to impress filenames containing matching String. You convey also learned how to combine grep -l amongst grep -i too grep -R to recursively search for all files containing unopen to matching text. You tin only run this ascendance from a exceed score directory e.g. domicile or / directory or may move a top-level directory of your application e.g. /home/appuser/app directory. It's ane of the powerful tools to honor out dependencies spell migrating from ane host to unopen to other host.

Further Learning
Linux Command Line Basics
10 Example of gyre ascendance inward Linux
How to honor all procedure listening on a port?
10 Examples of lsof ascendance inward Linux

Thanks a lot for reading this far. If you lot similar this illustration of grep ascendance to demo solely filenames without matching text too thence delight percentage amongst your friends too colleagues. If you lot convey whatever interrogation or feedback too thence delight drib a note.

No comments:

Post a Comment