Jeevs
Jeevs

Reputation: 719

Extract tarball into the same directory

I have a hierarchy of folders, which contain a lot of tarballs. I need to write a script which recursively goes to each directory, extract the tarball in the corresponding directory.

I tried

find ./ -name "*.tar.gz" -exec /bin/tar -zxvf {} \;

The code executed with all the tarballs extracted to the pwd, not in the corresponding directory.

Please assist me on this if possible. Thanks :)

Upvotes: 2

Views: 232

Answers (1)

anubhava
anubhava

Reputation: 786289

You can use find like this:

find . -name "*.tar.gz" -exec bash -c 'd=$(dirname "{}") && b=$(basename "{}") && cd "$d" && tar zxvf "$b"' \;

EDIT A shorter version of above find command will be:

find . -name "*.tar.gz" -execdir tar zxvf "{}" \;

Upvotes: 4

Related Questions