tar command

The tar command examples

The tar (Tape ARchive) tool is the most used archiving tool in Linux world. We already covered some of the compressing tools like zip/unzip, gzip/gunzip, and bzip examples which most of the time accompany this tar command. In this post, we will see on how to use tar command with simple examples.

Note: TAR is not an compressing tool, but use above mention compressing tools to archive that.

Syntax for Linux tar command

tar [options] [TarName] [File/Directory]

Tar Usage with Examples

The tar command gets its name from the fact that it stores and extracts files from tape/disk archives. The tar creates the tarball with .tar extension used to take backup of the files/directories on to a tape archive by default. This just convert all the files and folders into single tar archive for easy storage and transfer. Let us see some examples of ‘tar’ usage to understand it better.

Note:tar’ by default produces the uncompressed tar-ball with only ‘.tar’ extension to a file.

Example 1: Create a tape archive.  Create a basic tape archive from the tar command. With below example we are going to club all the files/folders in default folder.

[email protected]:~$ ls
 default
 [email protected]:~$ tar -cvf default.tar default/
 default/
 default/bootlogd
 default/nss
 default/whoopsie
 default/rsync
 default/ssh
 default/puppetqd
 default/cron
 default/rsyslog
 default/apache2
 default/grub
 default/rcS
 default/locale
 default/ufw
 [email protected]:~$ ls
 default default.tar

We archived the default directory in the above example, options cvf are used here, where
c — to create an archive
v — verbose to show the files that are being processed.
f — to mention the name of archive file.

Example 2: To create the compressed tape archive while we archive the file/directory. We use the ‘z’ option with tar, in order to gunzip the tar file.

 [email protected]:~$ tar -cvzf default.tar.gz default/
 default/
 default/bootlogd
 default/nss
 default/whoopsie
 default/rsync
 default/ssh
 default/puppetqd
 default/cron
 default/rsyslog
 default/apache2
 default/grub
 default/rcS
 default/locale
 default/ufw
 [email protected]:~$ ls
 default default.tar.gz

Note: We need to mention the tar filename along with it’s extension in the command. In the above example we mentioned the extension ‘.tar.gz’ in order to produce the zipped tar.
Also .tgz and .tar.gz are same.

Example 3: We can also filter the archive through bzip2. This is achieved by using ‘-j’ option.

[email protected]:~$ tar -cvjf def.tbz default/
 default/
 default/bootlogd
 default/nss
 default/testedfile
 default/whoopsie
 default/rsync
 default/ssh
 default/puppetqd
 default/cron
 default/rsyslog
 default/apache2
 default/grub
 default/rcS
 default/locale
 default/ufw
 default/test/
 default/test/rcS
 default/test/locale
 default/test/ufw

Example 4: In many cases, it’s worth to list the contents of a tar, before we actually completely extract it. Here we use the option ‘t’ to do it.

 [email protected]:~$ tar tvf default.tar.gz
 drwxr-xr-x sujit/sujit 0 2016-04-18 10:38 default/
 -rw-r--r-- sujit/sujit 47 2016-03-27 13:45 default/bootlogd
 -rw-r--r-- sujit/sujit 1756 2016-03-27 13:45 default/nss
 -rw-r--r-- sujit/sujit 31 2016-03-27 13:45 default/whoopsie
 -rw-r--r-- sujit/sujit 1768 2016-03-27 13:45 default/rsync
 -rw-r--r-- sujit/sujit 133 2016-03-27 13:45 default/ssh
 -rw-r--r-- sujit/sujit 875 2016-03-27 13:45 default/puppetqd
 -rw-r--r-- sujit/sujit 183 2016-03-27 13:45 default/cron
 -rw-r--r-- sujit/sujit 321 2016-03-27 13:45 default/rsyslog
 -rw-r--r-- sujit/sujit 637 2016-03-27 13:45 default/apache2
 -rw-r--r-- sujit/sujit 1225 2016-03-27 13:45 default/grub
 -rw-r--r-- sujit/sujit 686 2016-03-27 13:45 default/rcS
 -rw-r--r-- sujit/sujit 19 2016-03-27 13:45 default/locale
 -rw-r--r-- sujit/sujit 2072 2016-03-27 13:45 default/ufw

This gives us all the information about the files/directories within the tarball default.tar.gz

Related concept:   Redis DB Install and Few Commands

Example 5: It’s generally a case that we create the tarball of a directory, and when the directory is updated with newer files or modification to the older files, we would require to create the tar out of it again.
Instead of recreating it, we can use ‘u’ –update option to add the newer/modified files to the tarball.

[email protected]:~$ tar -uvf default.tar default/
 default/nss
 default/testedfile
 default/rsync
 [email protected]:~$

Example 6: To append new files at the end of an existing tarball. Here we use the ‘-r’ option to achieve this. This is bit different to updating, as updating will take already exiting files in tar which are updated after we archive them.

[email protected]:~$ tar tvf arch.tar
 -rw-rw-r-- sujit/sujit 0 2016-04-19 18:38 file1
 -rw-rw-r-- sujit/sujit 0 2016-04-19 18:38 file2
 -rw-rw-r-- sujit/sujit 0 2016-04-19 18:38 file3
 [email protected]:~$ tar rvf arch.tar test test2
 test
 test2
 [email protected]:~$ tar tvf arch.tar
 -rw-rw-r-- sujit/sujit 0 2016-04-19 18:38 file1
 -rw-rw-r-- sujit/sujit 0 2016-04-19 18:38 file2
 -rw-rw-r-- sujit/sujit 0 2016-04-19 18:38 file3
 -rw-rw-r-- sujit/sujit 29 2016-04-19 17:22 test
 -rw-rw-r-- sujit/sujit 65 2016-04-19 17:29 test2
 [email protected]:~$

Example 7: Comparison of the created tarball with it corresponding filesystem. This is handled by the diff/compare option.

[email protected]:~$ tar -dvf default.tar default/*
 default/bootlogd
 default/nss
 default/nss: Mod time differs
 default/nss: Size differs
 default/whoopsie
 default/rsync
 default/rsync: Mod time differs
 default/rsync: Size differs
 default/ssh
 default/puppetqd
 default/nss
 default/testedfile
 default/rsync

We have seen variations in the creation/modification of the tarball. Let’s see how we can extract the tarball

Related concept:   10 File Globbing examples in Linux/Unix

Example 8: As we used ‘-c’ option to create the archive, instead of that we will use ‘-x’ extract option to untar the tarball.

[email protected]:~$ tar xvf default.tar
 default/
 default/bootlogd
 default/nss
 default/whoopsie
 default/rsync
 default/ssh
 default/puppetqd
 default/test/rcS
 default/test/locale
 default/test/ufw
 default/nss
 default/testedfile
 default/rsync

Note: By default tar will extract the contents from the tarball into the current directory.

Example 9: A general case that we want to extract the tar into a different directory other than the one it is placed in. This is achieved using the ‘-C’ option.
Note: This ‘C’ is the capital c which stands for change directory, and it’s usage in the command also is different. Do not confuse with the create option for tar.

[email protected]:~$ mkdir Main
 [email protected]:~$ tar xvf default.tar -C Main/
 default/
 default/bootlogd
 default/nss
 default/whoopsie
 default/rsync
 default/ssh
 default/test/locale
 default/test/ufw
 default/nss
 default/testedfile
 default/rsync
 [email protected]:~$ ls
 default default.tar default.tar.gz def.tar.bz def.tbz Main
 [email protected]:~$ cd Main/
 [email protected]:~/Main$ ls
 default

Example 10: Overwrite of the existing files with the extracted files from the tarball is the default functionality of the tar. But this can be restricted using “-k” option.

[email protected]:~$ tar -xkvf /home/sujit/default.tar

Though the verbose does not show any difference in the information, the directory listing would definitely make out that the files are not overwritten.
Before the command execution :

[email protected]:~$ ls -lrt
 total 20
 -rw-r--r-- 1 sujit sujit 1758 Apr 19 17:50 nss
 -rw-r--r-- 1 sujit sujit 87 Apr 19 17:50 halt
 -rw-r--r-- 1 sujit sujit 185 Apr 19 17:50 cron

After the command Execution :

[email protected]:~/default$ ls -lrt
 total 80
 -rw-r--r-- 1 sujit sujit 31 Mar 27 13:45 whoopsie
 -rw-r--r-- 1 sujit sujit 2072 Mar 27 13:45 ufw
 -rw-r--r-- 1 sujit sujit 1960 Mar 27 13:45 tomcat7
 -rw-r--r-- 1 sujit sujit 133 Mar 27 13:45 ssh
 -rw-rw-r-- 1 sujit sujit 637 Mar 27 14:36 -testfile
 drwxrwxr-x 2 sujit sujit 4096 Apr 19 09:50 test
 -rw-rw-r-- 1 sujit sujit 28 Apr 19 09:58 testedfile
 -rw-r--r-- 1 sujit sujit 1758 Apr 19 17:50 nss
 -rw-r--r-- 1 sujit sujit 87 Apr 19 17:50 halt
 -rw-r--r-- 1 sujit sujit 185 Apr 19 17:50 cron

Example 11: To Append one tar file with another archive (technically called as concatenate), we use ‘-A’ option.

[email protected]:~$ tar tvf archive.tar
 -rw-rw-r-- sujit/sujit 0 2016-04-19 18:38 file1
 -rw-rw-r-- sujit/sujit 0 2016-04-19 18:38 file2
 -rw-rw-r-- sujit/sujit 0 2016-04-19 18:38 file3
 [email protected]:~$ tar -Af archive.tar default.tar
 [email protected]:~$ tar tvf archive.tar
 -rw-rw-r-- sujit/sujit 0 2016-04-19 18:38 file1
 -rw-rw-r-- sujit/sujit 0 2016-04-19 18:38 file2
 -rw-rw-r-- sujit/sujit 0 2016-04-19 18:38 file3
 drwxr-xr-x sujit/sujit 0 2016-04-19 09:50 default/
 -rw-r--r-- sujit/sujit 47 2016-03-27 13:45 default/bootlogd
 -rw-r--r-- sujit/sujit 1756 2016-03-27 13:45 default/nss
 -rw-r--r-- sujit/sujit 31 2016-03-27 13:45 default/whoopsie
 -rw-r--r-- sujit/sujit 1768 2016-03-27 13:45 default/rsync
 -rw-r--r-- sujit/sujit 133 2016-03-27 13:45 default/ssh
 -rw-r--r-- sujit/sujit 875 2016-03-27 13:45 default/puppetqd
 -rw-r--r-- sujit/sujit 183 2016-03-27 13:45 default/cron
 -rw-r--r-- sujit/sujit 321 2016-03-27 13:45 default/rsyslog
 -rw-r--r-- sujit/sujit 637 2016-03-27 13:45 default/apache2
 -rw-r--r-- sujit/sujit 1225 2016-03-27 13:45 default/grub
 -rw-r--r-- sujit/sujit 686 2016-03-27 13:45 default/rcS
 -rw-r--r-- sujit/sujit 19 2016-03-27 13:45 default/locale
 -rw-r--r-- sujit/sujit 2072 2016-03-27 13:45 default/ufw
 drwxrwxr-x sujit/sujit 0 2016-04-19 09:50 default/test/
 -rw-r--r-- sujit/sujit 686 2016-04-19 09:50 default/test/rcS
 -rw-r--r-- sujit/sujit 19 2016-04-19 09:50 default/test/locale
 -rw-r--r-- sujit/sujit 2072 2016-04-19 09:50 default/test/ufw
 -rw-r--r-- sujit/sujit 1757 2016-04-19 09:57 default/nss
 -rw-rw-r-- sujit/sujit 28 2016-04-19 09:58 default/testedfile
 -rw-r--r-- sujit/sujit 1769 2016-04-19 09:57 default/rsync

Example 12: To remove the files after adding to the archive. We make use of “–remove-files” option to do it.

[email protected]:~$ ls
 archive.tar default default.tar default.tar.gz def.tar.bz def.tbz file1 file2 file3 Main test test2
 [email protected]:~$ tar cvf arch.tar --remove-files file1 file2 file3
 file1
 file2
 file3
 [email protected]:~$ ls
 archive.tar arch.tar default default.tar default.tar.gz def.tar.bz def.tbz Main test test2
 [email protected]:~$

Here we have covered most of use cases of tar that we usually need. We always have the scope of creating new examples with new experiences encountered.