How to Use Grep to Show Just Filenames on Linux

How can I use grep to show just filenames on Linux?

The standard option grep -l (that is a lowercase L) could do this.

From the 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, standard input may be
replaced by something more appropriate in those locales.

You also do not need -H in this case.

How can I grep for a filename instead of the contents of a file?

You need to use find instead of grep in this case.

You can also use find in combination with grep or egrep:

$ find | grep "f[[:alnum:]]\.frm"

Show unique filename only on command grep result

use the -l flag.

test.txt:

Hello, World!
Hello, World!

Grep search:

$ grep ./ -re "Hello"
./test.txt:Hello, World!
./test.txt:Hello, World!
$ grep ./ -re "Hello" -l
./test.txt

From the manual:

-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.

grep output to show only matching file

grep -l 

(That's a lowercase L)

how to display file name during grep

I'm sure this has been on here before, but you just need to give it a second file name

for file in *.py; 
do grep -n --color 'main' $file /dev/null;
done

unix command line ...how to grep and show only file names that contain a string?

The -l flag (or, in both BSD and GNU grep, --files-with-matches) does what you want.

From the POSIX spec:

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

Both BSD and GNU also explicitly guarantee that this will be more efficient. (Older BSD versions say "… grep will only search a file until a match has been found, making searches potentially less expensive", newer BSD and GNU say "The scanning will stop on the first match".) If you don't know which grep you have and which options it has, just type man grep at the shell and you should get the manpage.

How to get only filenames without Path by using grep

Yet another simpler solution:

grep -l whatever-you-want | xargs -L 1 basename

or you can avoid xargs and use a subshell instead, if you are not using an ancient version of the GNU coreutils:

basename -a $(grep -l whatever-you-want)

basename is the bash straightforward solution to get a file name without path. You may also be interested in dirname to get the path only.

GNU Coreutils basename documentation

How to grep only show path/filename?

grep -l will output JUST the names of files which matched, without showing the actual match.

grep listing filenames without content

Use

grep -i -R -l "search keyword" location

to get the list of files which contain the keyword.



Related Topics



Leave a reply



Submit