0

I have a .tar.gz file that contains a backup of files across many directories.

Is it possible to unpack the single .tar.gz file and have those files moved to their intended directories?

If I run tar -ztvf filename.tar.gz I see all the files with the full directory. That is /etc/path/file, /usr/path.file ... etc. is returned for many (over 1,000) files. So the archive was created with the path to file preserved if that helps.

I know that tar –xvzf filename.tar.gz –C /path/to/directory will allow me to move a specific file. But can this be done with multiple files going to multiple different locations?

1 Answer 1

2

You can change directories as many times as necessary:

tar –xvzf filename.tar.gz –C /path/to/directory filepath1 -C ../../../path/to/otherdirectory filepath2

Each successive -C changes the current directory for everything that follows, so directories have to be specified relative to the previous directory. Since your archive preserves paths, you need to specify the full path of every file to extract.

tar extracts files to the path stored in the archive; so if you want all the files to go to the path they’re stored in in the archive, without prepending anything, run

tar -xzvf filename.tar.gz -C /

With GNU tar, you can apply file name transformations which can be used to change output file paths and names too.

3
  • See man tar on OpenBSD: “This is a positional argument which sets the working directory for the following files. When extracting, files will be extracted into the specified directory; when creating, the specified files will be matched from the directory.” Commented Sep 15, 2021 at 16:31
  • so if I am understanding I would have to designate every file to each directory I want it to go to? There is not a more efficient means to do this? there is no way to say untar this file and move the contents to the directory in its name? seems like the transformations would be useful for changing the directory end location or file name, but I dont want to do either of those.
    – mcv110
    Commented Sep 15, 2021 at 17:08
  • @mcv110 wait, you mean the paths stored in the archive are the desired paths? If so, tar -xzvf filename.tar.gz -C / will do the trick. Commented Sep 15, 2021 at 17:58

You must log in to answer this question.

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