0

I'm runing a grep to find recursively filenames which files content contain a string.

grep -rl string-to-find $pwd

The command returns results as expeted, but with file name and path:

var/log/httpd/access.log
var/log/httpd/access.log.1

How could I set it return only file name not full path?

I would like to get back as result:

access.log
access.log.1
BenB
  • 2,747
  • 3
  • 29
  • 54

1 Answers1

3

grep has no such flag. But you can pipe its output to a simple awk to get your desired output:

grep -rl string-to-find $pwd | awk -F/ '{ print $NF }'

The -F/ is to set the field separator to /, and print $NF means to print the last field.

janos
  • 120,954
  • 29
  • 226
  • 236