Linux: Working with archivers (gzip, bzip2 and 7zip)

Method 1

Step 1: Group the file you need to compress by using tar command.

root@bt:~/fw-scripts# tar -cf firewall.tar firewall.sh
root@bt:~/fw-scripts# ls -la
total 24
drwxr-xr-x  2 root root  4096 2011-08-04 00:35 .
drwx------ 33 root root  4096 2011-08-04 00:00 ..
-rwxr--r--  1 root root   967 2011-08-03 01:42 firewall.sh
-rw-r--r--  1 root root 10240 2011-08-04 00:35 firewall.tar

Step 2: Use an archiver of your choice. Use gzip.

root@bt:~/fw-scripts# gzip -9 firewall.tar
root@bt:~/fw-scripts# ls -la
total 16
drwxr-xr-x  2 root root 4096 2011-08-04 00:37 .
drwx------ 33 root root 4096 2011-08-04 00:00 ..
-rwxr--r--  1 root root  967 2011-08-03 01:42 firewall.sh
-rw-r--r--  1 root root  485 2011-08-04 00:35 firewall.tar.gz

Use bzip2.

root@bt:~/fw-scripts# bzip2 firewall.tar
root@bt:~/fw-scripts# ls -la
total 16
drwxr-xr-x  2 root root 4096 2011-08-04 00:39 .
drwx------ 33 root root 4096 2011-08-04 00:00 ..
-rwxr--r--  1 root root  967 2011-08-03 01:42 firewall.sh
-rw-r--r--  1 root root  519 2011-08-04 00:38 firewall.tar.bz2

To make the compression better:

root@bt:~/fw-scripts# bzip2 --best firewall.tar
This is the same as gzip -9 option. You can simply compress a tar file with only the gzip command without any options.

Decompress the files 

For bzip2:

root@bt:/tmp# bzip2 -d firewall.tar.bz2 
root@bt:/tmp# ls firewall.tar
firewall.tar

For gzip:

root@bt:/tmp# gzip -d firewall.tar.gz
root@bt:/tmp# ls -l firewall.tar
-rw-r--r-- 1 root root 10240 2011-08-04 00:46 firewall.tar

To untar the file:

root@bt:/tmp# tar -xf firewall.tar
root@bt:/tmp# ls firewall.*
firewall.sh  firewall.tar

Check the tar file content before unzip:

root@bt:/tmp# tar -tf firewall.tar
firewall.sh

Method 2

Compress with gzip

root@bt:~/fw-scripts# tar -czf firewall.tar.gz firewall.sh 
root@bt:~/fw-scripts# ls
firewall.sh  firewall.tar.gz

Compress with bzip2:

root@bt:~/fw-scripts# tar -cjf firewall.tar.bz2 firewall.sh
root@bt:~/fw-scripts# ls
firewall.sh  firewall.tar.bz2  firewall.tar.gz

Note: The f option stands for file, hence it must always be the last letter of your option. You can rearrange tar -czf or tar -zcf it is the same, but f is always the last and immediately follow by f will always be a filename of your compressed file. This works for bzip2 as well.

Compress file with 7zip

Compress your file into zip format:

ot@bt:~/fw-scripts# 7z a firewall.zip firewall.sh 

7-Zip 9.04 beta  Copyright (c) 1999-2009 Igor Pavlov  2009-05-30
p7zip Version 9.04 (locale=en_US.UTF-8,Utf16=on,HugeFiles=on,8 CPUs)
Scanning

Creating archive firewall.zip

Compressing  firewall.sh      

Everything is Ok
root@bt:~/fw-scripts# ls
firewall.sh  firewall.tar.bz2  firewall.tar.gz  firewall.zip

Compress in bzip2:

root@bt:~/fw-scripts# 7z a firewall.tar.bz2 firewall.sh 

7-Zip 9.04 beta  Copyright (c) 1999-2009 Igor Pavlov  2009-05-30
p7zip Version 9.04 (locale=en_US.UTF-8,Utf16=on,HugeFiles=on,8 CPUs)
Scanning

Creating archive firewall.tar.bz2

Compressing  firewall.sh      

Everything is Ok

Likewise if you want to zip in gzip format is also done the same.

Decompress with 7zip

Decompress zip:

root@bt:/tmp# 7z e firewall.zip 

7-Zip 9.04 beta  Copyright (c) 1999-2009 Igor Pavlov  2009-05-30
p7zip Version 9.04 (locale=en_US.UTF-8,Utf16=on,HugeFiles=on,8 CPUs)

Processing archive: firewall.zip

file firewall.sh
already exists. Overwrite with
firewall.sh?
(Y)es / (N)o / (A)lways / (S)kip all / A(u)to rename all / (Q)uit? y
Extracting  firewall.sh

Everything is Ok

Size:       967
Compressed: 475

Decompress bzip2:

ot@bt:/tmp# 7z e firewall.tar.bz2 

7-Zip 9.04 beta  Copyright (c) 1999-2009 Igor Pavlov  2009-05-30
p7zip Version 9.04 (locale=en_US.UTF-8,Utf16=on,HugeFiles=on,8 CPUs)

Processing archive: firewall.tar.bz2

file firewall.tar
already exists. Overwrite with
firewall.tar?
(Y)es / (N)o / (A)lways / (S)kip all / A(u)to rename all / (Q)uit? y
Extracting  firewall.tar

Everything is Ok

Size:       967
Compressed: 425
Advertisements
This entry was posted in Linux, System OS and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s