How to extract a tgz which consists of many files present in multiple subfolders to another directory (ubuntu linux)

by cezudun   Last Updated October 11, 2018 16:01 PM

current directory
            ├──Folder1
                ├── sub_folder1
            ├──Folder2
                ├── sub_folder21
                ├── sub_folder22
                      ├── sub_folder221
            ├──Folder3
                ├── sub_folder31
            ├──Folder4
                ├── sub_folder41
            ├──Folder5
                ├── sub_folder51

Given the above folder subfolder hierarchy, which was compressed into one tgz file, I need to extract all files present in any folders subfolders, into one and only output directory.
Actually all files exist in the deepest path of each folder subfolder combination. for example in sub_folder1,sub_folder2, sub_folder221, etc. I have tried with the following:

find . -name '*.tar.gz' -execdir tar -xzvf '{}' \;

...but that outputs the exact hierarchy of files and subfolders, when I need all files in all subfolders, to be extracted to just one output folder /path/to/one/folder

Tags : linux ubuntu find tar


Answers 1


The execdir is exactly the wrong command for your task, it will change to the directory of the file.

If you use -exec, the working directory will stay the same and all tar files will be extracted from that directory.

There is also the option -C to make tar change to that directory.

find . -name '*.tar.gz' -exec tar -xzvfC '{}' /path/to/one/folder \;
RalfFriedl
RalfFriedl
October 11, 2018 15:13 PM

Related Questions


Gracefully deleting files older than 30 days

Updated April 25, 2018 23:01 PM