July 6, 2021 . 2 MIN READ
How can I compress a whole directory under Linux / UNIX using a shell prompt?
You need to use tar command as follows (syntax of tar command):
tar -zcvf archive-name.tar.gz directory-name
Where,
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:
$ tar -zcvf prog-1-jan-2005.tar.gz /home/jerry/prog
Above command will create an archive file called prog-1-jan-2005.tar.gz in current directory. If you wish to restore your archive then you need to use following command (it will extract all files in current directory):
$ tar -zxvf prog-1-jan-2005.tar.gz
Where,
If you wish to extract files in particular directory, for example in /tmp then you need to use following command:
$ tar -zxvf prog-1-jan-2005.tar.gz -C /tmp
$ cd /tmp
$ ls –
Exclude directory:
tar -cvf cpm_web_files.tar.gz /var/www/cpm.cybernetikzdigital.com/web
–exclude “pm_files”
TAR: tar -cvf httpdocs_06_20_2006.tar.gz /home/user/domain.com/site–exclude “/home/user/domain.com/site/excluded-dir1” –exclude “/home/user/domain.com/site/excluded-dir2” GNU TAR: tar -cvf httpdocs_06_20_2006.tar.gz –exclude “/home/user/domain.com/site/excluded-dir1” –exclude “/home/user/domain.com/site/excluded-dir2″/home/user/domain.com/site