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:

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 13 comments… read them below or add one }

1 Alpesh 03.17.06 at 11:43 am

Excellent !
Step by step tutorial, easy to understand.

Thanks
Alpesh

2 BinhNT 02.27.07 at 8:14 am

Very excellent!
That is best guideline
Thank you!!!

3 Matthew Cupples 03.15.07 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?

4 rokcy 03.16.07 at 8:25 am

use tar -zxvf file to untar them:
tar -xvvf file.tar

5 omprakash 03.19.07 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.

6 Leion 08.16.07 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?

7 Mark Henry 04.02.08 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?

8 Eduardo Romero 04.02.08 at 5:35 pm

To zip the files try:

zip prog.zip /home/jerry/prog/*

9 vivek 04.02.08 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]

10 val 04.07.08 at 1:01 pm

just what I needed!! thanks for putting this up..

11 Elambarithi 04.22.08 at 1:58 pm

Superb..!
easy to understand..

12 Jeff 12.14.08 at 6:21 am

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”

13 Sebastian 05.31.09 at 9:17 pm

THANKS! :)

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Tagged as: , , , ,

Previous post: Howto: Changing Linux Login Shell

Next post: Finding files in Linux filesystems