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:
- Email FAQ to a friend
- Printable version
- Rss Feed
- Last Updated: 4-2-08

{ 13 comments… read them below or add one }
Excellent !
Step by step tutorial, easy to understand.
Thanks
Alpesh
Very excellent!
That is best guideline
Thank you!!!
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?
use tar -zxvf file to untar them:
tar -xvvf file.tar
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.
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?
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?
To zip the files try:
zip prog.zip /home/jerry/prog/*
Mark,
The -r option recurse into directories,
[code]zip -r file-name.zip .
zip -r file-name.zip /path/to/directory[/code]
just what I needed!! thanks for putting this up..
Superb..!
easy to understand..
thanks for your guideline.
how to list the file under the tar.gz?
I have try tar -tf
but fail and come out a message “This does not look like a tar archive”
THANKS! :)