305

I've downloaded mongodb-linux-x86_64-2.6.3.tgz file using windows 7 and kept it on D:\Amra\Software\Developing Soft location.

When I right click this .tgz file using Ubuntu and see property it shows Location: /media/towhid/Amra/Software/Developing Soft. Now how will I unzip this .tgz file using tar command from terminal?

karel
  • 110,292
  • 102
  • 269
  • 299
Towhid
  • 3,955
  • 3
  • 16
  • 11

3 Answers3

503

To extract a .tgz file with tar you need to use,

tar -xvzf /path/to/yourfile.tgz

where,

  • x for extract
  • v for verbose
  • z for gnuzip
  • f for file, should come at last just before file name.

You can use the following command in a terminal to unzip the file in your case,

tar -xvzf /media/towhid/Amra/Software/Developing\ Soft/mongodb-linux-x86_64-2.6.3.tgz

Extract a .tgz file in different directory:

One can use -C option to extract archive contents to a different directory as following,

tar -xvzf /path/to/yourfile.tgz -C /path/where/to/extract/
sourav c.
  • 44,037
  • 20
  • 101
  • 128
  • 4
    It shows following error: `tar (child): /media/towhid/Amra/Software/Developing: Cannot open: No such file or directory tar (child): Error is not recoverable: exiting now tar: Child returned status 2 tar: Error is not recoverable: exiting now ` – Towhid Jul 19 '14 at 08:25
  • 1
    you just copy my command, there is a syntax error with white space in your command. – sourav c. Jul 19 '14 at 08:27
  • 1
    if a folder name is `some folder`, you need to access it from a terminal as `some\ folder` using escape character. – sourav c. Jul 19 '14 at 08:30
  • 4
    This is very clear answer where answerer give complete explanation in every option. And a plus given for the example. Very understandable. – Ade Malsasa Akbar Jul 19 '14 at 08:39
  • @souravc how we specify the location for the uncompressed files ? – Kasun Siyambalapitiya Nov 07 '16 at 12:29
  • 1
    @KasunSiyambalapitiya you can change directory using `-C` option as `tar -xvzf /path/to/myarchive.tgz -C /path/where/to/extract/` – sourav c. Nov 07 '16 at 12:56
  • you can skip the `- ` whlie specifying the options and do `tar xvzf /path/to/yourFile.tgz`, otherwise it can have unexpected behavior depending on the order of the flags. check this answer : https://unix.stackexchange.com/questions/239118/does-parameter-order-matter-with-tar – VishnuVS Sep 23 '22 at 11:24
32

Let's end the decades of hardly-memorable one-letter tar options. Use this to extract your .tgz file:

tar --extract --file /path/to/file.tgz

The explanation of used options was purposedly left out.

Jeyekomon
  • 493
  • 1
  • 6
  • 12
  • 1
    Great add also the option to [extract files contained in archive.tar.gz to a new directory named archive](https://stackoverflow.com/a/59265563/2641825) `tar --extract --file /path/to/file.tgz --one-top-level`. That's often the default of GUI file archive programs. – Paul Rougieux Apr 23 '20 at 21:58
  • I would have upvoted this. Unfortunately, on my old Raspberry Pi at least, this didn't support the Tab autocompletion, and the filename was like 60 chars, whereas `tar -xvzf filename` *did* support Tab autocomplete, so I didn't have to type the whole filename. Therefore, I used the other answer instead. – Gabriel Staples Sep 19 '20 at 21:57
  • @GabrielStaples Well that is strange. I don't have the chance to test it on the Raspberry but the tab completion for this command should definitely work on many linux distributions. – Jeyekomon Sep 21 '20 at 08:06
5

Open the terminal and use the cd command to change directories to the directory where the mongodb-linux-x86_64-2.6.3.tgz file is located and the run the following command:

tar xzf mongodb-linux-x86_64-2.6.3.tgz   

The above command will extract the contents of the mongodb-linux-x86_64-2.6.3.tgz archive while preserving the archive's hierarchical tree directory structure.

A similar command extracts .tar.xz files. Open the terminal and the run the following command:

tar -xf /path/to/your/file.tar.xz

Explanation:

  • -x extract files from an archive
  • -f use archive file
karel
  • 110,292
  • 102
  • 269
  • 299
  • 1
    The archive is both compressed (gzip - hence the "z" flag/option), and tar achived, hence the "x" extract option. So unzip won't work since it doesn't do tars. – pd12 Jan 24 '15 at 04:19
  • Would it not be `tar -xzf`... (with dash)? – No Time Jan 24 '15 at 04:23
  • `xzf` with a dash or without a dash, both are OK, and you can also add a v for verbose to the options (`xzvf`) to see the files being extracted during unpacking. – karel Jan 24 '15 at 04:44