conandor
conandor

Reputation: 3707

How to read lines of data from multiple files

I need to extract data from files in directory /tmp/log. I have no problem extract from single file.

#!/bin/bash
while read line;
do
  echo $line
done < /tmp/log/file1

I want try it with multiple files /tmp/log/* but it returned error ambiguous redirect. Any idea how can I around it?

Upvotes: 2

Views: 10730

Answers (2)

jcollado
jcollado

Reputation: 40374

You could read the files in a for loop as follows:

for file in /tmp/log/*; do
    while read -r line; do
        echo "$line"
    done < "$file"
done

The strategy is just wrap your while loop with a for loop that takes care of processing each of the files one at a time.

Upvotes: 8

Vijay
Vijay

Reputation: 67221

Dont know exactly waht you need.. probably you are looking for this:

cat /tmp/log/*

Is this what you need?

for line in `cat /tmp/log/*`
do     
echo $line     
done

Upvotes: 0

Related Questions