How do I Compress a Whole Linux or UNIX Directory?

Posted on in Categories , , , , , , , last updated September 11, 2015

How can I compress a whole directory under a Linux / UNIX using a shell prompt?

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 the 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, say you have a 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 the 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 the following command:
$ tar -zxvf prog-1-jan-2005.tar.gz -C /tmp
$ cd /tmp
$ ls -

A note about non gnu/tar command

The above syntax use GNU tar command for compressing and uncompressing tar files. If your system does not use GNU tar, you can still create a compressed tar file, via the following syntax:
tar -cvf - file1 file2 dir3 | gzip > archive.tar.gz

See also:

70 comment

  1. 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?

  2. 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.

  3. 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?

  4. 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?

  5. 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”

  6. HI Excellent Tutorial .

    i Have one question, Is it possible to gzip all the files in a directory and remove all the files once we gziped is finished.
    Hope u got my point ?

  7. Thanks, this is good, but usaully when i compress an archive i want also crypt it using a password. Could you help me to do it??????????????? :-)

  8. Hi, I have to compress multiple dir within a dir and i used tar -czf option.. it created tgz file, but it doesnt remove the original directories.. Is there any way or option in zip/compress/archive to remove the original files after compression.. thanks

  9. THANK YOU, this saved my first Minecrafts world. I was playing around with compiz and trying to make my windows slightly transparent like you see people have in videos. But forgot to turn the setting down from 100% or 0% or whatever before setting the window type to “any”. so then every single window dissappeared and I couldn’t see even an outline or shadow. AHHHGGGG!!!!! Luckily the only thing I actually cared about on that system was the minecraft world I’d been playing on. So I was able to open a terminal, type these commands in blind, then do a move command to get the new “saves.tar.gz” into my dropbox folder.

    This post just turned a annoyed and ticked off night, into a “Hallelujah, THANK YOU GOD” kind of night lol

    Thanks to Vivek Gite also though, and to all the other Linux/Unix/PC in general Pro’s out there that put these tid-bits of info on the net. They save absent minded people like me all the time!!! :-P

    God Bless ya’s,
    Daniel

  10. nice tutorials. I have been referring to this website for all my linux questions and got answers for most of them.

  11. Thanks for the lesson. I tried to use gzip last night to backup our site files. Entire site gave 404’s because all the files were renamed to .gz. Linux rookie mistake.

  12. sometimes you might have very big files (with pictures, for instance) that you dont want to be in the archived file, in that case you can use EXCLUDE command to do exactly the same backup, without a big folder, and it will go like this:

    $ tar -zcvf prog-1-jan-2005.tar.gz –exclude ‘/home/jerry/prog/big-file/*’ /home/jerry/prog

    in that way the gzip will create an archive without the big-file and all its content.

  13. I have 3 GB directory under /opt file system and I want to compress this directory using gzip but my file system /opt having 500 MB space only. I want to compress this directory and compress directory data moved to another filesystem without effecting of my 3 GB directory because some applications running on this directory. Can anyone tell me the syntax.

  14. Can I just say that sometimes you have to check and see that you’re not trying to tar a symlink!!!

    A time consuming error… :/

  15. Hello,

    Excellent Tutorial.

    Someone can give me the a tip how to compress a bunch of specific folders and/or files from a txt file:
    For instance I have created a txt file with all the files name and/or folder name that I need to compress, any suggestion?.

    Thanks in advance,

    Habib

  16. omg —

    Instead of asking questions here where nobody will reply, simply try it

    “can i do this on directories?”

    fakn try it!

  17. I can’t tell how many times I had to come back here and look at the command lawl…

    It can be updated to use xz just using the -J flag

  18. How to create tar archive of mtd block and send it via TFTP to remote server? (without intermediate saving archive first on local system)

  19. Bad English, sorry. I am glad that I don’t work with Linux or UNIX. The horror. It even looks like SAW.

    How do I find a file?

    find . -name filena* -print

    ok? And what if I make a mistake, then I have to type that whole again over again until I decide to use bash. I don’t want to bash but I do it.

    Same goes for OpenSolaris or Solaris where you have to make the index of the man pages. Why?!? Because I get an error catman: windex not found or something when I don’t execute that stupid command.

    catman -w

  20. To automatically delete the original directory, as soon as the new archive is ready; append your code with:

     && rm -rf '/old/dir/' 

    .
    Or play it safe; with:

     && rm -ir '/old/dir/' 

    .

Leave a Comment