940

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

Chris Maes
  • 107
  • 5
shawnxuc
  • 9,503
  • 2
    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
  • 2
    @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
  • 2
    @PatMorin To be fair, once you understand the argument calling conventions you're using it makes sense. You can also do tar xfC archive.tar mydir or tar -C mydir -xf archive.tar. It's only mixing traditional-style and GNU-style flags that breaks things, as... is honestly kind of expected. Traditional flags are passed all as one blob as the first argument. You can't expect them to work if they're not the first. (I mean, yes, it's possible to interpret it anyway -- my point is that'd be above and beyond, not standard) –  Jun 11 '19 at 19:30

5 Answers5

1300

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.

muru
  • 197,895
  • 55
  • 485
  • 740
Wesley Rice
  • 13,466
  • 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 07 '15 at 06:15
  • 1
    If you're having troubles with this, note the --strip-components=1 advice from the other answer as well. – DreadPirateShawn Dec 25 '15 at 19:17
  • 26
    @consultstan https://xkcd.com/1168/ – Dessa Simpson Nov 28 '16 at 21:15
  • If output directory is long, in general, save to a variable like $TargetDir then mkdir -p "$TargetDir" && tar --directory "$TargetDir" myfile.tar.gz – Dagrooms Jul 17 '17 at 16:59
  • 47
    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 08:21
  • I know it seems frustrating but linux's philosophy is "do one thing and do it well". That's because even for trivial things like creating a directory, it creates a lot more complexity when writing and maintaining a utility like tar. There are other tools that exist to create directories and tar's main function is to extract and compress files, which it does exceedingly well :) – user2490003 Apr 18 '19 at 15:35
  • 2
    I totally agree, there should be an option to create the output directory if it doesn't exist. Otherwise, you can just extract the file and use mv. – Marius Hofert May 13 '19 at 11:50
  • @MariusHofert mv is not convenient if the tar creates many files in current directory after extraction, if current directory already contains several files, it is difficult to know which ones came out of the tar file – Dojo Sep 28 '22 at 09:03
  • A flag for "always expand in a folder of the name of the archive, except if the archive already outputs in a folder of the name of the archive" – Shodan Feb 26 '24 at 09:59
354

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
Bryan Larsen
  • 3,751
  • 3
    Thank you. Downloads via wget/etc always have a 'parent' directory. This is the complete answer imo - or should be noted in answer above. – B. Shea Apr 28 '17 at 16:51
  • 3
    Just what I was looking for. --strip-components is so useful when running provisioners that might use different versions. This way /target/directory can be a generic name, without having to worry about how the provider chose to name their archive folder. – Jack_Hu Jan 30 '19 at 17:54
  • 1
    Just a heads up, this can remove any number of leading directories, not just one: --strip-components=NUMBER strip NUMBER leading components from file names on extraction – ᴍᴇʜᴏᴠ Oct 28 '19 at 07:37
45

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
Thamme Gowda
  • 583
  • 4
  • 7
  • You can skip creating the directory by adding the --one-top-level=/target/directory option to the tar command, and removing the -C option. See this answer – smac89 Dec 12 '19 at 02:02
39

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
  • 6
    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
    This is the answer I needed. Very useful "not" to have to specify the directory name, or remember special code that will refer back to the file for the name. Thank you. – Hypocritus Jul 20 '19 at 00:51
  • 2
    For anyone on macOS, you'll need to install gnu-tar with brew install gnu-tar and then use gtar to get this command line option. – gsgx Sep 30 '22 at 04:47
4

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 /

Betlista
  • 411
  • 2
  • 4
  • 11