How to Compress and Decompress any file in Redhat Linux Server


hello friends today we will learn how to Compress and Decompress any file in Linux .Linux Server in File compression very powerful we can can Compress any file through many methods here i show you some Powerful method .You just follow simple step

compress data in linux

Step 1.here i create some text file and then i compress and compress through TAR method
[root@localhost ~]# touch a1.txt
[root@localhost ~]# touch a2.txt
[root@localhost ~]# touch a3.txt
[root@localhost ~]# touch a4.txt

Step 2 here i provide every file 40 MB size

[root@localhost ~]# dd if=/dev/sda if=a1.txt bs=2MB count=20
0+0 records in
0+0 records out
0 bytes (0 B) copied, 3.0118e-05 seconds, 0.0 kB/s
[root@localhost ~]# dd if=/dev/sda of=a1.txt bs=2MB count=20
20+0 records in
20+0 records out
40000000 bytes (40 MB) copied, 0.0749134 seconds, 534 MB/s
[root@localhost ~]# dd if=/dev/sda of=a2.txt bs=2MB count=20
20+0 records in
20+0 records out
40000000 bytes (40 MB) copied, 0.123504 seconds, 324 MB/s
[root@localhost ~]# dd if=/dev/sda of=a3.txt bs=2MB count=20
20+0 records in
20+0 records out
40000000 bytes (40 MB) copied, 0.144952 seconds, 276 MB/s
[root@localhost ~]# dd if=/dev/sda of=a4.txt bs=2MB count=20
20+0 records in
20+0 records out
40000000 bytes (40 MB) copied, 0.104576 seconds, 382 MB/s

Step 3 now we check our file
[root@localhost ~]# ls -lh
total 156.0MB
-rw-r–r– 1 root root 39M Jan 12 07:27 a1.txt
-rw-r–r– 1 root root 39M Jan 12 07:27 a2.txt
-rw-r–r– 1 root root 39M Jan 12 07:27 a3.txt
-rw-r–r– 1 root root 39M Jan 12 07:37 a4.txt

tar -cvf compression 

Step4- so let we will combine all file with  tar compress

[root@localhost ~]# tar -cvf alltxt.tar a1.txt a2.txt a3.txt a4.txt

(Note- if we have large of number file then we will use (*)   like this [root@localhost ~]# tar -cvf alltxt.tar .txt*  )  
a1.txt
a2.txt
a3.txt
a4.txt  

Step 5 now we will check  then we will found we have alltxt.tar new file
[root@localhost ~]# ls -lh
total 320.MB

-rw-r–r– 1 root root 39M Jan 12 07:27 a1.txt
-rw-r–r– 1 root root 39M Jan 12 07:27 a2.txt
-rw-r–r– 1 root root 39M Jan 12 07:27 a3.txt
-rw-r–r– 1 root root 39M Jan 12 07:37 a4.txt
-rw-r–r– 1 root root 153M Jan 12 07:46 alltxt.tar

Step5 next we will remove all txt file .
[root@localhost ~]# rm -rf a1.txt a2.txt a3.txt a4.txt

Step 6 now if we want check we check alltxt.tar in which file include  so we can look through this commands
[root@localhost ~]# tar -tvf alltxt.tar
-rw-r–r– root/root 40000000 2017-01-12 07:27:06 a1.txt
-rw-r–r– root/root 40000000 2017-01-12 07:27:17 a2.txt
-rw-r–r– root/root 40000000 2017-01-12 07:27:30 a3.txt
-rw-r–r– root/root 40000000 2017-01-12 07:37:41 a4.txt

Step 7 now if we want decompress we can do this belong commands
[root@localhost ~]# tar -xvf alltxt.tar
a1.txt
a2.txt
a3.txt
a4.txt

w.zip Compress

[root@localhost ~]# zip w.zip a1.txt a2.txt a3.txt a4.txt 

(note- we can also use for all txt file [root@localhost ~]# zip w.zip .txt *)

adding: a1.txt (deflated 90%)
adding: a2.txt (deflated 90%)
adding: a3.txt (deflated 90%)
adding: a4.txt (deflated 90%)

 

now here you can look my all txt file size is 160 mb but after compress only 16 mb file size
[root@localhost ~]# ls -lh
total 176.MB
-rw-r–r– 1 root root 39M Jan 12 07:27 a1.txt
-rw-r–r– 1 root root 39M Jan 12 07:27 a2.txt
-rw-r–r– 1 root root 39M Jan 12 07:27 a3.txt
-rw-r–r– 1 root root 39M Jan 12 07:37 a4.txt

-rw-r–r– 1 root root 16M Jan 12 08:55 w.zip

now we delete a1.a2.a3.a4 txt file and then decompress

[root@localhost ~]# rm -rf a1.txt a2.txt a3.txt a4.txt 

Step 8 we can decompress through belong commands
[root@localhost ~]# unzip w.zip
Archive: w.zip
inflating: a1.txt
inflating: a2.txt
inflating: a3.txt
inflating: a4.txt

GZIP/GUNGIP(.gn) Compress

gunzip compression in we can compress one by one file we can say this compress only get one particular file
[root@localhost ~]# touch hello.txt
[root@localhost ~]# dd if=/dev/sda of=hello.txt bs=2M count=100
100+0 records in
100+0 records out
209715200 bytes (210 MB) copied, 2.96087 seconds, 70.8 MB/s

here i create hello.txt file its size is  210MB now i compress through gzip

 
[root@localhost ~]# gzip hello.txt
[root@localhost ~]# ls -lh
rw-r–r– 1 root root 67M Jan 12 09:18 hello.txt.gz
so you can look hello.txt file size is only 67mb

fi we want uncompressed we can do it belong  commands
[root@localhost ~]# gunzip hello.txt .gz

we can look
[root@localhost ~]# ls -lh
-rw-r–r– 1 root root 200M Jan 12 09:18 hello.txt

BZIP2/BUNZIP2(.bz2) compression 

this compression is very powerful compression but its bad think it take time. BZIP2/BUNZIP2(.bz2)   compression also particular file

here i create 419 mb demo.txt file we will compress through BZIP2

[root@localhost ~]# touch Demo.txt
[root@localhost ~]# dd if=/dev/sda of=Demo.txt bs=2M count=200
200+0 records in
200+0 records out
419430400 bytes (419 MB) copied, 14.6637 seconds, 28.6 MB/s
we will do compress through belong commands
[root@localhost ~]# bzip2 Demo.txt
you can look just Demo.txt file  after compression only 111Mb
-rw-r–r– 1 root root 111M Jan 12 09:37 Demo.txt.bz2

we can do decompress through this commands

[root@localhost ~]# bunzip2 Demo.txt.bz2

TAR+GZIP(tar.gz / .tgz) compression 

TAR+GZIP(tar.gz / .tgz) this compression we can apply on group of file

here i explane in details

here i create four demo file through touch commands
[root@localhost expirment]# touch demo.txt
[root@localhost expirment]# touch demo1.txt
[root@localhost expirment]# touch demo2.txt
[root@localhost expirment]# touch demo3.txt
[root@localhost expirment]# touch demo4.txt

you can look through this commands
[root@localhost expirment]# ls -lh
total 20K
-rw-r–r– 1 root root 0 Jan 12 10:17 demo1.txt
-rw-r–r– 1 root root 0 Jan 12 10:17 demo2.txt
-rw-r–r– 1 root root 0 Jan 12 10:17 demo3.txt
-rw-r–r– 1 root root 0 Jan 12 10:17 demo4.txt
-rw-r–r– 1 root root 0 Jan 12 10:16 demo.txt

then here i provide every file 200Mb size
[root@localhost expirment]# dd if=/dev/sda of=demo.txt bs=2M count=100
100+0 records in
100+0 records out
209715200 bytes (210 MB) copied, 0.597468 seconds, 351 MB/s
[root@localhost expirment]# dd if=/dev/sda of=demo1.txt bs=2M count=100
100+0 records in
100+0 records out
209715200 bytes (210 MB) copied, 0.361243 seconds, 581 MB/s
[root@localhost expirment]# dd if=/dev/sda of=demo2.txt bs=2M count=100
100+0 records in
100+0 records out
209715200 bytes (210 MB) copied, 2.37261 seconds, 88.4 MB/s
[root@localhost expirment]# dd if=/dev/sda of=demo3.txt bs=2M count=100
100+0 records in
100+0 records out
209715200 bytes (210 MB) copied, 0.669441 seconds, 313 MB/s
[root@localhost expirment]# dd if=/dev/sda of=demo4.txt bs=2M count=100
100+0 records in
100+0 records out
209715200 bytes (210 MB) copied, 0.353607 seconds, 593 MB/s

here we will look every file size is 200MB
[root@localhost expirment]# ls -lh
total 1002M
-rw-r–r– 1 root root 200M Jan 12 10:18 demo1.txt
-rw-r–r– 1 root root 200M Jan 12 10:19 demo2.txt
-rw-r–r– 1 root root 200M Jan 12 10:19 demo3.txt
-rw-r–r– 1 root root 200M Jan 12 10:19 demo4.txt
-rw-r–r– 1 root root 200M Jan 12 10:18 demo.txt

now we will compress through TAR+GZIP(tar.gz / .tgz)  compression

[root@localhost expirment]# tar cvfz all.tgz *txt
demo1.txt
demo2.txt
demo3.txt
demo4.txt
demo.txt

now you can look 800MB file compress with 331MB
root@localhost expirment]# ls -lh
total 1.4G
-rw-r–r– 1 root root 331M Jan 12 10:33 all.tgz
-rw-r–r– 1 root root 200M Jan 12 10:18 demo1.txt
-rw-r–r– 1 root root 200M Jan 12 10:19 demo2.txt
-rw-r–r– 1 root root 200M Jan 12 10:19 demo3.txt
-rw-r–r– 1 root root 200M Jan 12 10:19 demo4.txt
-rw-r–r– 1 root root 200M Jan 12 10:18 demo.txt

now we can delete all Demo.txt file

 [root@localhost expirment]# rm -rf *txt
[root@localhost expirment]# ls -lh
total 331M
-rw-r–r– 1 root root 331M Jan 12 10:33 all.tgz

[root@localhost expirment]# tar xvzf all.tgz
demo1.txt
demo2.txt
demo3.txt
demo4.txt
demo.txt

TAR+BZIP2(tar.bz2/.bz2) Compression 

this compression also use TAR help and compress group of file

so let we start before i told you.You wil  create  any 5-6 .txt./mp3/ .mp4  file

so now here i agree you have four .txt file then i compress my demo.txt file here

 [root@localhost expirment]# tar cvfj all.tbz2 *.txt
demo1.txt
demo2.txt
demo3.txt
demo4.txt
demo.txt
then we will look here tbz2 file size is 307MB
[root@localhost expirment]# ls -lh
total 1.7G
-rw-r–r– 1 root root 307M Jan 12 10:52 all.tbz2
-rw-r–r– 1 root root 200M Jan 12 10:18 demo1.txt
-rw-r–r– 1 root root 200M Jan 12 10:19 demo2.txt
-rw-r–r– 1 root root 200M Jan 12 10:19 demo3.txt
-rw-r–r– 1 root root 200M Jan 12 10:19 demo4.txt
-rw-r–r– 1 root root 200M Jan 12 10:18 demo.txt
then we will delete all demo file through this commands

[root@localhost expirment]# rm -rf *.txt
we can decompress through this commands
root@localhost expirment]# tar xvjf all.tbz2
demo1.txt
demo2.txt
demo3.txt
demo4.txt
demo.txt

if you will have any problems in compression please comments here i will solve your problem

Thank you

by Vishnu Sharma

Advertisements

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 )

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s