Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to grep only show path/filename? [duplicate]

Tags:

linux

grep

I'm trying to locate the files which contain certain pattern, so I use grep -ir 'pattern' . to search all files under current directory. But I don't want to see the content of each file, I only need the path/filename. How can I do that?

Ps: Because some JSON file contains very long text in one line, then it will occupy the whole screen. I don't want to filter out those JSON files, because that's what I'm trying to locate

like image 782
CodTango Avatar asked Dec 01 '14 21:12

CodTango


People also ask

How do I only grep for filenames?

In order to get only filenames, we can enable the -l or the --files-with-matches flag.

How do I specify a path in grep?

If you want to find in all files of a directory, you can use grep -r /path/to/directory .

Can you use grep on a directory?

You can make grep search in all the files and all the subdirectories of the current directory using the -r recursive search option: grep -r search_term .

How do I grep all files in a directory?

To search all files in the current directory, use an asterisk instead of a filename at the end of a grep command. The output shows the name of the file with nix and returns the entire line.


1 Answers

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

like image 91
Marc B Avatar answered Sep 21 '22 14:09

Marc B