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?


From man grep:

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


grep -h 'search term' multiple files


$ 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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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