javier_domenech
javier_domenech

Reputation: 6263

Grep list files once

I'm looking for some pattern in my project, and it's been repeated too much in some files.. How can I tell grep to show only once each file?

I'm using:

grep -R "my_pattern" *

Upvotes: 7

Views: 2259

Answers (4)

If you want only the names of the files containing "my_pattern":

grep -lR "my_pattern" *

But the above command ignores all sub-folders starting with a dot.

If you want the file names from the sub-folder .git containing "https://github.com":

grep -lR "https://github.com" .git/*

Upvotes: 1

grahamrhay
grahamrhay

Reputation: 2166

You can use the "count" functionality:

grep $TERM -crl $DIR

The -c returns a count of hits in each file, and the -l removes any that have a count of 0

Upvotes: 2

Anthem
Anthem

Reputation: 41

You can use the "uniq" command: http://unixhelp.ed.ac.uk/CGI/man-cgi?uniq

grep -R "my_pattern" * | sort -u

This will sort and give you unique values.

Upvotes: 4

Nicolas Galler
Nicolas Galler

Reputation: 1309

You could do this, it will make grep only print the first match:

grep -m 1 -R "pattern" *

Upvotes: 7

Related Questions