Log In | Register | April 25, 2024

Share
 

Linux - June 16, 2012

Archive, compress, unpack, and uncompress files using tar, gzip, and bzip2.

Using tar

TAR can be used to backup/compress and transfer files/folders easily. With tar you can archive as well as compress. Before using tar there are a few flags you want to be familiar with.

-c flag = Create a new archive.
-v flag = Verbosely list files processed.
-f flag = The archive file.
-x flag = Extract files from an archive.
-j flag = Filter the archive through bzip2.
-z flag = Filter the archive through gzip.
-C flag = Uncompress/extract to a defined directory.

Using TAR to archive/compress files and folders.

Lets say we have a folder filled with files that we want to archive. To do so we simply do the following.

tar -cvf archive.tar /folder_to_archive

The above command creates a file named archive.tar full of all of the files and directories within the /folder_to_archive directory. The -c allows the tar utility to know that we want to create a new archive. The -v flag allows us to see the progress of the archive and what files it is currently processing. The -f flag allows you to create the name of the file you want to create. In this case its archive.tar.

Now with the same command above we can add in some level of compression. Lets compress the archive using bzip2.

tar -jcvf archive.tar.bz2 /folder_to_archive

Notice that in the above command I did two things. First I added the -j flag allowing us to use bzip2 compression. The second thing was that I changed the file name to have a .bz2 extension. This portion is not necessary but good for you and other system administrators to reference and know what file type this file is using.

Lets now compress the same folder using gzip.

tar -zcvf archive.tar.gz /folder_to_archive

In the above command instead of the -j flag I used the -z flag letting tar know that we want to compress this folder using gzip. I also changed the name here on our file to include a .gz extension which is not necessary but good practice.

Using TAR to extract your compressed/archived file.

Similar to the above commands which we learned you can easily uncompress files by using the -x flag instead of the -c flag.

For instance to unarchive a standard tar file do the following.

tar -xvf archive.tar

In order to decompress a bzip2 file use the -j flag along with the -x flag.

tar -jxvf archive.tar.bz2

In order to decompress a gzip file use the -z flag along with the -x flag.

tar -zxvf archive.tar.gz

Lastly you can also use the -C flag to decompress your file to a defined directory as such.

tar -xvf archive.tar -C /tmp

Running the above command will decompress archive.tar within the /tmp directory.

Using gzip and gunzip

You can also use gzip and gunzip to compress and decompress files like tar. When running gzip on a file, the uncompressed file disappears and you’re left with the compressed version. Here’s an example below on how to use gzip to compress a file.

gzip test_file.txt

Note that the new file left is now set with a .gz extension. In order to decompress the file you can use gunzip as such. After the decompression is complete the compressed file is then removed.

gunzip test_file.txt.gz

Using bzip2 and bunzip2

You can also use bzip2 and bunzip2 to compress and decompress files like tar. Similar to the gzip utility, after compressing a file the uncompressed file disappears and you’re left with the compressed version. Below is an example on how to use bzip2.

bzip2 test_file.txt

Note that the new file left is now set with a .bz2 extension. In order to decompress the file you can use bunzip2 as such. After the decompression is complete the compressed file is then removed.

bunzip test_file.txt.bz2

Post By: | FavoriteLoadingAdd to favorites

5 Comments

RHCSA Certification Study Guide | DevBlog.co
Monday, June 25, 2012

[...] Archive, compress, unpack and uncompress files using tar, gzip, and bzip2 [...]

mducanda
Wednesday, October 17, 2012

Hi dude,you forgot to talk about STAR :

here is an example :

1. yum install star

2. star -c -f=/home/user.star /home/user
it will compresse user folder to /home/

or more complicated :

star -xattr -H=exustar -c -f=/home/user.star /home/user

this will keep ACL with -H=exustar and SElinux attributes with -xattr

3.star -x -f=/home/user.star
it will extract archive

RHCSA (Red hat Certified System Administrator EX-200) – Datacenter Modernization
Friday, October 30, 2020

[…] Archive, Compress , uncompress using tar, Start, gzip […]

Simon
Tuesday, January 12, 2021

Hi, you can use this free tool to make tar.bz2 archives: https://freetools.site/file-compressor/tar-bz2

Courtney
Saturday, October 9, 2021

Create TAR.GZ online for free https://freetools.site/file-compressor/tar-gz

Leave a Comment



Need Help? Ask a Question

Ask anything you want from how to questions to debug. We're here to help.

You Must Be Logged In To Post A Question.

Log In or Register