How do I Compress a Whole Linux or UNIX Directory?

by on February 11, 2006 · 52 comments· LAST UPDATED April 2, 2008

in , ,

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

  • -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


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

Tweet itFacebook itG+ itDownload PDF versionFound an error/typo on this page?

{ 52 comments… read them below or add one }

1 Alpesh March 17, 2006 at 11:43 am

Excellent !
Step by step tutorial, easy to understand.



2 BinhNT February 27, 2007 at 8:14 am

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


3 Matthew Cupples March 15, 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?


4 Paulo A. Costa February 18, 2012 at 12:53 am

try: gunzip -rv /directoryname


5 rokcy March 16, 2007 at 8:25 am

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


6 omprakash March 19, 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.


7 Leion August 16, 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?


8 Mark Henry April 2, 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?


9 Eduardo Romero April 2, 2008 at 5:35 pm

To zip the files try:

zip /home/jerry/prog/*


10 nixCraft April 2, 2008 at 7:22 pm


The -r option recurse into directories,

zip -r .
zip -r /path/to/directory


11 val April 7, 2008 at 1:01 pm

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


12 Elambarithi April 22, 2008 at 1:58 pm

easy to understand..


13 Jeff December 14, 2008 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”


14 Sebastian May 31, 2009 at 9:17 pm



15 Chaitanya July 28, 2009 at 7:04 am

Hey Man its really gr8
Thanks for the help


16 Jen August 6, 2009 at 9:09 pm

This is an excellent tutorial! Simple and easy to understand. Cool!


17 Hippy March 25, 2010 at 12:18 am

Very usefull been a long time since I done this had to refresh and this was perfect thanks


18 ihraz April 6, 2010 at 7:09 am



19 Shounka April 15, 2010 at 6:55 am

Thanks, its really very helpful!


20 Tuhin Paul June 27, 2010 at 3:23 am

Thank you very much.
Truly very helpful.


21 eta August 15, 2010 at 1:29 am

Thanks for compress in linux tutorial.


22 Praveen October 13, 2010 at 10:09 am


Thanks for ur excellent help it did a lot for meeeeeeeeeeeeeee


23 vishal sapkal October 20, 2010 at 3:15 pm

very good
can u give how to compress detail directory


24 sekhar November 24, 2010 at 5:29 am

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 ?


25 Tsrlinkan January 25, 2011 at 10:06 pm

Wowwwww really simple and efficient !!!!!!!!!!!!! Thanks for the useful thing


26 avi February 9, 2011 at 12:12 pm



27 Adeel February 12, 2011 at 12:25 pm

Thanks, easy to understand and excellent guide.


28 HiTux March 21, 2011 at 5:16 pm

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??????????????? :-)


29 Nicola Hamwi April 9, 2011 at 12:20 pm

Excellent tutorial !!!


30 Saeasy May 17, 2011 at 10:58 pm

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


31 Junaid July 7, 2011 at 6:46 am

Nice article


32 Daniel Clem August 16, 2011 at 9:02 am

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,


33 Mike September 29, 2011 at 11:02 am

Thanks for helping out – very straightforward.
Well done.


34 matthew September 30, 2011 at 5:20 am

Nice clear and concise tutorial !! Well done



35 Pratik January 17, 2012 at 8:57 am

Thanks a ton :)


36 Paulo A. Costa February 18, 2012 at 12:52 am

@Matthew Cupples:

try: gunzip -rv /directoryname


37 Matthias February 21, 2012 at 9:53 am

Thanks, man!


38 Ahmed Adel February 29, 2012 at 12:18 am

Very useful
Thank you


39 sudhi May 14, 2012 at 3:30 am

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


40 Surjit May 30, 2012 at 7:47 am

GReat !!. now becomes expert in ssh commands


41 great July 24, 2012 at 12:02 pm

Great tutorial cyberciti!


42 Mark October 5, 2012 at 3:02 pm

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.


43 gery November 26, 2012 at 3:11 am

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.


44 brahmabotlagunta December 6, 2012 at 9:13 pm

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.


45 marsh February 21, 2013 at 3:21 pm

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… :/


46 Kolimba March 15, 2013 at 10:46 am

I agree with you marsh I just wasted an hour of my life doing that


47 chuche May 18, 2013 at 3:49 am

Thanks for the help!


48 kishore June 28, 2013 at 10:02 am

Its really awesome command…… Thanx a lot…


49 Habib October 29, 2013 at 7:49 pm


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,



50 mike January 21, 2014 at 8:11 pm

omg —

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

“can i do this on directories?”

fakn try it!


51 Nollan October 9, 2014 at 2:17 am

This is the best tutorial I have found does far for a new bigginer


52 khalid February 17, 2015 at 9:40 am

Excellent, Thanks


Leave a Comment

Tagged as: , , , ,

Previous Faq:

Next Faq: