Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

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 can't seem to find any documentation dealing with this: is there any way to stop grep from printing the path to the file of a matched line when searching over multiple files?

share|improve this question
up vote 5 down vote accepted

From man grep:

-h, --no-filename
Suppress the prefixing of filenames on output when multiple files are searched.

Usage:

grep -h 'search term' multiple files

Example:

$ grep network apache2 svnserve sudo 
apache2:# Required-Start:    $local_fs $remote_fs $network $syslog $named
apache2:# Required-Stop:     $local_fs $remote_fs $network $syslog $named
svnserve:# Required-Start:    $network $remote_fs $syslog
svnserve:# Required-Stop:     $network $remote_fs $syslog
$ grep -h network apache2 svnserve sudo
# Required-Start:    $local_fs $remote_fs $network $syslog $named
# Required-Stop:     $local_fs $remote_fs $network $syslog $named
# Required-Start:    $network $remote_fs $syslog
# Required-Stop:     $network $remote_fs $syslog
share|improve this answer

there are 2 switches to grep that can help you here

  • --exclude, here you can directly provide file-names/paths to excluded using wildcard characters ( & escaping the actually required wildcard character)

    example: grep a ./* --exclude=*.log

  • --exclude-from, here you can provide a filename containing those strings to be matched and excluded

I'm using GNU grep 2.5.4, in case it's not available using your grep version.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .