Reputation: 1
new to bash scripts and trying to grep
for a string in a list of directories:
directory.txt contains the following
/apps/work/txt.out
/apps/work/Monday.txt
/apps/garbage/howdo.file
What I want to do is for each line in directory.txt, read the file and grep
for a string.
Can someone point me in the right direction?
Upvotes: 0
Views: 42
Reputation: 184975
Try doing this :
cat directory.txt | xargs grep word
Replace word
with your own pattern.
Upvotes: 0
Reputation: 22821
Should be as simple as this:
search="BLOCK"
while read -r filename; do
[ -f "$filename" ] && grep "$search" "$filename"
done < directory.txt
The while loop gets its input from directory.txt
, puts each line into the variable $filename
, checks if the file actually exists and if so executes the grep
command on it
Upvotes: 1