Use tar command to archive/compress files/folders in Linux

Windows Articles

Ubuntu Rolling Release? It is possible with Rolling Rhino

Throughout the Linux community, there are always expectations about when Ubuntu decides to have a Rolling Release. This, at least, since the...

How to recover files deleted with Windows File Recovery

Hello! Deleting files by accident is one of the biggest fears of users. Indeed, unintentionally deleting documents, music or photos can be...

How to install Vivaldi Web Browser on Ubuntu 20.04 / 19.10?

This is a short post in which I will show you how to install Vivaldi Web Browser on Ubuntu 20.04 / 19.10...

Linuxfx is a Windows 10 clone based on Ubuntu.

Hi! Most Windows users are afraid to switch to Linux for various reasons. However, the biggest one is the difference in the...

How to install OpenMeetings on Ubuntu 20.04 / 18.04?

If there is one thing the Internet has completely revolutionized, it is long-distance communications. And with it the business meetings that thanks to many...
Muhammad Nabeel
Muhammad Nabeel
Network and System Administrator

tar is a Linux utility. It is stand for tape archive. tar command is use to collect many files/directories into one archive file, often referred to as a tarball. Normally it used for distribution or backup purposes. It is most popular tool in Linux systems to create compressed archive files.

In this article you will learn that how to use tar command for data archive and its compression using (tar, tar.gz, tar.bz2).

We will use below tar tools.

1- Tar
2- Gzip
3- Bzip2

Create tar Archive File

So, in below example we will create a tar archive file data.tar for a directory /boot in current working directory.

tar -cvf data.tar /boot
ls

c – Creates a new .tar archive file.
v – Verbosely show the .tar file progress.
f – File name type of the archive file.

Untar (Un-Compress) Archive File

Now, we will un-compress data.tar archive file in current working directory using below command.

tar -xf data.tar

x to extract archive file.

uncompress or extract an archive in current working directory.

So what if you want to Un-compress archive in different path or directory. use -C (Capital C) in command and specify your path to un-compress it, Please see below example command.

tar -xf data.tar -C /media
ls /media

You can see in above image that boot directory is uncompressed in /media directory.

Create tar.gz (gzip) Archive File

If you want to compress a file/folder with gzip, then you have to use “z” in tar command and make archive extension as .tar.gz

z – To create a compressed gzip archive file

Run below command

tar -czf data.tar.gz /boot

Un-compress gzip archive file

tar -xf data.tar.gz

Then, you can also un-compress gzip file in your desired directory

tar -xf data.tar.gz -C /media

Create tar.bz2 Archive File

The bz2 feature is used to highly compress and create archive. Its archive size is small then gzip archive. The bz2 compression takes more time to compress and decompress files as compared to gzip which takes less time.

j – To create highly compressed tar file, Use following command

tar cj data.tar.bz2 /boot

Un-compress bz2 archive file

tar -xf data.tar.bz2

Now, un-compress it in another directory

tar -xf data.tar.bz2 -C /media

List Content of Archive File

t – it is used to list archive data without extract it.

tar -tvf data.tar
tar -tvf data.tar.gz
tar -tvf data.tar.bz2

That’s it.

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article

Ubuntu Rolling Release? It is possible with Rolling Rhino

Throughout the Linux community, there are always expectations about when Ubuntu decides to have a Rolling Release. This, at least, since the...

How to recover files deleted with Windows File Recovery

Hello! Deleting files by accident is one of the biggest fears of users. Indeed, unintentionally deleting documents, music or photos can be...

How to install Vivaldi Web Browser on Ubuntu 20.04 / 19.10?

This is a short post in which I will show you how to install Vivaldi Web Browser on Ubuntu 20.04 / 19.10...

Linuxfx is a Windows 10 clone based on Ubuntu.

Hi! Most Windows users are afraid to switch to Linux for various reasons. However, the biggest one is the difference in the...

How to install OpenMeetings on Ubuntu 20.04 / 18.04?

If there is one thing the Internet has completely revolutionized, it is long-distance communications. And with it the business meetings that thanks to many...