Question

I have a file dagens_130325.tar.gz containing the folder dagens. In one folder I have hundreds of these daily info. I would like to unpack dagens_130325.tar.gz/dagens to 130325 with all the files inside. Then 130326 etc.

Is there a way to do it?

Was it helpful?

Solution 2

This was the solution I came up with in the end

#!/bin/bash
search_dir=/yourdir/with/tar.gz
for entry in "$search_dir"/*.tar.gz
do
  substring=$(basename "$entry")
  echo $substring
  sub2=${substring:7:6}
  tar -xvzf $substring
  rm -rf $sub2
  mv dagens $sub2
done

OTHER TIPS

Not sure this is the right stack where to ask this kind of question, however try with

tar -zxvf dagens_130325.tar.gz -C /tmp/130325 dagens

This way, the folder dagens for the archive dagens_130325.tar.gz is going to be extracted into /tmp/130325. However, note that the target folder must exist, otherwise the command will fail

So, supposedly you have 4 archives in the form dagens_1.tar.gz, dagens_2.tar.gz, ..., you can write an extract.sh file containing

#!/bin/bash

for i in {1..4}
do
   mkdir /tmp/$i
   FILE="dagens_$i.tar.gz"
   tar -zxvf $FILE -C /tmp/$i dagens
done

Having this file the execute permission, being in the same folder as your archives and executing it should produced the result you asked.

use

#!/bin/bash
for file in dagens_*.tar.gz
do
    from=${file%_*} #removes chars after _
    to=${file#*_}   #removes chars before _
    to=${to%.t*}    #removes chars after .t (.tar.gz)
    tar -zxf $file --show-transformed --transform "s/$from/$to/"
done
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top