How do you extract a single folder from a large tar.gz archive?

298,095

tar stores relative paths by default. GNU tar even says so if you try to store an absolute path:

tar -cf foo.tar /home/foo
tar: Removing leading `/' from member names

If you need to extract a particular folder, have a look at what's in the tar file:

tar -tvf foo.tar

And note the exact filename. In the case of my foo.tar file, I could extract /home/foo/bar by saying:

tar -xvf foo.tar home/foo/bar # Note: no leading slash

So no, the way you posted isn't (necessarily) the correct way to do it. You have to leave out the leading slash. If you want to simulate absolute paths, do cd / first and make sure you're the superuser. Also, this does the same:

tar -C / -xvf foo.tar home/foo/bar # -C is the ‘change directory’ option

There are very obvious, good reasons why tar converts paths to relative ones. One is the ability to restore an archive in places other than its original source. The other is security. You could extract an archive, expect its files to appear in your current working directory, and instead overwrite system files (or your own work) elsewhere by mistake.

Note: if you use the -P option, tar will archive absolute paths. So it always pays to check the contents of big archives before extracting.

Share:
298,095

Related videos on Youtube

Garrett Hall
Author by

Garrett Hall

Updated on September 18, 2022

Comments

  • Garrett Hall
    Garrett Hall over 1 year

    I am using this command on a 5GB archive

    tar -zxvf archive.tar.gz /folder/in/archive
    

    is this the correct way to do this? It seems to be taking forever with no command line output...

    • Admin
      Admin about 12 years
      Did you use an absolute or relative path? Should be relative, other than that it looks right. How long did you let it run, and how good is your system? It will take at least several minutes on a fast machine. Potentially an hour or more? I'm running a test now.
    • Admin
      Admin about 12 years
      Yes, it completed after ~1hr, but the absolute path was wrong, you were correct.
    • Admin
      Admin about 12 years
      On Linux, you can watch how far into the archive tar is with cat /proc/$(pidof tar)/fdinfo/0 (adapt the command if you have more than one tar process running).
    • Admin
      Admin about 7 years
      The important part is also no trailing slash. Using folder/folder2/ won't work.
  • cjaphe
    cjaphe almost 10 years
    --strip-components=N may be useful to some here.
  • santiago arizti
    santiago arizti over 7 years
    in my case it was necessary to write ./foldername/ instead of foldername, I guess that it was because when I created the tar I created it with tar -cvf santi.tar ./* and when I "listed" the tar this is what it said drwxr-xr-x santi/santi 0 2016-04-11 09:42 ./foldername/
  • Peter Krauss
    Peter Krauss almost 7 years
    And about .gz ?
  • Simon Gates
    Simon Gates almost 7 years
    @PeterKrauss change -xvf to -xzvf (adding the -z option) and obviously foo.tar to foo.tar.gz or whatever your archive is named. The same holds for -j (bz2) and on some recent versions, -J (xz). Otherwise, a pipeline like zcat foo.tar.gz | tar -xvf - … also works.
  • Bo R
    Bo R about 5 years
    And remember, if you are tired of all the verbose output during extraction just skip the v in all the examples. E.g., tar -tf foo.tar and tar -xf foo.tar ....