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
Finding Number Is Even/Odd in Assembly
Pipe Only Stderr Through a Filter
How Can a Process Intercept Stdout and Stderr of Another Process on Linux
How to Specify the Library Version to Use At Link Time
How to Compare Two Datetime Strings and Return Difference in Hours? (Bash Shell)
Execute Command After Every Command in Bash
Getting a Unique Id from a Unix-Like System
Contiguous Physical Memory from Userspace
How to Programmatically "Burn In" Ansi Control Codes to a File Using Unix Utils
Assembly Segmentation Fault After Making a System Call, At the End of My Code
How to Delete an Exported Environment Variable
Recursively Counting Files in a Linux Directory
How to Find All Serial Devices (Ttys, Ttyusb, ..) on Linux Without Opening Them
Linux Bash: Multiple Variable Assignment
How to Disassemble Raw 16-Bit X86 Machine Code