Linux tar command - How to archive, compress and extract files in Linux

There are many useful open source programs in the Internet such as network security tools, Internet security tools and multimedia software available for Linux. Those programs available in a package compiled for certain Linux distribution and some package only available in source. Examples of software package files are rpm for Redhat and .deb for Debian while a source file, normally in '.tar.gz' type is for all Linux distributions. The '.tar.gz' file is a tar archived file and has been compressed with gunzip program. You'd probably seen those kind of files when you download security patches for your Linux system. Don't worry if this is the first time you've heard of it.

Linux tar program is a files archiving and compressing tool that came standard with default installation. That means when you successfully installed Linux (including Slackware Linux and Ubuntu server), you already have tar program in your system. This tutorial is a basic guide on how to use Linux tar command to create a tar archived file and compress it. The tutorial would not be completed unless user knows how to uncompressed and unarchived the tar file again.

Linux tar command

The name 'tar' is an abbreviation of 'tape archiver'. It is the most popular archiving program in the UNIX operating systems. You can use Linux tar program to create archives, to add more files to an archive file, to extract tar archived file and many more.

Type 'man tar' in the Linux command line terminal to see the full GNU tar manual page. The table below shows some options available to be used with the Linux tar command:

FUNCTION LETTERS
One of the following options must be used:


-A, --catenate, --concatenate
append tar files to an archive

-c, --create
create a new archive


-d, --diff, --compare
find differences between archive and file system


--delete
delete from the archive (not for use on mag tapes!)


-r, --append
append files to the end of an archive


-t, --list
list the contents of an archive


-u, --update
only append files that are newer than copy in
archive


-x, --extract, --get
extract files from an archive


-z, --gzip, --ungzip
filter the archive through gzip

You can find more options in full tar manual page. You can do that later if you want. Now we are going to look at how to archive files in Linux system using Linux tar command.

How to archive files using Linux tar command

An archive file means keeping many files and directories in a single file. We can create a Linux archive file easily using tar command. If you haven't create a tar file before, let's make your first archive file using Linux tar command's guide below.

We need to know the tar command syntax before start using the command. Here is a Linux tar command syntax (or the command format):

tar ( create | extract ) <destination file> <source files or directories>

Here is a tar command syntax to create an archive file:

tar -cvf <filename.tar> <file1> <file2> ...

The options:

  • c = create a new archive.
  • v = verbosely list files processed.
  • f = file, use archive file.

filename.tar is the output name.

file1 file2 ... are files (sources) that you want to archive.

Here is a step by step guide on how to create a Linux tar archive file. Normally, we put all files that we are going to archive into a dedicated directory. In this example, all files has been put in the tutorial directory.

[email protected]:~/tutorial$ ls -l
total 284
-rw-r--r-- 1 luzar users 681 2009-03-04 07:38 named.conf
-rw-r--r-- 1 luzar users 101810 2009-03-04 06:58 tar_command01.png
-rw-r--r-- 1 luzar users 119211 2009-03-04 07:25 tar_command02.png
-rw-r--r-- 1 luzar users 18118 2009-03-04 07:27 tar_example01.png
-rw-r--r-- 1 luzar users 27157 2009-03-04 07:26 tar_example02.png
-rw-r--r-- 1 luzar users 153 2009-03-04 07:37 vitest
[email protected]:~/tutorial$

Now we can archive all the files in the tutorial directory by simply issuing the tar command, provide the filename.tar as the resulted file name and selecting all files with * wildcard. See how to create an archive file using Linux tar command example below:

[email protected]:~/tutorial$ tar -cvf tar-tutorial.tar *
named.conf
tar_command01.png
tar_command02.png
tar_example01.png
tar_example02.png
vitest
[email protected]:~/tutorial$

In the example above, we run the tar command in the same directory that we want to archive all files. It doesn't matter, you can run tar command from anywhere in the Linux system. If all the files that we are going to archive are in one directory as in the example above, we can also use the directory name that contains all the files instead of the * wildcard. See another way on how to run the Linux tar command:

[email protected]:~/tutorial$ cd ..
[email protected]:~$ tar -cvf tar-tutorial.tar tutorial/
tutorial/
tutorial/tar_example01.png
tutorial/tar_command01.png
tutorial/tar-tutorial.tar
tutorial/vitest
tutorial/named.conf
tutorial/tar_command02.png
tutorial/tar_example02.png
[email protected]:~$

We just move to one upper directory and run the tar command again. This time we mention the directory name. Put full path if you are running tar command from other directory. We called this tar with absolute path. When using 'absolute path', all files will be restored back to their respective directories when extracting the tar file.

If we want the tar file to be extracted to anywhere in the system, we can use the same option as in the first example. Another way is by creating a 'relative file' tar archive file. The tar command syntax is quite different from the normal tar syntax. See example below:

[email protected]:/home/luzar# tar -cvf /var/backup/basicconfig.tar -C basicconfig/ .

The different is, when creating a 'relative file names' tar archive file, '-C' is included before source and a '.' after the source.

Now let's check the result whether the new tar file, tar-tutorial.tar exist:

[email protected]:~$ ls -l tutorial
total 560
-rw-r--r-- 1 luzar users 681 2009-03-04 07:38 named.conf
-rw-r--r-- 1 luzar users 101810 2009-03-04 06:58 tar_command01.png
-rw-r--r-- 1 luzar users 119211 2009-03-04 07:25 tar_command02.png
-rw-r--r-- 1 luzar users 18118 2009-03-04 07:27 tar_example01.png
-rw-r--r-- 1 luzar users 27157 2009-03-04 07:26 tar_example02.png
-rw-r--r-- 1 luzar users 276480 2009-03-17 20:24 tar-tutorial.tar
-rw-r--r-- 1 luzar users 153 2009-03-04 07:37 vitest
[email protected]:~$

We got the result of the new created tar archived file there in blue-coloured font (not blue in reality!). Now let's check the content of the Linux tar archived file we've just created. The option use to list the content of a Linux tar file is -t.

[email protected]:~$ tar -tvf tar-tutorial.tar
drwxr-xr-x luzar/users 0 2009-03-17 20:44 tutorial/
-rw-r--r-- luzar/users 18118 2009-03-04 07:27 tutorial/tar_example01.png
-rw-r--r-- luzar/users 101810 2009-03-04 06:58 tutorial/tar_command01.png
-rw-r--r-- luzar/users 276480 2009-03-17 20:44 tutorial/tar-tutorial.tar
-rw-r--r-- luzar/users 153 2009-03-04 07:37 tutorial/vitest
-rw-r--r-- luzar/users 681 2009-03-04 07:38 tutorial/named.conf
-rw-r--r-- luzar/users 119211 2009-03-04 07:25 tutorial/tar_command02.png
-rw-r--r-- luzar/users 27157 2009-03-04 07:26 tutorial/tar_example02.png
[email protected]:~$

So we've got all files successfully archived using Linux tar command. Good. We can continue to the next topic, how to compress file using Linux tar command.

How to compress file using Linux tar command

The Linux tar command cannot be used to compress file but the gunzip command can. So we use Linux tar command with gunzip to compress file in Linux. To use gunzip command with tar, we just need to add -z option and change the result file name to '.tar.gz'. The Linux tar command with gunzip syntax used to compress a tar archived file is:

tar -zcvf filename.tar.gz file1 file2 ...

Here is an example on how to create a Linux tar archived file compressed with gunzip:

[email protected]:~$ tar -zcvf tar-tutorial.tar.gz tutorial/
tutorial/
tutorial/tar_example01.png
tutorial/tar_command01.png
tutorial/tar-tutorial.tar
tutorial/vitest
tutorial/named.conf
tutorial/tar_command02.png
tutorial/tar_example02.png

See the difference in size between the normal tar archived file and the tar compress gunzip file:

[email protected]:~$ ls -l | grep tar-tutorial.tar
-rw-r--r-- 1 luzar users 552960 2009-03-17 21:33 tar-tutorial.tar
-rw-r--r-- 1 luzar users 502992 2009-03-17 21:39 tar-tutorial.tar.gz
[email protected]:~$

We can also compress a tar archived file with gunzip. See example below:

[email protected]:~$ tar -zcvf tar-example.tar.gz tar-tutorial.tar/
tutorial/
tutorial/tar_example01.png
tutorial/tar_command01.png
tutorial/tar-tutorial.tar
tutorial/vitest
tutorial/named.conf
tutorial/tar_command02.png
tutorial/tar_example02.png

So far so good, now let's move to the final topic in the Linux tar tutorial, how to extract a tar archived file and a gunzip compressed file below.

How to extract tar file in Linux

This is the most important topic in the Linux tar command tutorial. Not all Linux users need to create an archive file but most users need to extract tar file and unzip a tar compressed Linux file. Here is a tar syntax or a format to extract a tar archived file:

tar -xvf <filename.tar>

The options:

  • x = extract files from an archive
  • v = verbosely list files processed
  • f = file, use archive file

'filename.tar' is the name of the tar archive file.

Let's see some examples on how to extract tar file in real situation in the Linux system. First, create a new directory to put all the files we are going to extract. Change into the new directory and extract the tar file. You have to provide the full path to the tar file. See step by step example below:

[email protected]:~$ cd result/
[email protected]:~/result$ mkdir tar
[email protected]:~/result$ cd tar
[email protected]:~/result/tar$ tar -xvf /home/luzar/tar-tutorial.tar
tutorial/
tutorial/tar_example01.png
tutorial/tar_command01.png
tutorial/tar-tutorial.tar
tutorial/vitest
tutorial/named.conf
tutorial/tar_command02.png
tutorial/tar_example02.png
[email protected]:~/result/tar$

Here is the result of the extracted files:

[email protected]:~/result/tar$ ls
tutorial/
[email protected]:~/result/tar$ ls -l tutorial/
total 560
-rw-r--r-- 1 luzar users 681 2009-03-04 07:38 named.conf
-rw-r--r-- 1 luzar users 276480 2009-03-17 20:44 tar-tutorial.tar
-rw-r--r-- 1 luzar users 101810 2009-03-04 06:58 tar_command01.png
-rw-r--r-- 1 luzar users 119211 2009-03-04 07:25 tar_command02.png
-rw-r--r-- 1 luzar users 18118 2009-03-04 07:27 tar_example01.png
-rw-r--r-- 1 luzar users 27157 2009-03-04 07:26 tar_example02.png
-rw-r--r-- 1 luzar users 153 2009-03-04 07:37 vitest
[email protected]:~/result/tar$

This is the Linux tar command's syntax to extract gunzip compressed tar file:

tar zxvf <filename.tar.gz>

The options:

  • z = filter the archive through gzip
  • x = extract files from an archive
  • v = verbosely list files processed
  • f = file, use archive file

'filename.tar.gz' is the name of the package.

This is an example on how to extract a gunzip compressed tar file:

[email protected]:~/result/tar$ cd ..
[email protected]:~/result$ mkdir gunzip
[email protected]:~/result$ cd gunzip/
[email protected]:~/result/gunzip$ tar -zxvf /home/luzar/tar-tutorial.tar.gz
tutorial/
tutorial/tar_example01.png
tutorial/tar_command01.png
tutorial/tar-tutorial.tar
tutorial/vitest
tutorial/named.conf
tutorial/tar_command02.png
tutorial/tar_example02.png
[email protected]:~/result/gunzip$

Here is the result of the gunzip extracted file:

[email protected]:~/result/gunzip$ ls tutorial/
named.conf tar_command01.png tar_example01.png vitest
tar-tutorial.tar tar_command02.png tar_example02.png
[email protected]:~/result/gunzip$

That cover almost all the basics of Linux tar command to archive and compress files using gunzip. Learning Linux is a continuous process. Good luck and enjoy!

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.