dracoblue.net

bash tip: for each line in a file

When you want execute an action on a set of files (all, which contain testfile), this technique is pretty straight forward:

for line in `find . | grep "testfile"`
do
    rm "$line"
done

But this actually does not work, if the file name contains spaces!

In this cases the "while read"-construct is failsafe:

find . | grep "testfile" | while read line
do
    rm "$line"
done
In bash by
@ 05 Apr 2010, Comments at Reddit & Hackernews