1

I have a .tar.gz file which may have the following files:

folder1/folder2/folder3/imp_folder1/file11.jpg
folder1/folder2/folder3/imp_folder1/file12.jpg
folder1/folder2/folder3/imp_folder2/file21.jpg
folder1/folder2/folder3/imp_folder3/file31.jpg
...
...

I want to untar it to the following directories:

/new_folder1/new_folder2/imp_folder1/file11.jpg
/new_folder1/new_folder2/imp_folder1/file12.jpg
/new_folder1/new_folder2/imp_folder2/file21.jpg
/new_folder1/new_folder2/imp_folder3/file31.jpg
...
...

Basically, folder1/folder2/folder3/ should be replaced by /new_folder1/new_folder2/.

Since, this will have 100s of thousands of files, what will be the fastest way to achieve this ?

AdminBee
  • 22,803
thatGuy
  • 11

1 Answers1

2

You've already got few links to possible answers in the comments, but I wanted to suggest another approach. You can create an identical directory tree as the original in some location, and link the path you want to replace (folder1/folder2/folder3) to the new location (/new_folder1/new_folder2).

cd /tmp
mkdir -p folder1/folder2
ln -s /new_folder1/new_folder2 folder1/folder2/folder3

Then you can extract the archive by adding the --keep-directory-symlink flag (exists since version 1.27).

tar --keep-directory-symlink -zxvf tarball.tar.gz
aviro
  • 5,532