ls -1 | xargs chmod 755
repeatedly runs the chmod command with a bunch of filenames per invocation until all the output of ls is consumed.
But xargs has a fatal flaw. Spaces in filenames screw it up.
So my shell idiom of choice is now something like:
ls -1 | perl -e 'chomp(@args = <>); while (@args) { @a = splice(@args, 0, 50); system("chmod", "755", @args) }'
(though I'd use Perl's built-in chmod rather than shelling out, in this case).
The case that inspired it was fixing the id3 tags on my mp3 files:
find . -name \*.mp3 -print | perl -e 'chomp(@args=<>); while (@args) { @a = splice(@args, 0, 50); system("id3convert", @a) }'
Change 50 to however many arguments per command you want.
--Nat
Spaces in filenames (Score:2, Insightful)
ls -1 | perl -pe 's/
The more usual (and more comprehensive) way 'round this seems to be to use "find" for everything, and then combine find's "-print0" option with the -0 option to xargs. But that doesn't help much with a simple pipe from "ls".
Cheers,
Paul
Re:Spaces in filenames (Score:3, Informative)
ls -Q | xargs chmod 755
If you don't have it, you know the motto : Get New Utilities.
Re:Spaces in filenames (Score:2)
+1, informative
J. David works really hard, has a passion for writing good software, and knows many of the world's best Perl programmers
Re:Spaces in filenames (Score:2, Insightful)
I have been using xargs with space-riddled filenames for many years, as shown by examples below:
find . -print0|xargs -0 chmod 755 (GNU only) /\\ /g'|xargs chmod 755 (works everywhere)
find . -print|sed 's/
/-\
Re:Spaces in filenames (Score:2)
Well, the Perl idiom of sucking a bunch of array elements off at a time still stands, but now I feel SUPERHUMAN with my new-found shell fu.
use.perl to the rescue!
--Nat