How do you run a command for each line of a file?

hawk picture hawk · Dec 18, 2012 · Viewed 195.4k times · Source

For example, right now I'm using the following to change a couple of files whose Unix paths I wrote to a file:

cat file.txt | while read in; do chmod 755 "$in"; done

Is there a more elegant, safer way?

Answer

P.P picture P.P · Dec 18, 2012

Yes.

while read in; do chmod 755 "$in"; done < file.txt

This way you can avoid a cat process.

cat is almost always bad for a purpose such as this. You can read more about Useless Use of Cat.