how perform grep operation on all files in a directory

user2147075 picture user2147075 · Mar 8, 2013 · Viewed 349k times · Source

Working with xenserver, and I want to perform a command on each file that is in a directory, grepping some stuff out of the output of the command and appending it in a file.

I'm clear on the command I want to use and how to grep out string(s) as needed.

But what I'm not clear on is how do I have it perform this command on each file, going to the next, until no more files are found.

Answer

umi picture umi · Mar 8, 2013

grep $PATTERN * would be sufficient. By default, grep would skip all subdirectories. However, if you want to grep through them, grep -r $PATTERN * is the case.