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.
sujit@linuxnix.com:~$ ls default sujit@linuxnix.com:~$ 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 sujit@linuxnix.com:~$ 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.
sujit@linuxnix.com:~$ 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 sujit@linuxnix.com:~$ 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.
sujit@linuxnix.com:~$ 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.
sujit@linuxnix.com:~$ 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
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.
sujit@linuxnix.com:~$ tar -uvf default.tar default/ default/nss default/testedfile default/rsync sujit@linuxnix.com:~$
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.
sujit@linuxnix.com:~$ 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 sujit@linuxnix.com:~$ tar rvf arch.tar test test2 test test2 sujit@linuxnix.com:~$ 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 sujit@linuxnix.com:~$
Example 7: Comparison of the created tarball with it corresponding filesystem. This is handled by the diff/compare option.
sujit@linuxnix.com:~$ 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
Example 8: As we used ‘-c’ option to create the archive, instead of that we will use ‘-x’ extract option to untar the tarball.
sujit@linuxnix.com:~$ 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.
sujit@linuxnix.com:~$ mkdir Main sujit@linuxnix.com:~$ 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 sujit@linuxnix.com:~$ ls default default.tar default.tar.gz def.tar.bz def.tbz Main sujit@linuxnix.com:~$ cd Main/ sujit@linuxnix.com:~/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.
sujit@linuxnix.com:~$ 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 :
sujit@linuxnix.com:~$ 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 :
sujit@linuxnix.com:~/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.
sujit@linuxnix.com:~$ 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 sujit@linuxnix.com:~$ tar -Af archive.tar default.tar sujit@linuxnix.com:~$ 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.
sujit@linuxnix.com:~$ ls archive.tar default default.tar default.tar.gz def.tar.bz def.tbz file1 file2 file3 Main test test2 sujit@linuxnix.com:~$ tar cvf arch.tar --remove-files file1 file2 file3 file1 file2 file3 sujit@linuxnix.com:~$ ls archive.tar arch.tar default default.tar default.tar.gz def.tar.bz def.tbz Main test test2 sujit@linuxnix.com:~$
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.
author_sujit
Latest posts by author_sujit (see all)
- 12 tar command examples in Linux/Unix - September 26, 2017
- 9 bzip and bunzip command examples in Linux/Unix - September 22, 2017
- 12 rsync commands examples in Linux/Unix - September 22, 2017