Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

untar all .gz in directories and subdirectories

For starters I have checked most of the solutions available no

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

Unzipping multiple zip files in a directory?

I have a directory that has subdirectories with .gz files in them, and would like to extract all into either one folder or just keep them in their folders. Thank you in advance

like image 561
user3862515 Avatar asked Dec 19 '22 12:12

user3862515


2 Answers

Problem

You want to decompress all compressed files inside a directory and all its subdirectories.

Solution

Use bash and the utility find to output to the console a list of all contents from the present directory. Use a looping construct to decompress each file.

Decompress all files in the current directory:

$ for file in `ls -1`; do
       sudo tar -xvf "${file}" ; done

Decompress all archives in the current directory and any subdirectories (my personal favorite):

$ for file in `find *`; do
       sudo tar -xvf "${file}" ; done

Decompress all archives recursively and do the same again for any remaining:

# Make the above loop a function to be called more than once 
# In case of compressed files inside compressed files this will 
# run twice the previous snippet.

$ function decompress_all_complete () {
     function decompress_all () {
          for file in `find *`; do
                sudo tar -xvf "${file}" ; done
     } ;
    for i in `echo {1..2}`; do
         decompress_all_complete; done
}

You could use variants of this for loop, if you like adventure :-)



    $ for program in tar unzip untar ; do      # You could simply add to this list...
         for file in `find *`; do
               sudo `which "${program}"` -xvf "${file}" ; 
         done ;
    done

Discussion

The utility find lists everything from your present directory, and is fast. The snippet below will decompress the files one directory at a time each time but illustrates the simple logic of all the following code. Above are options and variations that do solve this problem; I was assuming at first that your present working directory contains all the files that you want to decompress to use the simplest version of the snippet.

This pseudo code tries to communicate the logic behind my solution briefly:

#Use all decompressing programs locally installed :: apropos compress
for --temp_container_1-- in --list_of_programs_to_decompress_files-- ; do

# run each program through every file in the current directory
for --temp_container_2-- in --list_of_files-- ; do

    # use program with x options on y file
    sudo "${temp_container_1}" [TYPE COMMON OPTIONS] "${temp_container_2} ;

# Successfully decompressed some files. Try another program.
done ;

# There is nothing else to do.
done

Context

I tested these snippets using or in:

* Linux debian 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt9-3 (2015-04-23) x86_64 GNU/Linux
* find (GNU findutils) 4.4.2
* GNU bash, version 4.3.33(1)-release (x86_64-pc-linux-gnu)
* tar (GNU tar) 1.27.1


like image 126
Schopenhauer Avatar answered Jan 02 '23 18:01

Schopenhauer


If I understand your question, you could use something like

DEST=<Destination Folder>
SRC=<Src Folder>
find $SRC -name "*.tar.gz" -or -name "*.tgz" -exec tar xzvvf -C $DEST {} \;
like image 42
Elliott Frisch Avatar answered Jan 02 '23 17:01

Elliott Frisch