[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

15. Creating Archives and Zipped Files

An archive is a single file holding a collection of other files in a structure that makes it possible to retrieve the original individual files (called "members" of the archive). The standard tool for creating and extracting archives in Linux is named 'tar', and it is commonly used in conjunction with a compression tool named 'gzip'. A Compressed Archive is an archive which has been compressed (zipped), similar to the compressed archives made by PKZIP or Winzip under Windows.

Why do you need an archive? Archives/compressed archives are useful for taking backups and distributing files over the Internet.

In this chapter we will see how to create and extract an archive and a compressed archive.

15.1 Creating Archives  
15.3 Extracting Files from Archives  
15.8 More on Archives  


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

15.1 Creating Archives

Tar is used to make archives--it's like PKZIP or Winzip. You have to run the tar command from a terminal emulator as in these examples:
To make a new archive:
tar -cvf <archive_name.tar> <file> [file...]
For example you are in the directory `/home/raj/temp'. This has three files `barney.txt,betty.txt,fred.txt' and you want to create an archive of these files. Here is how you will do it:
tar -cvf flints.tar barney.txt fred.txt betty.txt


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

15.2 Creating Compressed Archives

Using tar with the 'z' option compresses the files with gzip and creates a compressed archive (or tarball); this is probably the most common use for tar. For example:
tar -czvf flints.tgz barney.txt fred.txt betty.txt
will create a compressed archive named `flints.tgz'.
Note that you can use the tar command on a folder or folders as well as individual files. For example, if I wanted to create a compressed archive containing the folder `/home/raj/rubble/' and all its contents, I would do this (from the directory holding the folder I want to compress/archive):
tar -czvf rubble/


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

15.3 Extracting Files from Archives

To extract files from an archive use:
tar -xvf <archive_name.tar> [filename.tar...]

For example to extract all the files of the archive `flints.tar' (created in the previous section)
tar -xvf flints.tar
This will extract the files `fred.txt barney.txt betty.txt'

To extract only `fred.txt'
`tar -xvf flints.tar fred.txt'
This will extract the files `fred.txt'


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

15.4 Extracting Compressed Archives (Tarballs)

You can use the 'z' option to extract compressed archives - these are the most common and have .tgz or .tar.gz filename extensions. For example:
tar -zxvf flints.tgz


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

15.5 Viewing Archive Contents

What if you only want to view the contents of the archive witout extracting them.
tar -tf <file.tar.gz>
lets you do that.

For example to view the members of the archive `flints.tar'
tar -tf flints.tar
This will show you the following
fred.txt
barney.txt
betty.txt


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

15.6 Compressing Files with gzip

Gzip reduces the size of the files. Whenever possible, each file is replaced by one with the extension .gz. Though there are other file compressing utilities like compress, files compressed using gzip are most common.

The command gzip <file> compresses the file. For example if I have two files `/home/crypto/temp/foo.txt' and `/home/crypto/temp/bar.txt'. Here is how to compress them
gzip /home/crypto/temp/foo.txt /home/crypto/temp/bar.txt
If you are in the directory `/home/crypto/temp/' you do not have to specify the full path to the file.

The files are replaced with one with the extension .gz. For example, fter running the above example you will find `bar.txt.gz, foo.txt.gz' innstead of `bat.txt' and `foo.txt'.

Gzip takes following command line options (among other)

-v
Verbose. Display the name and percentage reduction for each file compressed.
-h
Print an informative help message describing the options then quit.

You might have noticed that gzip does not lump the files together. Each file is compressed separately. What if you want all your files in one single archive? Just use the 'z' option with tar as mentioned earlier.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

15.7 Zip and Unzip

Compressed archives in the Linux world are mainly of the tgz or tarball type, while Windows users are probably more familiar with the `zip' format. Despite this, it's quite easy to share archives with Windows users. The tool for unzipping or expanding zip files has the logical name of `unzip', and it's installed by default with most distros. Unzip has a few options (see `man zip'). but usually it's just a matter of using unzip filename.zip. For example, assuming I have a file named `betty.zip' into my home directory. I'd use this to unzip it:
unzip betty.zip
You can also create zip files under Linux with a tool named `zip'. Unlike unzip, zip isn't always installed by default, though you should be able to find the zip package on your installation CDs. Zip has quite a few options (see `man zip'), but usually you'll only need to use zip filename file1 file2 file3 etc.. For example, if I wanted to create a zip archive named `rock.zip' that contained the files `dino.txt bambam.txt wilma.txt', I'd do this:
zip rock dino.txt bambam.txt wilma.txt
Note that zip will add the .zip extension to the filename. Zip can compress folders and their contents, eg:
zip myzip myfiles/*
It will also compress folders recursively with the '-r' option. Files created with zip are compatible with Windows tools such as WinZip and PKZIP, and files created with WinZip/PKZIP etc can be expanded with unzip. I should also mention that reasonably recent versions of WinZip etc can unpack .tgz archives, though if you are making archives for Windows users you might want to stick with the zip format to avoid confusion.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

15.8 More on Archives


[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated on March, 5 2003 using texi2html
Hosted by www.Geocities.ws

1