beck03076
beck03076

Reputation: 3308

Parallel processing of untar/remove in unix shell script

Question:

I want to untar a tarfile which has many tar files within itself and remove the files in all the tar files and I want all of these processes to run in parallel in Unix bash scripting.

Conditions:

  1. The script should return an error if any untar/remove process has any error.
  2. It should only return success after all N (untar and remove) processes complete successfully.

Proposed solution:

 mkdir a
 tar -C a -xvf b.tar
 cd a
 for i in *
 do
 rm -r $i &
 done

Upvotes: 1

Views: 4660

Answers (3)

Ole Tange
Ole Tange

Reputation: 33725

If you have GNU Parallel http://www.gnu.org/software/parallel/ installed you can do this:

tar xvf foo.tgz | perl -ne 'print $l;$l=$_;END{print $l}' | parallel rm

It is useful if you do not have space to extract the full tar.gz file, but you need to process files as you unpack them:

tar xvf foo.tgz | perl -ne 'print $l;$l=$_;END{print $l}' | parallel do_stuff {}\; rm {}

You can install GNU Parallel simply by:

wget http://git.savannah.gnu.org/cgit/parallel.git/plain/src/parallel
chmod 755 parallel
cp parallel sem

Watch the intro videos for GNU Parallel to learn more: https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1

Upvotes: 3

oHo
oHo

Reputation: 54611

The answer tar xvf a.tar | tac | xargs -P 4 rm -rv is inspired from Burton Samograd's comment about xargs -P

$ mkdir -p a/b/c/d
mkdir: created directory `a'
mkdir: created directory `a/b'
mkdir: created directory `a/b/c'
mkdir: created directory `a/b/c/d'

$ touch a/1 a/2 a/3 a/b/4 a/b/5

$ tar cf a.tar a

$ rm -rfv a
removed directory: `a/b/c/d'
removed directory: `a/b/c'
removed `a/b/4'
removed `a/b/5'
removed directory: `a/b'
removed `a/3'
removed `a/1'
removed `a/2'
removed directory: `a'

$ tar xvf a.tar | tac | xargs -P 4 rm -rv
removed `a/2'
removed `a/1'
removed `a/3'
removed `a/b/5'
removed `a/b/4'
removed directory: `a/b/c/d'
removed directory: `a/b/c'
removed directory: `a/b'
removed directory: `a'

Upvotes: 1

Burton Samograd
Burton Samograd

Reputation: 3638

mkdir a
tar -C a -xvf b.tar
 cd a
 success=$(for i in *
 do
 rm -r $i || echo failed & # if a job fails false will be echoed
 done
 wait)
 # if any of the jobs failed, success will be set to a value other than ""
 [[ -z "$success" ]] && exit 0 || exit 1

Upvotes: 1

Related Questions