Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

grep

How can I use grep to show just file-names (no in-line matches) on Linux?

I am usually using something like:

find . -iname "*php" -exec grep -H myString {} \; 

How can I just get the file-names (with paths), but without the matches? Do I have to use xargs? I didn't see a way to do this on my grep man page.

like image 738
cwd Avatar asked Jul 09 '11 22:07

cwd


People also ask

How do I grep only filenames?

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.

How do I list filenames only in Linux?

How to make ls display only filenames and file sizes in output. If you want the ls command output to only contain file/directory names and their respective sizes, then you can do that using the -h option in combination with -l/-s command line option.

How do I list only file names in Unix?

Linux or UNIX-like system use the ls command to list files and directories. However, ls does not have an option to list only directories. You can use combination of ls command, find command, and grep command to list directory names only. You can use the find command too.

Which flag can be used to print filenames in the grep command?

To print just the name of the files where a pattern is found instead of actually matched lines, use -l flag.


1 Answers

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.

like image 119
Random832 Avatar answered Oct 06 '22 01:10

Random832