Skip to main content Skip to navigation

3.1.6 Archiving files

An archive is a collection of files in which each file is labelled with its source. Archiving is useful for making backups, and also collecting files together - in particular if you're going to distribute this collection of files to others. Many free software packages can be downloaded as tar files. tar is a widely-used archiving tool in linux. Tar is usually invoked with operations and options . There are many operations and options in tar - here the most frequently used ones will be covered. Tar writes the archive to the device you specify on the command line. This device can be any block device, including the hard drive your filesystem resides on. You should refer to the man page or a linux textbook for a more in-depth discussion of this command.

The most frequently used operations and options are:

-c create a new archive
-x extract the files from an archive
-t list the contents of an archive
-r append files to the end of an archive
-f take the device name from the next argument on the command line
-v show the name of each file processed
-z also zip (compress) the data when creating the archive, or unzip when exctracting the archive

The syntax of the tar command is:

tar options/operations files you wish to archive


The user harry wishes to create a new archive of all files in the directory /home/harry/MP3s/. He will write this archive to the /tmp/ directory on his machine. He will call this archive mp3.tar. He achieves this this by typing
 tar -cvf /tmp/mp3.tar /home/harry/MP3s/ 
Now harry would like to check the contents of this tar file. To do this he types
 tar -tf /tmp/mp3.tar 
This will return a list of the files contained in this archive.
The user phillipa now copies this archive mp3.tar into her current working directory. She wishes to unpack this archive into her own filespace. She does this by typing
 tar -xvf mp3.tar 
Harry wants to compress a lot of text files that are in /home/harry/textfiles/ into one big file in /tmp/mytextfiles.tgz. He uses the -z option and types
cd /home/harry 
tar -czvf /tmp/mytextfiles.tgz textfiles
Note the usage of .tgz as the extension in combination with -z. This is to make it clear that the tar file is also zipped.Then he wants to unpack it in /tmp/textfiles
cd /tmp 
tar -xzvf /tmp/mytextfiles.tgz
The 'textfiles' directory is automatically created, because it is also stored in the archive.