Archiving files

The tar program is used to create and manipulate tar archives. You can use tar on previously created archives to extract files, to store additional files, or to update or list files which were already stored. Initially, tar archives were used to store files on magnetic tape. The name comes from this use; it stands for tape archiver. Conventionally, tar archives are given names ending with '.tar'. Often, people refer to tar archives as "tar files," and archive members as "files". The three most frequently used operations are --create ('-c'), --list ('-t') and --extract ('-x'). Two frequently used options are --file ('-f') and --verbose ('-v'). To place the files apple, banana and orange into an archive named fruits.tar, use the following command.

$ tar --create --file=fruits.tar apple banana orange

If you include the --verbose option, tar will list the files it is working on.

$ tar --create --verbose --file=fruits.tar apple banana orange

Eventually, you will probably want to use short forms of options. Here is what the previous example looks like using short option forms.

$ tar -cvf fruits.tar apple banana orange

You can determine exactly what a particular archive contains.

$ tar --list --file=fruits.tar

To extract an entire archive, use:

$ tar -xvf fruits.tar

You can extract specific files from the archive file.

$ tar --extract --file=fruits.tar banana

Compressing files makes them smaller and storing it such that the file can be restored. Linux provides several tools to compress files, the most common is gzip. Each file is replaced by one with the extension '.gz'. By default, gzip keeps the original file name in the compressed file.

$ gzip banana

Compressed files can be restored to their original form using gunzip.

$ gunzip banana

There is another pair of commands that operate identically to gzip and gunzip. These are bzip2 and bunzip2. You can recognize these files because they have a '.bz' or '.bz2' extension instead of '.gz'. The standard Microsoft archiving utility is the zip file. It is well supported in Linux by the zip and unzip. The default mode of zip is to add files to an archive and compress it. The first argument in the command line is the name of the archive to be operated on. After that, is a list of files to be added.