Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I currently have n data files in a directory where each file has at most 1 line of very long data. My directory structure is


I know that at least one of those files contain the keyword I'm looking for, but since the one line of data is too long, it covers my entire terminal. How do I get the filename only after performing a keyword grep? The grep command I'm using is:

grep keyword *.json
share|improve this question
up vote 16 down vote accepted

The -l argument should do what you want.

   -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
share|improve this answer

You can use xargs in combination with --null with grep or --print0 with ack. I find that ack speed is faster.

grep -lr --null searchterm * | xargs -I {} -0 echo {}

ack -lr --print0 searchterm * | xargs -I {} -0 echo {}

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.