fighting with xargs :-( - Solved

Don Russell fedora at
Mon Jun 26 17:45:20 UTC 2006

Don Russell wrote:
> Don Russell wrote:
>> Not FC5 specific...
>> I am trying to use a (simple) command to gzip a bunch of files in a 
>> given directory... BUT I want to keep the original files too. gzip 
>> does not seem to have an option to keep the original file. In some 
>> cases, the file names contain blanks and or $ characters...
>> I want to gzip each file individually, not combine several files into 
>> one .gz file.
>> So, I thought some form of ls and xargs would do the trick and I 
>> would be done in 5 minutes. :-)
>> ls -1 *[^.gz] | xargs -r -I {fn} gzip -c {fn} > {fn}.gz
>> (hours pass, reams of reading later...)
>> I added the -p option to xargs so I could see what it is actually 
>> doing (vs what I think it should do) and see the command actually 
>> stops at the >. The > {fn}.gz isn't part of the command created by 
>> xargs...
>> Try again, escaping the > ...
>> ls -1 *[^.gz] | xargs -rp -I {fn} gzip -c {fn} \> {fn}.gz
> Thanks to all who replied..... I learned a very important thing about 
> xargs.... xargs does not use a shell to run the command, so the 
> command build by xargs cannot use redirection.
> This is what I did in it's place and it works well, including handling 
> file with blanks etc...
> find *[^.gz] -print0 | while read -d $'\0' fn;
> do
>    gzip -c --best "$fn" > "$fn.gz";
>    touch -r "$fn" "$fn.gz";
> done;
> Works great.... not as easy as, say...
> ls -1Q *[^.gz] | xargs -rI {fn} gzip -k --best {fn}
> Where a -k option means "and keep the original file where you are done"

oops... should be *when* you are done...

More information about the users mailing list