32.3 C
Muhammad Nabeel
Network and System Administrator

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

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

- Advertisement -

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

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.

- Advertisement -
Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
"The best Linux newsletter on the web"


Please enter your comment!
Please enter your name here

Latest article