user3862515
user3862515

Reputation: 73

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

Upvotes: 6

Views: 29730

Answers (4)

Nether
Nether

Reputation: 1160

In case you want to recursively untar into the same folder the archive was in, you can do the following:

for file in `find . -name '*.tar.gz'`; do \
  DRN_NAME=$(dirname $file)
  sudo tar -xzvf "${file}" -C $DRN_NAME ;
  sudo rm $file
done

for file in `find . -name '*.tgz'`; do \
  DRN_NAME=$(dirname $file)
  sudo tar -xzvf "${file}" -C $DRN_NAME ;
  sudo rm $file
done

Works on:

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.1 LTS
Release:    20.04
Codename:   focal

$ tar --version
tar (GNU tar) 1.30

$ find --version
find (GNU findutils) 4.7.0

$ bash --version
GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu)

Upvotes: 2

Schopenhauer
Schopenhauer

Reputation: 1182

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


Upvotes: 9

Elliott Frisch
Elliott Frisch

Reputation: 201537

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 {} \;

Upvotes: 5

invalid entry
invalid entry

Reputation: 127

This should recursively extract all tar.gz files in the current directory and all sub-directories.

for subdir in `find . -type d`
do
  for f in $subdir/*.tar.gz
  do
    tar -xzf $f -C <destination>
  done
done

Upvotes: -1

Related Questions