606

I thought tar archive.tar /users/mylocation would work, but it doesn't. How can I do that?

  • Also, -C, --directory DIR\n change to directory DIR should rather be described as change output directory to DIR (will fail if DIR doesn't exist). Thanks @Mich. See also @Bryan_Larsen's answer. – valid Jul 13 '15 at 10:21
  • 1
    @Ujjwal Sing : Indeed, the tar man page is not that helpful. It describes "-C DIR" as "change to directory DIR" . Even worse: "tar -C mydir xf archive.tar " doesn't work, while "tar xf archive.tar -C mydir" does. – Pat Morin Aug 25 '16 at 15:46
881

To extract an archive to a directory different from the current, use the -C, or --directory, tar option, as in

tar -xf archive.tar -C /target/directory

Note that the target directory has to exist before running that command (it can be created by mkdir /target/directory).

Read the manual page (command: man tar) for other options.

  • 71
    You have to create the directory manually though. – Jürgen Paul May 16 '13 at 10:01
  • Can the path be stripped: i.e just retrieve the file – zzapper Aug 26 '14 at 14:52
  • 3
    If it's a larger file, when you run the command, all you get is no output for several seconds or even minutes while it's working. You can fix this by adding the -v flag (verbose mode) which lists the name of each file as it extracts it. – IQAndreas May 7 '15 at 6:15
  • 7
    @consultstan xkcd.com/1168 – Duncan X Simpson Nov 28 '16 at 21:15
  • 5
    Isn't it amazing that, after all these years, with all the options tar has accumulated, there still isn't an option to create the output directory if it doesn't exist? – EM0 Apr 13 '18 at 8:21
233

Note that if your tarball already contains a directory name you want to change, add the --strip-components=1 option:

tar xf archive.tar -C /target/directory --strip-components=1
  • Thank you. Downloads via wget/etc always have a 'parent' directory. This is the complete answer imo - or should be noted in answer above. – bshea Apr 28 '17 at 16:51
24

Combining the previous answers and comments:

To simply extract the contents and create target directory if it is missing:

mkdir -p /target/directory && tar xf archive.tar -C /target/directory

To extract and also remove the root(first level) directory in the zip

mkdir -p /target/directory && tar xf archive.tar -C /target/directory --strip-components=1
9

Another option is to use --one-top-level. This will automatically create a directory based on the filename of the original.

tar zxvf filename.tgz --one-top-level

Additionally if you want, you can specify your own and tar will create it automatically.

tar zxvf filename.tgz --one-top-level=new_directory
  • 1
    I'm using GNU tar 1.26 and I get an unrecognized option error for --one-top-level – Emma Strubell Sep 24 '18 at 14:29
1

What I found interesting in relation to extraction is, that it depends how you created the archive, see this example

cd /tmp
mkdir folder
touch folder/file.txt

when you do tar -zcvf folder.tar.gz folder everything is as expected = when you untar it now it will be untarred (folder will be create, if you removed it) as /tmp/folder/.

But, when you will create tar as tar -zcvf tmp-folder.tar.gz /tmp/folder and you untar it in /tmp folder, the result will be /tmp/tmp/folder directory ! In such case you have to untar it to / - tar -xf tmp-folder.tar.gz -C /

1

With tar 1.29, we can just add the directory name after the tar filename. For instance,

tar zxvf tar-file directory-name/

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.