how to create and extract tar.gz and tar.bz2 archives

posted on 16 Oct 2008 12:47 by worapot  in Hosting

Introduction

The commonly employed archive types on Linux and Unix systems are tar, tar.gz and tar.bz2 archives. Note that tar.gz and tar.bz2 archives are simply gzip-ped and bzip(2)-ped tar archives, respectively. Working with these files is made very simple through the use of GNU tar utility, which is included as part of the base packages in modern distros. In this article, I'll show you how to create and extract compressed archives using tar.

So, how is tar used? Lets start by looking at some of the common tar operations and options.

man tar  # ©2007 linux.dsplabs.com.au

The following tar usage information is taken directly from the man pages.

Usage:   tar <operation> [options]Common Operations:   -A, --catenate, --concatenate    append tar files to an archive   -c, --create                     create a new archive   -r, --append                     append files to the end of an archive   -x, --extract, --get             extract files from an archiveCommon Options:   -f, --file [HOSTNAME:]F          use archive file or device F                                    (default "-", meaning stdin/stdout)   -j, --bzip2                      filter archive through bzip2,                                    use to decompress .bz2 files   -p, --preserve-permissions       extract all protection information   -v, --verbose                    verbosely list files processed   -z, --gzip, --ungzip             filter the archive through gzip,                                    use to decompress .gz files

Note that the GNU tar utility does not require a preceding minus for the single letter options (thanks to correct, see comments below).

Creating archives

To create a tar archive the c switch is used. To further encode it using gzip compression the j option is also added, or for bzip2 compression the j switch is included. Note that tar program pipes its output into gzip and bzip2 tools in order to create the tar.gz and tar.bz2 archives, respectively. OK, to compress a directory called dir into dir.tar, dir.tar.gz and dir.tar.bz2 archives, the following commands are used, respectively.

tar cf dir.tar dir/  # ©2007 linux.dsplabs.com.autar czf dir.tar.gz dir/  # ©2007 linux.dsplabs.com.autar cjf dir.tar.bz2 dir/  # ©2007 linux.dsplabs.com.au

In the above examples, the use of the f option specifies that the compressed version of the dir directory is to be placed in a corresponding archive file. On the other hand, if the f option was not given (and thus the archive name was also omitted), then the stdout, i.e. your terminal screen, would be used as the output instead. This is useful if you want to pipe the output of your tar command into another Linux tool. Anyhow, lets have a look at the resulting archives as well as the original directory using ls -la.

total 424drwx------ 3 kamil kamil   4096 Nov 27 22:39 .drwx------ 3 kamil kamil   4096 Nov 27 22:34 ..drwx------ 2 kamil kamil   4096 Nov 27 22:36 dir-rw------- 1 kamil kamil 276480 Nov 27 22:39 dir.tar-rw------- 1 kamil kamil  83330 Nov 27 22:39 dir.tar.gz-rw------- 1 kamil kamil  45927 Nov 27 22:39 dir.tar.bz2

Lets also have a look at the size of the original directory using du -sh dir # ©2007 linux.dsplabs.com.au.

280K    dir

From the above shell output, you can see that by default tar only archives files without compressing them, while gzip and bzip2 filters achieve quite high compression. Note that bzip2 typically achieves better compression that gzip, although it might take longer time to do so. Also note, that in this case gzip and bzip2 filters achieve quite high compression ratios because the dir directory contains text files. If the directory contained already compressed files, say f.e. binary images compressed using the JPEG compression, then neither gzip nor bzip2 could do much more in terms of compression.

Extracting archives

Extracting archives is also very simple. Instead of the c switch the x is used and the archive name is given as the only other parameter. The commands for archive extraction shown below correspond to the archive creation commands given earlier.

tar xf dir.tar  # ©2007 linux.dsplabs.com.autar xzf dir.tar.gz  # ©2007 linux.dsplabs.com.autar xjf dir.tar.bz2  # ©2007 linux.dsplabs.com.au

Infact, in most cases, tar will figure out what archive type you are trying to extract (from their hex headers I suppose), so that the filter specifications are not really needed. Hence, the following still works fine.

tar xf dir.tar  # ©2007 linux.dsplabs.com.autar xf dir.tar.gz  # ©2007 linux.dsplabs.com.autar xf dir.tar.bz2  # ©2007 linux.dsplabs.com.au

However, if you explicitly specify the decoder, then tar will assume that that is the encoding of the given archive. If for whatever reason that is not the case, an error will occur.

The verbose mode

The v switch can be used to enable the verbose mode. This can be useful if you would like to see a list of files being compressed or extracted. For example, lets extract the dir.tar.gz archive, with verbose mode enabled, using the following command.

tar xvzf dir.tar.gz  # ©2007 linux.dsplabs.com.au

The above command produces a list of inflated files as shown in the following output.

dir/dir/NVIDIA_DRIVER_README.txtdir/NVIDIA_LICENSE.txtdir/readme.txt

Some common errors

Lets take a look at some common error examples. As described previously, specifying incorrect filter type, f.e. using the following commands

tar xjf dir.tar  # ©2007 linux.dsplabs.com.autar xzf dir.tar.bz2  # ©2007 linux.dsplabs.com.au

results in the respective errors messages shown below.

bzip2: (stdin) is not a bzip2 file.tar: Child returned status 2tar: Error exit delayed from previous errorsgzip: stdin: not in gzip formattar: Child returned status 1tar: Error exit delayed from previous errors

Also, if a file becomes damaged, say truncated, then obviously an error will occur. Lets simulate such a corruption. Lets keep only the initial 100 bytes of the dir.tar.bz2 archive by using the following command.

head -c100 dir.tar.bz2 > corrup.tar.bz2  # ©2007 linux.dsplabs.com.au

If we now try to extract this archive,

tar xjf corrupt.tar.bz2  # ©2007 linux.dsplabs.com.au

then the following error message will be produced.

bzip2: Compressed file ends unexpectedly;        perhaps it is corrupted?  *Possible* reason follows.bzip2: Inappropriate ioctl for device        Input file = (stdin), output file = (stdout)It is possible that the compressed file(s) have become corrupted.You can use the -tvv option to test integrity of such files.You can use the `bzip2recover' program to attempt to recoverdata from undamaged sections of corrupted files.tar: Child returned status 2tar: Error exit delayed from previous errors

Interestingly, we are told to check archives integrity, so lets do that.

bzip2 -tvv corrupt.tar.bz2  # ©2007 linux.dsplabs.com.au

The bzip2 utility tells us what we already know… we are missing part of the file.

corrupt.tar.bz2:    [1: huff+mtf file ends unexpectedlyYou can use the `bzip2recover' program to attempt to recoverdata from undamaged sections of corrupted files.

Lets try the second suggestion, the bzip2recover utility.

bzip2recover corrupt.tar.bz2  # ©2007 linux.dsplabs.com.au

Unfortunately, with only 100 bytes it is not possible to recover anything from the corrupted archive.

bzip2recover 1.0.3: extracts blocks from damaged .bz2 files.bzip2recover: searching for block boundaries ...   block 1 runs from 80 to 800 (incomplete)bzip2recover: sorry, I couldn't find any block boundaries.

Similarly, if we truncate the gzip archvie, then gzip will inform us of unexpected end of file.

gzip: stdin: unexpected end of filetar: Child returned status 1tar: Error exit delayed from previous errors

On the other hand, truncating a tar archive does not cause error messages during extraction process. Obviously, only the files still fully contained (an not truncated/corrupted) in such an archive will be extracted. Such tuncation/corruption errors often occur when extracting archives downloaded from the Internet.


Did you find the above information useful and interesting? If so, please support this site by using the blog directory links at the bottom of this page. Thanks for your support!

If you have any Linux related problems or questions then please feel free to post them on our Linux Forums: http://linux.dsplabs.com.au/forums.

Comment



smilebig smileopen-mounthed smileconfused smilesad smileangry smiletonguequestionembarrassedsurprised smilewinkdouble winkcry

Tweet

You are infringing on the copyright of linux.dsplabs.com.au by blatantly copying the copyrighted material from that site. Remove it immediately.

#1 By linux.dsplabs.com.au (132.234.251.211) on 2009-02-03 21:48

F6Yjzn comment1 ,

#2 By lKLpWbGLYDoLoSc (71.13.25.204) on 2009-06-27 11:06

Incredible site!

#3 By EXPEcwyIE (67.246.245.173) on 2009-07-03 01:34

Great. Now i can say thank you!

#4 By GxaYLBRconAqOc (190.44.1.62) on 2009-07-03 03:03

Perfect work!

#5 By giTCuWTWyF (98.204.109.251) on 2009-07-03 04:38

Very interesting site. Hope it will always be alive!

#6 By naUnHqtKqx (72.198.78.229) on 2009-07-04 16:17

Great work, webmaster, nice design!

#7 By bxNPXhZKYOSdrhKa (216.105.183.190) on 2009-07-04 17:27

Very interesting site. Hope it will always be alive!

#8 By VNkqLBirymynYBrios (76.11.250.9) on 2009-07-04 18:38

It is the coolest site, keep so!

#9 By PVcOQaWNHGRUoS (77.250.88.52) on 2009-07-06 03:01

Beautiful site!

#10 By LVwfvRjwYoRLvW (24.78.158.52) on 2009-07-06 04:20

It is the coolest site, keep so!

#11 By ggWBnlWslygnHcDQlt (94.209.243.19) on 2009-07-06 05:38

I want to say - thank you for this!

#12 By ZKMUZPudoeXbBpQy (72.191.40.238) on 2009-07-06 14:35

Great site. Good info.

#13 By iwQtWDaDqMP (216.26.222.141) on 2009-07-06 15:53

Beautiful site!

#14 By XvYYqkNFXwYElNxnV (68.16.15.230) on 2009-07-06 17:11

Perfect work!

#15 By gbMlhcNq (68.144.25.83) on 2009-07-09 01:43

Incredible site!

#16 By rwGfuXQdC (200.109.130.239) on 2009-07-09 03:02

I bookmarked this link. Thank you for good job!

#17 By RbrJefJzNFmnZqyWlA (24.124.57.168) on 2009-07-09 05:01

Incredible site!

#18 By snadBwIbT (76.100.226.58) on 2009-07-10 12:00

It is the coolest site, keep so!

#19 By yxbzjjmlNAWShQNoDz (96.19.111.23) on 2009-07-10 13:57

I want to say - thank you for this!

#20 By DjFzMNcwsYZGLwk (67.247.143.161) on 2009-07-10 15:15

Beautiful site!

#21 By OslPcksTOGR (82.217.156.253) on 2009-07-10 16:35

Great site. Keep doing.

#22 By TwpPjbMVo (98.151.12.142) on 2009-07-10 18:47

Great site. Good info.

#23 By iODhKBhTQYWCbXPG (88.108.168.144) on 2009-07-10 20:07

Very interesting site. Hope it will always be alive!

#24 By ZqLTRCoYFCT (70.123.198.189) on 2009-07-10 21:29

Incredible site!

#25 By LOfCmpEiYLyBjuJ (81.165.185.20) on 2009-07-12 03:05

Great work, webmaster, nice design!

#26 By REEeSIozEjmzkzNTswC (76.31.74.49) on 2009-07-12 05:12

Great site. Keep doing.

#27 By nsuWTqdNOWdJrlZ (76.214.8.124) on 2009-07-12 07:22

WoGZKo

#28 By pOtEgrlEgEaxiip (69.249.226.67) on 2009-07-14 20:46

It is the coolest site, keep so!

#29 By aHoRpJTTSiOZvajmc (68.111.179.29) on 2009-07-23 01:03

If you have to do it, you might as well do it right.

#30 By AANliDZWIDjWxfpTF (66.190.177.104) on 2009-07-23 08:33

I want to say - thank you for this!

#31 By HlsXSozwDUER (69.145.153.115) on 2009-07-23 15:27

Beautiful site!

#32 By syfibuTu (206.255.11.73) on 2009-07-23 16:11

It is the coolest site, keep so!

#33 By FgkFBIvdvZQqaNEg (216.114.228.45) on 2009-07-23 23:49

Perfect site, i like it!

#34 By QGVoaeYkjiExoxOsbF (24.58.60.119) on 2009-07-24 07:35

Great site. Good info.

#35 By DMbWwAWlTedmMRboNbM (166.82.131.134) on 2009-07-24 15:21

Beautiful site!

#36 By fUjPWfUeFyn (75.22.33.154) on 2009-07-25 00:37

It is the coolest site, keep so!

#37 By cYyLaGvdrYBCoJFrMYY (76.194.142.117) on 2009-07-25 09:24

If you have to do it, you might as well do it right.

#38 By xeSCVYzAeaqLeK (70.128.114.249) on 2009-07-30 22:33

Excellent site. It was pleasant to me.

#39 By JbVivbrXlvanLFZAGR (98.25.10.146) on 2009-07-31 06:26

Perfect site, i like it!

#40 By BdfOAsTkYZMng (201.173.140.169) on 2009-07-31 14:25

Great work, webmaster, nice design!

#41 By knebLtVfZCdAfjKwB (24.145.30.220) on 2009-07-31 23:06

Great. Now i can say thank you!

#42 By GGsPTNfgoIEy (71.80.99.174) on 2009-08-01 16:27

Perfect site, i like it!

#43 By dGSdPOcmbUjHl (66.233.16.220) on 2009-08-02 01:12

NXNPZl I want to say - thank you for this!

#44 By pUIeUWwFwSAxNGTy (68.174.207.155) on 2009-08-03 01:00

chmZ0C Great work, webmaster, nice design!

#45 By nZjnkLzjbTx (66.168.156.230) on 2009-08-03 03:04

It is the coolest site, keep so!

#46 By RUKFANzqTvUNP (72.135.235.96) on 2009-08-03 17:53

Shut up linux.dsplabs.com.au its only infomation to help others- go figure.

#47 By hmm (58.106.26.3) on 2009-09-11 10:18