How do I Compress a Whole Linux or UNIX Directory?
Q. How can I compress a whole directory under Linux / UNIX using a shell prompt?
A. It is very easy to compress a Whole Linux/UNIX directory. It is useful to backup files, email all files, or even to send software you have created to friends. 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):
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:
$ 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,
- -x: Extract files
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 -
See also:
E-mail
Print
Can't find an answer to your question? Contact us
Related Other Helpful FAQs:
Discussion on This FAQ
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!
Tags: Compress directory, Linux, tar command, UNIX, zip Compress directory



March 17th, 2006 at 11:43 am
Excellent !
Step by step tutorial, easy to understand.
Thanks
Alpesh
February 27th, 2007 at 8:14 am
Very excellent!
That is best guideline
Thank you!!!
March 15th, 2007 at 11:20 pm
yeah, it worked alright. :p it tarballed the directory and every file inside the directory into separate files
problem is, there are about 1k+ files
how do i have them all unzipped?
March 16th, 2007 at 8:25 am
use tar -zxvf file to untar them:
tar -xvvf file.tar
March 19th, 2007 at 7:53 am
Can we compress and uncompress a file even without having the write permission to the source file or the zipped by merely having the execute permission.
August 16th, 2007 at 2:39 am
The system I am running this command does not support tar -z. Hence I tried to do a tar followed by a gzip. Then there was a lack of space error. Is there a way to do this for me?
April 2nd, 2008 at 1:39 pm
This is cool but I need to compress directory which has many folders and files within each other. Without knowing each folder name, can I zip the directory using your example?
April 2nd, 2008 at 5:35 pm
To zip the files try:
zip prog.zip /home/jerry/prog/*
April 2nd, 2008 at 7:22 pm
Mark,
The -r option recurse into directories,
[code]zip -r file-name.zip .
zip -r file-name.zip /path/to/directory[/code]
April 7th, 2008 at 1:01 pm
just what I needed!! thanks for putting this up..
April 22nd, 2008 at 1:58 pm
Superb..!
easy to understand..