I have a directory, /Landsat_Data/ which contains subdirectories (Landsat_Data/Site1, Landsat_Data/Site2, etc.). Each subdirectory contains .tar.gz files (e.g. /Landsat_Data/Site2/LE70930862008092-SC20160107074735.tar.gz, etc.) and each .tar.gz file contains tif or xml files like thisL

-rw-r--r-- espa/ie  29952 2016-01-07 14:57 LT50930861991021ASA00_sr_snow_qa.tif 

What I want to do is to untar each tar file into its own sub-subdirectory (e.g. Landsat_Data/Site2/LE70930862008092-SC20160107074735/).

So far, I am using this command line while being the Landsat_Data directories:

find . -type f -name "*.tar.gz" -execdir tar -xvzf {} \;

However, this command extracts all the tif and xml files into each subdirectory (e.g. Landsat_Data/Site2) while I just want to untar the LE70930862008092-SC20160107074735.tar.gz to have a sub-sub-directory called LE70930862008092-SC20160107074735 which contains all the tif and xml files. Any idea how I can achieve what I want?

  • Could we see a sample of tar -tvf of a tar file? It also seems like your subfolder have a further folder structure, could we see a sample of that as well? Oct 12, 2016 at 13:15
  • When you say "tar folder", you mean "tar file", right? Oh, and in the *nix world, we don't have folders, we have "directories" :)
    – terdon
    Oct 12, 2016 at 13:19
  • @Zachary -rw-r--r-- espa/ie 59416 2016-01-07 14:57 LT50930861991021ASA00_sr_band5.tif -rw-r--r-- espa/ie 29952 2016-01-07 14:57 LT50930861991021ASA00_toa_band6_qa.tif
    – SimonB
    Oct 12, 2016 at 13:20
  • Welcome to Unix & Linux! Please edit your question to add extra information. It is hard to read and easy to miss in the comments. Also, comments can be deleted with no warning.
    – terdon
    Oct 12, 2016 at 13:20
  • @terdon I have tar directories which contains different tif files as shown in a previous comments. Just want to untar the directories while the files stay in the directories. Hope it is clear.
    – SimonB
    Oct 12, 2016 at 13:23

2 Answers 2



find . -type f -name "*.tar.gz" -execdir tar -xvzf {} \;

according to man find

-execdir command ;

-execdir command {} +

Like -exec, but the specified command is run from the subdirectory containing the matched file, which is not normally the directory in which you started find.

  • Thanks. But this extracts the files contain in the tar-folder but I just want to untar the tar-folder while the files stay inside the untar-folders. Any clue how to do it?
    – SimonB
    Oct 12, 2016 at 13:05
  • @SimonB can you edit your post 1) with initial folder's content 2) intended extracted file ?
    – Archemar
    Oct 12, 2016 at 13:08

Here's one way (this assumes you're using bash):

$ shopt -s globstar   
$ for tar in /Landsat_Data/**/*.tar.gz; do 
   tar xvzf "$tar" --one-top-level="$(dirname "$tar")"/"$(basename "$tar" .tar.gz)"

The globstar option makes ** match 0 or more files and directories recursively, so /Landsat_Data/**/*.tar.gz will match all *tar.gz files in any subdirectory of /Landsat_Data/. The dirname command prints the directory name of its input and the basename only the name with the extension you give it (here tar.gz removed. So, for example:

$ dirname /Landsat_Data/Site2/LE70930862008092-SC20160107074735.tar.gz
$ basename /Landsat_Data/Site2/LE70930862008092-SC20160107074735.tar.gz .tar.gz

The --one-top-level is explained in man tar:

          Extract all files into DIR, or, if used without argument, into a
          subdirectory  named by the base name of the archive (minus stan‐
          dard compression suffixes recognizable by --auto-compress).

Taken together, this will do what you want.

If your tar doesn't have that option, you can do it manually:

$ shopt -s globstar   
$ for tar in /Landsat_Data/**/*.tar.gz; do 
    newdir="$(dirname "$tar")"/"$(basename "$tar" .tar.gz)"
    mkdir "$newdir"
    tar xvzf "$tar" -C "$newdir"
  • Thanks terdon. I do have an error message though. ' unrecognized option '--one-top-level=/Landsat_Data/**/*'
    – SimonB
    Oct 12, 2016 at 14:40
  • @SimonB what operating system are you using?
    – terdon
    Oct 12, 2016 at 14:42
  • I am on linux OS
    – SimonB
    Oct 12, 2016 at 14:46
  • @SimonB strange, maybe you have an older tar version. Anyway, see updated answer.
    – terdon
    Oct 12, 2016 at 14:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .