About Linux FAQ

Browse More FAQs:

How do I Compress a Whole Linux or UNIX Directory?

Posted by Vivek Gite [Last updated: April 2, 2008]

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 this to a friend      Printable version

Related Other Helpful FAQs:

Discussion on This FAQ

  1. Alpesh Says:

    Excellent !
    Step by step tutorial, easy to understand.

    Thanks
    Alpesh

  2. BinhNT Says:

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

  3. Matthew Cupples Says:

    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 Says:

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

  5. omprakash Says:

    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 Says:

    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 Says:

    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 Says:

    To zip the files try:

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

  9. vivek Says:

    Mark,

    The -r option recurse into directories,
    [code]zip -r file-name.zip .
    zip -r file-name.zip /path/to/directory[/code]

  10. val Says:

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

  11. Elambarithi Says:

    Superb..!
    easy to understand..

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!

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

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Tags: , , , ,

Copyright © 2006-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.