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?
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?
I see that you tagged bash, but Perl would also be a good way to do this:
You could also apply a regex to make sure you're getting the right files, e.g. to only process .txt files:
To "preview" what's happening, just replace the backticks with double quotes and prepend
print
:If your file is not too big and all files are well named (without spaces or other special chars like quotes), you could simply:
If you have special chars and/or a lot of lines in
file.txt
.if your command need to be run exactly 1 time by entry:
This is not needed for this sample, as
chmod
accept multiple files as argument, but this match the title of question.For some special case, you could even define location of file argument in commands generateds by
xargs
:If you know you don't have any whitespace in the input:
If there might be whitespace in the paths, and if you have GNU xargs:
If you want to run your command in parallel for each line you can use GNU Parallel
Each line of your file will be passed to program as an argument. By default
parallel
runs as many threads as your CPUs count. But you can specify it with-j
Yes.
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.if you have a nice selector (for example all .txt files in a dir) you could do:
bash for loop
or a variant of yours: