Site icon The Ultimate Linux Newbie Guide

running a command against every line in a textfile

shell prompt image

shell prompt image

So, you have a text file like so:

file1
file2
..

And you want to run a command on each line of the file (say, chmod 644). Like all things UNIX, there are more than one way to do thing, but here’s the quick and dirty answer:

xargs -0 -n 1 chmod 644 < <(tr \\n \\0 <filelist.txt)

 

So, this will run xargs on the std. input (filelist.txt).
-n is max arguments – in this case we have 1 arg. (the list of files in the file – per line).
-0 means that input lines are terminated by a null char, not a whitespace, to keep things tidy.
Finally, we run tr which deletes characters, in this case removing the newlines and whitespace.

Exit mobile version