Suppose in unix directory,there are some files placed with same filename but with different extension like


There can be n number of files.

I need to first get the files from the directory and then iterate on each files. With the below command I am getting the files but can anyone please help how to iterate on each file and read each file and do processing.?

find "/tmp/files/" -name "abc.*" -type f -exec echo {} \;
  • what kind of processing are you planing to perform? – RomanPerekhrest Jan 3 '18 at 14:06
  • 1
    @RomanPerekhrest:- After I get the list of files I need to iterate on all files and grep for particular value. – user6376225 Jan 3 '18 at 14:11
  • If the file names are the same save for extensions AND the extensions are 3 byte numbers: for f in abc.*; do [[ "$f" =~ "abc."[0-9]{3} ]] && echo "Processed: $f"; done – Stan Strum Jan 3 '18 at 17:51

Why iterate? grep takes several file arguments at once, so

grep PATTERN abc.*

should work.

In case the files are not all in the same directory, using find is an option:

find /tmp/files -name 'abc.*' -type f -exec grep PATTERN /dev/null {} +

The + at the end tells find to pass as many files as possible to grep in one go. The /dev/null is to make sure at least 2 files are passed to grep so grep always outputs the file name for every match. With GNU grep, you can use the -H (aka --with-filename) option instead.

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.