Question

I have created a script below and when I execute it, I face a problem. Instead to extract it 5 times it will just extract once. So how to get this issue resolved?

i=0

   for tarfile in *.tar.gz;
                        do
                        ((i++))
                         [ $i = 5 ] && break ;
                tar -xzvf $tarfile
                done
                rm -rvf $tarfile

Help is greatly appreciated. I want to extract the tar.gz file and inside of it is only tar.gz file. Noted: it is decompressed 5 times and I want to get the last tar.gz file decompressed. Please help me.

Was it helpful?

Solution

If and only if the filenames for each of the nested tar do not collide, you can do the following.

for i in `seq 1 4`; do name=$(ls *.tar.gz); tar xvfz $name; rm $name; done;

If they are all the same, like foo.tar.gz inside foo.tar.gz inside foo.tar.gz, you can simply delete the rm $name from the above.

If there are only some collisions you will have to play more clever tricks, such as moving intermediate files into another directory.

Update: If you just want to move the final tar to a new directory, just do use mv after the loop above.

mv *.tar.gz OtherDirectoryName
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top