How to use tar command

Tar (tape archive ) is the most widely used command in Unix like operating system for creating archive of multiple files and folders into a single archive file and that archive file can be further compressed using gzip and bzip2 techniques. In other words we can say that tar command is used to take backup by archiving multiple files and directory into a single tar or archive file and later on files & directories can be extracted from the tar compressed file.
In this tutorial we will going to review and discuss various tar command examples including how to create archive files using tar, tar.gz and tar.bz2 compression, how to extract archive file, extract a single file, view content of archive file, verify a file, add files or directories to archive file, estimate the size of tar archive file, etc.

Using the tar command

The tar command is available by default on most linux systems and you do not need to install it separately.
Syntax of tar command:

# tar [options][files]

Some of the commonly used options in tar command are listed below :

--delete //delete from the archive(not on mag tapes!)
-r, --append //append files to the end of an archive
-r, --list //list the contents of an archive
--test-label //test the archive volume label and exit
-u, --update //only appened files newer than copy in archive
-x, --extract, --get //extract files from an archive
-C, --directory=DIR //change to directory DIR
-f, --file=ARCHIVE //use archive file or device ARCHIVE
-j, --bzip2 //filter the archive trough bzip2
-J, --xz //filter the archive trough xz
-p, --preserve-permissions //extract information about file permissions
-v, --verbose //verbosely list files processed 
-z, --gzip //filter the archive trough gzip

1. Create tar archive file

The syntax is as follows to create a tar file:

$ tar -cvf lintut.tar lintut/

-c : Create a tar ball.
-v : Verbose output (show progress).
-f : Output tar ball archive file name.

Create tar archive file
Create tar archive file

2. List the contents of tar archive file

Using –t option in tar command we can view the contents of tar files without extracting it.

$ tar -tvf lintut.tar 
drwxr-xr-x rasho/rasho       0 2017-01-07 14:22 lintut/
-rw-r--r-- rasho/rasho       0 2017-01-07 14:22 lintut/file1
-rw-r--r-- rasho/rasho       0 2017-01-07 14:22 lintut/file4
-rw-r--r-- rasho/rasho       0 2017-01-07 14:22 lintut/file2
-rw-r--r-- rasho/rasho       0 2017-01-07 14:22 lintut/file3
-rw-r--r-- rasho/rasho       0 2017-01-07 14:22 lintut/file5

3. Creating and compressing tar file(tar.gz or .tgz)

To create a compressed gzip archive file we use the -z option.For example, create a tar file of /var/log folder. Extensions of such tar files will be either tar.gz or .tgz.

$ tar -cvzf log.tgz /var/log
$ tar -cvzf log.tar.gz /var/log

4. Creating and compressing tar file(tar.bz2 or .tbz2)

Let’s assume that we want to create compressed (bzip2) tar file of /var/log folder. This can be achieved by using the option -j in the tar command. Extensions of such tar files will be either tar.bz2 or .tbz

$ tar -jcpvf log.tar.bz2 /var/log
$ tar -jcpvf log.tbz2 /var/log

5. Exctract tar archive file

-x option is used to extract the files and directories from the tar file. Let’s extract the content of above created tar file.

$ tar -xvf lintut.tar

This command will extract all the files and directories of lintut tar file in the current working directory.

6. Exctract tar archive to a particular folder

In case you want to extract tar file to a particular folder or directory then use -C option and after that specify the path of a folder.

$ tar -xvf lintut.tar -C /tmp/

7. Extracting or unzip tar.gz or .tgz files

tar files with extension tar.gz or .tgz is extracted or unzipped with option -x and -z. Example is shown below:

$ tar -zxpvf log.tgz -C /tmp/

8. Extracting or unzip tar.bz2 or .tbz2 files

tar files with the extension tar.bz2 or .tbz2 is extract with option -j and -x. Example is shown below:

$ tar -jxpvf archivefile.tbz2 -C /tmp/

9. View the size of .tar , .tgz and .tbz2 file

Use the below commands to view the size of tar and compressed tar files.

$ tar -czf - archivefile.tar | wc -c
$ tar -czf - archivefile.tgz | wc -c
$ tar -czf - archivefile.tbz2 | wc -c


$ tar -czf - log.tar.gz | wc -c

10. Splitting big tar file into smaller files

In Unix like operating big file is divided or split into smaller files using split command. Big tar file can also be divided into the smaller parts using split command.
Let’s assume we want to split “log.tgz” file into smaller parts of each 1 MB.

Syntax:  split -b  . “prefix-name”
$ split -b 1M log.tar.gz log-parts

Above command will split the mybackup compressed tar file into the smaller files each of size 6 MB in current working directory and split file names will starts from log-partsaa … parts-partsab. In case if you want to append numbers in place of alphabets then use -d option in above split command.

$ ls -l log-parts*
-rw-r--r-- 1 rasho rasho 1048576 Jan  7 15:30 log-partsaa
-rw-r--r-- 1 rasho rasho  707985 Jan  7 15:30 log-partsab

Now we can move these files into another server over the network and then we can merge all the files into a single tar compressed file using below mentioned command.

$ cat log-partsa* > log.tar.gz

11. Untar Multiple files from tar, tar.gz and tar.bz2 File

To extract or untar multiple files from the tar, tar.gz and tar.bz2 archive file. For example the below command will extract “file1” “file2” from the archive files.

$ tar -xvf lintut.tar "file1" "file2" 
$ tar -zxvf lintut.tar.gz "file1" "file2" 
$ tar -jxvf lintut.tar.bz2 "file1" "file2"

12. Append or add files to end of archive or tar file

-r option in the tar command is used to append or add file to existing tar file. Let’s add file10 file in lintut.tar

$ tar -rvf lintut.tar file10

Note: In the Compressed tar file we can’t append file or directory.

13. How To Verify tar, tar.gz and tar.bz2 archive file

To verfify any tar or compressed archived file we use option as -W . To do, just use the following examples of command.

$ tar tvfW lintut.tar

That’s all, hope you like different examples of tar command. Please share your feedback and comments.

Leave a Reply

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to top button