Tuesday, July 9, 2013

How to Compress a Whole Directory in Linux?


It is very easy to compress a Whole directory in Linux. It is useful for backup all files, directories, mails, or even to send software you have created. Technically, it is called as a compressed archive. GNU tar command is best for this work. It can be use on remote Linux or UNIX server. It does two things for you:
=> Create the archive
=> Compress the archive
You need to use tar command as follows (syntax of tar command):

[root@akwal ~]# tar -zcvf archive-name.tar.gz directory-name

Where,

-z: Compress archive using gzip program
-c: Create archive
-v: Verbose i.e display progress while creating archive
-f: Archive File name

For example, you have directory called /home/jerry/prog and you would like to compress this directory then you can type tar command as follows:

[root@akwal ~]# tar -zcvf prog-1-jan-2005.tar.gz /home/jerry/prog

If you wish to restore your archive then you need to use following command (it will extract all files in current directory):

[root@akwal ~]# tar -zxvf prog-1-jan-2005.tar.gz

Where,

-x: Extract files

If you wish to extract files in particular directory, for example in /tmp then you need to use following command:

[root@akwal ~]# tar -zxvf prog-1-jan-2005.tar.gz -C /tmp
[root@atta ~]# cd /tmp
[root@atta ~]# ls -

0 comments: