Quick Tip #2: How to extract files with TAR in Linux

Print this articlePrint this article

There are different types of archives – those are canonical (for tapes) tar archive files, there are tar files compressed with Z, gzip or bzip2, etc. There are also apt, deb, rpm, yum packages, but those do require full scale package manager, as opposed to simple extraction utility.

This short tip should give you instructions on extracting archived files from different archives, using Linux shell utility called ‘tar’.

Now let us show you few known extraction techniques for simple archives.

How to extract tar files

To extract files from archive with *.tar extension, you should execute the following command:

tar xf filename.tar

NOTE: This will extract files to current directory. If you want to extract it somewhere else, you should execute this command instead:

tar xf filename.tar -C /somepath

This will extract all files from filename.tar and put them into /somepath folder. NOTE: The path should exist already (mkdir /somepath to create new directory).

How to extract tar.gz or tgz files

To extract files from *.tar.gz or *.tgz archive, you should issue the following command:

tar zxf filename.tar.gz
OR
tar zxf filename.tgz

How to extract files from tar.bz2 archive

To extract files from *.tar.bz2 archives, you should use the following command:

tar jxf filename.tar.bz2

How to extract files from tar.Z archive

To extract files from *.tar.Z archive, you should issue the following command:

tar Zxf filename.tar.Z

NOTE: Z should be capital, otherwise no files will be extracted, since small ‘z’ means to use Gzip filter, but big ‘Z’ means to use Compress filter – which are DIFFERENT.

How to display process of extraction

To see how files are being extracted, use the ‘v’ (as in Verbose) switch.

tar zxvf filename.tar.gz
tar jxvf filename.tar.bz2
tar xZvf filename.tar.Z

How to extract a single file from tar.gz, tar.bz2 or tar.Z files

To extract a single file from using TAR utility, execute one of the following commands:

tar zxf filename.tar.gz somefile1 somefile2 – for extraction of somefile1 and somefile2 ONLY, from tar.gz archive
tar jxf filename.tar.bz2 somefile – for extraction of somefile ONLY, from tar.bz2 archive
tar Zxf filename.tar.Z someFile – for extraction of someFile ONLY, from tar.Z archive

How to prevent files from being overwritten

By default, tar will overwrite any files that match those names and paths as given in the archive. You can change this behavior by adding ‘k’ switch.

tar zxfk filename.tar.gz – will extract ONLY files that are NOT EXISTING in the path of extraction, giving “File exists” message for every file found

How to keep newer files ONLY, during extraction

To overwrite all files, except those that are newer than ones in archive, issue this command:

tar --keep-newer-files zxf file.tgz – will keep all newer files found in path of extraction

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <span>
  • Lines and paragraphs break automatically.
  • Each email address will be obfuscated in a human readable fashion or (if JavaScript is enabled) replaced with a spamproof clickable link.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • Each email address will be obfuscated in a human readable fashion or (if JavaScript is enabled) replaced with a spamproof clickable link.
Image CAPTCHA
Enter the characters shown in the image.