How can I:

  • Search for a particular pattern in the content of files in a directory and its subdirectory
  • And list the filename if it has particular pattern?

use: the -l and -r flags:

grep -l -r pattern

man grep

-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 POSIX .)

-R, -r, --recursive Read all files under each directory, recursively; this is equivalent to the -d recurse option.

  • Hi Yaron, thanks for the reply... I am using find . -type f -exec egrep "mxprod|nlsoprdc" {} /dev/null \; and i am getting the matches further i need to zip all the files listed with the above command and delete.. pls help – kavya Aug 10 '17 at 5:20
  • Hi Yaron tried your answer getting error like : grep: illegal option -- r Usage: grep -hblcnsviw pattern file . . . pls help – kavya Aug 10 '17 at 5:23
  • @kavya - egrep has the option of -l which output only the name of the files it found, so you can use the -l flag. regarding your new question -I'd open a new question * - I've a list of files - resulted from find command, how can I use it to zip/tar the files and delete the original files* – Yaron Aug 10 '17 at 5:32

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