0

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

abc.001
abc.002
abc.003

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 {} \;
3
  • what kind of processing are you planing to perform? Commented Jan 3, 2018 at 14:06
  • 1
    @RomanPerekhrest:- After I get the list of files I need to iterate on all files and grep for particular value. Commented Jan 3, 2018 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 Commented Jan 3, 2018 at 17:51

1 Answer 1

2

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.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.