views:

184

answers:

3

I want to know how I can send the command(s) spawned by xargs to background. For example, consider

find . -type f  -mtime +7 | tee compressedP.list | xargs compress

I tried

find . -type f  -mtime +7 | tee compressedP.list | xargs -i{} compress {} &

.. and as unexpected, it seems to send xargs to the background instead?

How do I make each instance of the compress command go to the background?

+1  A: 

You could probably make a very quick shellscript to call compress.

#!/bin/sh 
# call it 'compbg' and chmod a+x
compress $* &

then

find . -type f  -mtime +7 | tee compressedP.list | xargs -I{} compbg {}

Although I think you might be happier using this xargs argument:

 -P maxprocs
         Parallel mode: run at most maxprocs invocations of utility at once.

This command should find / tee / compress 10 files at a time until its done, as well as returning control immediately to the calling script/shell.

find . -type f  -mtime +7 | tee compressedP.list | xargs -I{} -P10 compress {} &
gnarf
The UNIX I have ( $ uname -aSunOS xxx 5.10 Generic_xx7xx1-06 sun4u sparc SUNW,Sun-Fire ) does not have -P ( $ xargs -Pxargs: illegal option -- Pxargs: Usage: xargs: [-t] [-p] [-e[eofstr]] [-E eofstr] [-I replstr] [-i[replstr]] [-L #] [-l[#]] [-n # [-x]] [-s size] [cmd [args ...]] )Any other alternatives than the shell script wrapper?
PoorLuzer
Sorry, I can't find much for SunOS - although I did find this package "xjobs" which you might be able to compile... http://www.maier-komor.de/xjobs.html
gnarf
A: 

For SunOS you may have a look at GNU Parallel http://www.gnu.org/software/parallel/

find . -type f  -mtime +7 | tee compressedP.list | parallel compress

It has the added benefit of not crapping out if the filename contains ' " or space. Adding -j+0 will make it run one compress per CPU core.

Ole Tange
A: 

This works for me when I want to edit all files that contain foo

grep -rl foo ./ | `xargs emacs` &

Scott Mattocks