Hope that you are enjoying Linux !! Now in this post, we’ll see Compression file folder technique.
Compression file folder in Linux
Generally, compression file folder in windows is known as zip, rar, 7zip.
And in Linux, compression file folder is known as .gz, .bz2, .xz etc.
The archive file in Linux is known as .tar
Now lets see some example:
At first we logged in as super user and after that we changed our directory to home, our main target is to go to the directory named “lnx” which we created previously.
[root@localhost ~]# cd /home ;
[root@localhost home]# cd lnx
Now we’ll see the files/folders located in this directory:
[root@localhost lnx]# ls
file1 passwd ; can able to see the two files which is located here.
Now we’ll copy the passwd folder to our current directory.
[root@localhost lnx]# cp /etc/passwd .[root@localhost lnx]# cp -rv /etc /lnx ; copy command
‘/etc’ -> ‘/lnx’
‘/etc/fstab’ -> ‘/lnx/fstab’
‘/etc/crypttab’ -> ‘/lnx/crypttab’
‘/etc/mtab’ -> ‘/lnx/mtab’
‘/etc/resolv.conf’ -> ‘/lnx/resolv.conf’
‘/etc/fonts’ -> ‘/lnx/fonts’
.
.
.
.
.
.
Now lets see the lists by using “ls” command
[root@localhost lnx]# ls
file1 passwd
Now through this command we’ll see the size of file/folder located in “lnx” folder.
[root@localhost lnx]# du -ch * ; See the size of the files/folder
4.0K file1
4.0K passwd
8.0K total
Now we’ll make archive through this command
[root@localhost lnx]# tar -cvf ar1.tar file1file1
Yes, we’ve made a compression of this file. Lets see this:
[root@localhost lnx]# ls
ar1.tar file1 passwd
Finally successfully we can able to create and see the archive file. See ar1.tar is our file.
Now lets see the size of our compressed file.
[root@localhost lnx]# du -ch * ; See the size of the file/folder12K ar1.tar
4.0K file1
4.0K passwd
20K total
rm command
[root@localhost lnx]# rm -rf etc
[root@localhost lnx]# ls
ar1.tar file1 passwd
Now if we want to extract the archive, then can use this command.
[root@localhost lnx]# tar -xvf ar1.tarfile1
Now see the list, see we can also see the extracted files here
[root@localhost lnx]# ll
total 20
-rw-r–r– 1 root root 10240 May 8 02:00 ar1.tar
-rw-r–r– 1 root root 35 May 8 00:19 file1
-rw-r–r– 1 root root 3004 May 7 23:51 passwd
ar1.tar file1 passwd
This is the command for archive + compression.
[root@localhost lnx]# tar -czvf ar1.tar.gz passwdpasswd [root@localhost lnx]#
[root@localhost lnx]# ls
ar1.tar ar1.tar.gz file1 passwd
See the size.
[root@localhost lnx]# du -ch ar1.tar.gz
4.0K ar1.tar.gz
4.0K total
passwd [root@localhost lnx]# ls ; See the list
ar1.tar ar1.tar.gz file1 passwd [root@localhost lnx]# seq 1000000 > number ; Only compress [root@localhost lnx]# [root@localhost lnx]# ls ; Lets see the listar1.tar ar1.tar.gz file1 number passwd
See the file name
[root@localhost lnx]# gzip number[root@localhost lnx]# ls
ar1.tar ar1.tar.gz file1 number.gz passwd
See the list, yeh !! you can able to see a file named number.gz
Want to see the size??
[root@localhost lnx]# du -sh number.gz
2.1M number.gz
[root@localhost lnx]# gunzip number.gz ; Unzip number
[root@localhost lnx]# ls
ar1.tar ar1.tar.gz file1 number passwd
Now we’ll view the contents of the tar file
[root@localhost lnx]# tar -tvf ar1.tar
-rw-r–r– root/root 35 2016-05-08 00:19 file1
passwd [root@localhost lnx]# tar -xvf ar1.tar
passwd [root@localhost lnx]#
Lets see some of the screenshot of this command, I’ve made it for you.:)
Hope that you’ve enjoyed this section. If you’ve any questions, the comment please.
Leave a Reply