perl one-liner: print filenames with content matched

17 September 2009

If you want to know which files contains a specific text string you can do:

perl -nle '/find this/ and print $ARGV and close ARGV' file1 file2 .....
  • $ARGV contains the name of each file.
  • ARGV is the filehandle for $ARGV.
  • This one-liner searches each line (-n option) for a match (/xxx/) and when it happens (and) prints the file name and closes the filehandle to pass to search the next file.

This one-liner is very useful when  combined with xargs.