Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to untar all .tar.gz with shell-script?

Tags:

linux

shell

tar

I tried this:

DIR=/path/tar/*.gz

if [ "$(ls -A $DIR 2> /dev/null)" == "" ]; then
  echo "not gz"
else
  tar -zxvf /path/tar/*.gz -C /path/tar
fi

If the folder has one tar, it works. If the folder has many tar, I get an error.

How can I do this?

I have an idea to run a loop to untar, but I don't know how to solve this problem

like image 716
fifty arashi Avatar asked Nov 24 '10 03:11

fifty arashi


2 Answers

I find the find exec syntax very useful:

find . -name '*.tar.gz' -exec tar -xzvf {} \;

{} gets replaced with each file found and the line is executed.

like image 53
Joshua Martell Avatar answered Oct 04 '22 21:10

Joshua Martell


for f in *.tar.gz
do
  tar zxvf "$f" -C /path/tar
done
like image 40
Ignacio Vazquez-Abrams Avatar answered Oct 04 '22 20:10

Ignacio Vazquez-Abrams