Tar command Examples : Compress & Decompress the files\directories

One of the most performed functions during day to day activities of a system admin is data archiving or data compression/Decompression. And normally we use ‘tar’ command to do so but there are other options as well for data archiving or extraction, but that’ the tutorial for another time. In this tutorial, we are going to discuss tar command examples & the various actions we can achieve using it All these commands will work on most of the Linux distributions.

TAR Command

It is certainly the most popular & widely used command for compressing or decompressing files. It has integrated compression & create an archive with “.tar” extension & we can then also use compress it with ‘gzip’ or ‘bzip2’ compression method.

Recommended Read: Learn to use Screen command (with examples)

Also Read: Use of du & df commands (with examples)

Let’s discuss some tar command examples,


Creating a simple archive

To create a simple archive of using tar, syntax is

$ tar -cvf output.tar /path/input

here, -c will create an archive,

-v, will enable verbose mode & will show the progress,

-f, allows us to name the archive

output.tar, will be the name of the created archive, &

/path/input, is the complete path to file or directory that needs to be archived.


Creating a compressed GZIP archive

To create a compressed archive with tar, syntax will be

$ tar -cvzf output.tar.gz /path/input

here, -z option used with above mentioned command will help us create a Gzip compression based archive. Also notice the change in the output file extension, it will now be ‘.tar.gz’ or ‘tgz’instead of only ‘tar’.


Creating a compressed Bzip2 based archive

Now for some reason if we want to create an archive that used Bzip2 as the compression method, then we can use the following syntax to create a compressed archive

$ tar -cvjf output.tgz2 /path/input

Here, -j option will create a Bzip2 based tar archive & the resulting output file extension will be ‘tgz2’or ‘tar.bz2’


Compressing multiple directories

With tar we can also compress multiple directories at once, syntax for the command will be

$ tar -cvf output.tar /path/input1 /path/input2 /path/input3

Here, input1, input2, input3 are the various directories or files that needs to be compressed.


Creating an archive excluding some files/directories

We might need to create an archive or files or directories and might need to exclude some files from being archived. To do that, we can use ‘- – exclude’ option & syntax for the command will be,

$ tar -cvf output.tar /path/input1 — exclude=/path/exclude_file –exclude=/path/exclude_directory

We use multiple ‘–exclude’ to exclude multiple files & directories, we can also exclude a particular file type,

$ tar -cvf output.tar /path/input1 – exclude=/path/*.txt


Listing of files

We can also list the contents of an archive without extracting it using the following commands,

To view the contents of a simple tar archive, command is

$ tar -tvf output.tar

To view the content of a gzip tar archive, command is

$ tar -ztvf output.tar.gz

To view the content of Bzip2 archive, command is

$ tar -jtvf output.tar.gz2


Decompressing the archives

If we need to extract or decompress the archives, commands will be,

To extract the contents of a simple tar archive, command is

$ tar -xvf output.tar

To extract the content of a gzip tar archive, command is

$ tar -zxvf output.tar.gz

To extract the content of Bzip2 archive, command is

$ tar -jxvf output.tar.gz2


That’s it guys, we now end this tutorial on how to compress & decompress using tar command. I hope these tar command examples were informative to you guys, please do leave your valuable comments or suggestions using the comment box below.

If you think we have helped you or just want to support us, please consider these :-

Connect to us: Facebook | Twitter | Google Plus

Become a Supporter – Donate us some of you hard earned money:

Linux TechLab is thankful for your continued support.


Passionate about Linux & open source. Loves to learn, read & write about Linux as well as new technologies.

You may also like...

Leave a Reply

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