

Find|xargs like a boss (Real Example) - g-garron
http://www.r-bloggers.com/find-xargs-like-a-boss/

======
noodly
Comments are closed on that page, so instead I will comment here.

There's no need to use xargs, and pipe to sh in this scenario. The same
functionality can be achieved by using only find:

    
    
      PROCS=8;cuffs=`which cufflinks`;find `pwd` -name 'accepted_hits.bam' -type f -exec qsub -l ncpus=$PROCS -- $cuffs -p $PROCS -o {}-cufflinks -g genes.gtf {} \;

~~~
barrkel
Creating a text stream which you then pipe to a shell is a good way of
debugging complex operations, especially potentially destructive ones that may
need inspection to verify. It's a technique I use a lot, to the point that
some of my more destructive scripts don't do anything - they just output shell
commands, and it's up to me to pipe them to the shell.

~~~
noodly
You can achieve this also by using find and its -ok option, instead of -exec,
that way find will ask for confirmation before doing anything.

~~~
JOnAgain
Thanks for the -ok option. was not aware -- but yeah, I've been a heavy user
of -exec option for years, and it always blows my mind when people get crazy
complicated xargs going.

------
saurik
I would have expected an article on how to use these tools "like a boss" to
point out that if there is whitespace in the filenames you will run into
problems unless you use "find ... -print0 | xargs -0 ...".

~~~
masklinn
What I'd have expected in an article on how to use this tool is talk about
`-exec` and `-exec +` before shooting straight for xargs.

------
icebraining
xargs is very useful, although nowadays I usually use GNU parallel instead
(it's essentially the same but with a few advantages:
[https://www.gnu.org/software/parallel/man.html#differences_b...](https://www.gnu.org/software/parallel/man.html#differences_between_xargs_and_gnu_parallel))

~~~
makmanalp
xargs -P is sort of similar

~~~
wging
The link in the post to which you replied covers that; it's the section of the
man page which deals specifically with differences between xargs and parallel.
Having used neither of these tools before, I think two relevant lines from
that link would be:

>xargs can run a given number of jobs in parallel, but has no support for
running number-of-cpu-cores jobs in parallel.

>xargs has no support for keeping the order of the output, therefore if
running jobs in parallel using xargs the output of the second job cannot be
postponed till the first job is done.

------
zaphar
The author would benefit from replacing his backticks with $(pwd) instead. You
can nest this form arbitrarily deep as well as having it interpolate in
strings or heredocs.

~~~
gcr
$(foo) is a bashism; unsupported by different (older) versions of bash and
especially sh

~~~
orra
I don't know how well $(foo) is supported outwith bash _in practice_ , but it
_is_ POSIX:
[http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3...](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_03)

------
sgt
For the record, these examples assume GNU xargs, so they won't necessarily
work with the BSD xargs that comes with OS X.

------
zaptheimpaler
This was one I used to backup all websites in a folder:

    
    
      find . -mindepth 1 -maxdepth 1 -type d | awk 'BEGIN {FS="./"}; {print $2}' | xargs -d '\n' tar czf backup1.tar
    

Note that you'll have to change $2 to the correct column number for the name
field depending on your output.

~~~
Domenic_S
Why not just cp the parent directory?

~~~
narsil
This command doesn't include the files in the parent directory.

An easier way to accomplish the original command uses globbing (like in bash):

    
    
        tar czf backup1.tar */

~~~
Domenic_S
I got that part, but why are there files there in the first place...?

