5

There is a similar question here.

If I understand correctly, the first answer indicates that we cannot extract the contents of a folder, but only a specific file from a folder?

The second answer uses the --directory option. This looks to be precisely what I want, however this does not extract files from a certain directory within the archive, but instead outputs the files to the directory indicated by the --directoryoption.

When I tarred up my folder tar included the entire path from the root directory down. So it has home/ole/folder/thestuff. I want to extract folder/thestuff into the current folder that I'm in without including home/ole.

Thoughts?

Ole
  • 707
  • 1
    You can always follow the tar extraction with mv home/ole/folder . and then rmdir home/ole; rmdir home. – NickD Sep 21 '18 at 18:30
  • Yes that's what I have been doing. I'm also going to use the -C option in the future to leave the home/ole folder off the path, but I was thinking that there has to be a way to just extract a folder? – Ole Sep 21 '18 at 18:31
  • @steve ... I tried the --directory option which ended up totally nuking the updated content I had in the original sub folder ... ooops :) – Ole Sep 21 '18 at 18:33
  • If it's not possible, that's cool, it's an easy work around ... But if it is possible then I'd like to understand how ... – Ole Sep 21 '18 at 18:35

2 Answers2

6

If I understand correctly, the first answer indicates that we cannot extract the contents of a folder, but only a specific file from a folder?

No, it's talking about relative vs absolute filenames. The difference between /some/path and some/path

Take a look at the contents of your tarfile tar tf file. You can't specify the folder as /path/to/folder, when it's listed as path/to/folder.

There's no problem giving (the correct) folder to extract.

  • List the files that will be extracted

tar tf file.tar path/to/folder

  • Extract the files (into the current directory)

tar xf file.tar path/to/folder

As an example:

Check the contents:

$ tar tf ../test.tar
a/
a/3
a/2
a/1
b/
b/3
b/2
b/1

Extract folder a

$ tar xf ../test.tar a

See what it extracted

$ find .
.
./a
./a/3
./a/2
./a/1
BowlOfRed
  • 3,772
2

In my case I could do this:

tar -xvzf archive.tar.gz --strip-components=2

This removes home/ole from the path, and extract everything below that in the archive into the current directory.

Ole
  • 707