Untar subfolders with initial folder’s content in linux

directorytar

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?

Best Answer

try

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.

Related Question