Home » Linux » Linux Commands » TAR command examples in Unix / Linux

TAR command examples in Unix / Linux

The “tar” command in linux used for making archive of files and directories. Using “tar” command you can create “.tar“, “.tar.gz“, “.tar.bz2” file archive. These are known as “tarball“, “gizp“, “bzip” in Linux. TAR has more flexibility than ZIP, which is widely used in cross platform environment.

I will show tar command examples in this article.

First take a look, how many options are available for the command

Main operation mode:

-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 on mag tapes!)
-r, –append append files to the end of an archive
-t, –list list the contents of an archive
–test-label test the archive volume label and exit
-u, –update only append files newer than copy in archive
-x, –extract, –get extract files from an archive

Compression options:

-a, –auto-compress use archive suffix to determine the compression
program
-I, –use-compress-program=PROG
filter through PROG (must accept -d)
-j, –bzip2 filter the archive through bzip2
-J, –xz filter the archive through xz
–lzip filter the archive through lzip
–lzma filter the archive through lzma
–lzop
–no-auto-compress do not use archive suffix to determine the
compression program
-z, –gzip, –gunzip, –ungzip filter the archive through gzip
-Z, –compress, –uncompress filter the archive through compress

I choose a WordPress installer for the demonstration. The total size of the installer is 26MB. We will check the archived file size of each compression method can offer.

1. Create tar archive File

Tar archive does compress the files and folders automatically. The archived can be stored on disk or can transfer on different disk or It can be used for email attachment .

# tar -caf wordpress.tar wordpress/

The archive size reduced to 23MB.

[root@test46 unixhops]# ls -lh
 -rw-r--r-- 1 root root 23M Sep 1 04:09 wordpress.tar

2. Create tar.gz archive file

gz stands for gunzip. .tar.gz archive can compress the files and folder in efficient manner.

# tar -czf wordpress.tar.gz wordpress/

The archive size reduced to 7.6MB. Pretty impressive!

[root@test46 unixhops]# ls -lh
 -rw-r--r-- 1 root root 23M Sep 1 04:09 wordpress.tar
 -rw-r--r-- 1 root root 7.6M Sep 1 04:15 wordpress.tar.gz

3. Create tar.bz2 archive file

The bzip is more efficient to reduce the size of archive file.

# tar -cjf wordpress.tar.bz2 wordpress/

The archive size reduced to 6.9MB.

[root@test46 unixhops]# ls -lh
 -rw-r--r-- 1 root root 23M Sep 1 04:09 wordpress.tar
 -rw-r--r-- 1 root root 6.9M Sep 1 04:27 wordpress.tar.bz2
 -rw-r--r-- 1 root root 7.6M Sep 1 04:26 wordpress.tar.gz

4. Untar tar archive file

The usability of the of any archive if useful when it can be easily unpacked. Unpack the .tar file

# tar -xf wordpress.tar
 or
 # tar -xf wordpress.tar /home/unixhops/extracted

The ‘x’ is stands for extract.

5. Uncompress tar.gz archive file

# tar -xzf wordpress.tar.gz

6. Uncompress tar.bz2 archive file

# tar -xjf wordpress.tar.bz2

7. List content of tar archive file

# tar tf wordpress.tar
 wordpress/
 wordpress/wp-blog-header.php
 wordpress/wp-login.php
 wordpress/wp-config-sample.php
 wordpress/wp-admin/
 wordpress/wp-admin/link-parse-opml.php
 wordpress/wp-admin/ms-upgrade-network.php
 wordpress/wp-admin/themes.php

8. Untar specific file from tar archive file

# tar -xf wordpress.tar wordpress/wp-config-sample.php

That’s all for now. Those are the basic tar command examples in Unix / Linux. For advance options make use of tar man page : https://linux.die.net/man/1/tar

Share your feedback to the comment section.

Check Also

MySQL Command Line Administration in Linux

Linux system admins are very familiar with command line tools. Many time admins need to …

Leave a Reply

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

%d bloggers like this: