3

I have a list of .tar.gz files with the same inner folder structure and I'd like to batch gunzip | untar a single file from them in separated folders.

logs_2013-08-01.tar.gz
logs_2013-08-02.tar.gz
logs_2013-08-03.tar.gz
logs_2013-08-04.tar.gz
logs_2013-08-05.tar.gz

I know the command to untar a single file is

tar -xvf {tarball.tar} {path/to/file}  

And adding the -C to specify the output folder the command for a single archive looks like:

tar -xvf logs_2013-08-01.tar.gz -C 2013-08-01/ /var/logs/audit

But how can I do a loop to untar everything?

sergei
  • 33
  • 1
  • 6

3 Answers3

3

Use atool(1). It is already packaged for many distributions.

Just run:

$ atool -x -e *.tar.gz

to uncompress and untar every file in its own directory.

dawud
  • 15,096
  • 3
  • 42
  • 61
3

You can try this (tested on Linux/bash):

for pkg in logs_*.tar.gz; do
   where="${pkg#logs_}"
   where="${where%.tar.gz}/"

   [ -d "$where" ] || mkdir "$where"

   tar zxfv $pkg -C "$where" /var/logs/audit
done

Simply, you loop over all the tgz archives in the current dircetory, then you take "date" part from its name, create required directory and extract them finally.

dsmsk80
  • 5,817
  • 18
  • 22
2

You can simply loop over the files like this

#!/bin/bash
# Pass the name of the file to unpack on the command line $1
for file in *.gz
do
    dir=$(echo "$file" | cut -c 6-15)
    tar -xvf "$file" -C "$dir" "$1"
done
user9517
  • 115,471
  • 20
  • 215
  • 297