ChatGPT解决这个技术问题 Extra ChatGPT

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

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. We don’t allow questions about general computing hardware and software on Stack Overflow. You can edit the question so it’s on-topic for Stack Overflow. Closed 4 months ago. The community reviewed whether to reopen this question 4 months ago and left it closed: Original close reason(s) were not resolved Improve this question

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.

You mean, you want the filenames that have at least one match?
Great question. I actually started using this to open a list of files returned by grep in Vim. vim $(grep -rl Something app/)
You cannot list files using grep command, it is basically used to fetch desired text from a file or a list. For instance, ps aux | grep 'apt-get' or grep 'text-to-find' /path/to/file/
@iamjayp Ummm, you can too list files using the grep command. grep -lr 'text-to-find' ./* works quite nicely!

P
Peter Mortensen

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.


-H also shows the matches themselves. The question was for just filenames.
@Hauke And my answer was -l. I mentioned -H only because the user's command in the question included -H and it is redundant when -l is given.
Nice, I personally like using this with other flags grep -nrl "some text" . when looking for text in a set of subfolders recursively
What's the mnemonic for ell?
"fiLes with matches"
I
Ignacio Vazquez-Abrams

From the grep(1) 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.)


Stopping at the first match is necessary and nature .
the documentation makes it a bit unclear. "The scanning will stop on the first match" - indicates that all file names will be printed, but the scanning for the matching word will stop at the first occurence.
@VishalP: Since we don't care where the occurrences are, only that they exist, there is no point in searching each file beyond the first occurrence.
@IgnacioVazquez-Abrams Exactly. The first impression of the document makes you feel that it will only print the file name of the first file. You really need a couple of reads to understand it.
'The scanning of the current file will stop on the first match.' would be much clearer.
P
Peter Mortensen

For a simple file search, you could use grep's -l and -r options:

grep -rl "mystring"

All the search is done by grep. Of course, if you need to select files on some other parameter, find is the correct solution:

find . -iname "*.php" -execdir grep -l "mystring" {} +

The execdir option builds each grep command per each directory, and concatenates filenames into only one command (+).


P
Peter Mortensen

My command suggestion for getting the filename with path

sudo find /home -name *.php

The output from this command on my Linux OS:

compose-sample-3/html/mail/contact_me.php


The question specifically asks how they get the file names using grep.
This command will actually not do what you expect. Shell expansion will first expand *.php and then feed the result as argument 4 into find. You should always escape your file pattern with either \*.php or "*.php" when using find.