How To Archive Files And Directories In Linux – Part 1

In our previous tutorial, we discussed how to compress and decompress files using gzip and bzip2 programs. In this tutorial, we are going to learn how to archive files in Linux. Aren’t archiving and compressing same? Some of you might often think these terms refers the same meaning. But, both are completely different. Archiving is the process of the combining multiple files and directories (same or different size) into one file. On the other hand, compressing is the process of reducing the size of of a file(s) or directory(s). Archiving is often used as part of the system backups or when moving the data from one system to another. Hope you understand the difference between archiving and compressing. Now, let us get into the topic.

Archive files and directories

The most common programs to archive files and directories are;

tar

zip

This is a big topic. So, I am going to publish this article in two parts. In the first part, we will see how to archive files and directories using Tar command.

Archive files and directories using Tar command

Tar is an Unix command which stands for Tape Archive. It is used to combine or store multiple files (same or different size) into a single file. There are 4 main operating modes in tar utility.

c – Create an archive from a file(s) or directory(s).

x – Extract an archive.

r – Append files to the end of an archive.

t – List the contents of the archive.

For complete list of modes, refer the man pages.

Create a new archive

For the purpose of this guide, I will be using a folder named ostechnix that contains three different type of files.

$ ls ostechnix/
file.odt image.png song.mp3

Now, let us create a new tar archive of the the directory ostechnix.

$ tar cf ostechnix.tar ostechnix/

Here, c flag refers create new archive and f refers the file name.

Similarly, to create an archive from a set of files in the current working directory, use this command:

$ tar cf archive.tar file1 file2 file 3

Extract archives

To extract an archive in the current directory, simply do:

$ tar xf ostechnix.tar

We can also extract the archive in a different directory using C flag(capital c). For example, the following command extracts the given archive file in Downloads directory.

$ tar xf ostechnix.tar -C Downloads/

Alternatively, go to the Downloads folder and extract the archive inside it like below.

$ cd Downloads/

$ tar xf ../ostechnix.tar

Some times you may want to extract files of a specific type. For example, the following command extracts the “.png” type files.

$ tar xf ostechnix.tar --wildcards "*.png"

Create gzipped and bzipped archives

By default, Tar creates the archive file ends with .tar. Also, tar command can be used in conjunction with the compression utilities gzip and bzip2. The files ends with .tar extensions refer the plain tar archive, the files ends with tar.gz or .tgz refers a gzipped archive, and the tar files ends with tar.bz2 or .tbz refers bzipped archive, respectively.

First, let us create a gzipped archive:

$ tar czf ostechnix.tar.gz ostechnix/

Or,

$ tar czf ostechnix.tgz ostechnix/

Here, we use z flag to compress the archive using gzip compression method.

Now, let us extract bzipped archive in the current directory. To do so, we do:

$ tar xjf ostechnix.tar.bz2

Or, extract the archive to some other directory:

$ tar xjf ostechnix.tar.bz2 -C Downloads

Create archive of multiple directories and/or files at a time

This is another coolest feature of tar command. To create an gzipped archive of multiple directories or files at once, use this command:

$ tar czvf ostechnix.tgz Downloads/ Documents/ ostechnix/file.odt

The above command will create an archive of Downloads, Documents directories and file.odt file in ostechnix directory and save the archive in the current working directory.

Exclude directories and/or files from while creating an archive

This is quite useful when backing up your data. You can exclude the non-important files or directories from your backup. This is where –exclude switch comes in help. For example, you want to create an archive of your /home directory, but exclude Downloads, Documents, Pictures, Music directories.

The above command will create an gzipped archive of my $HOME directory, excluding Downloads, Documents, Pictures and Music folders. To create bzipped archive, replace z with j and use the extension .bz2 in the above example.