How do I Compress a Whole Linux or UNIX Directory?

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 or BSD tar command is best for this work. It can be use on remote Linux or UNIX server. It does two things for you:
Tutorial details
Difficulty level Easy
Root privileges No
Requirements tar
Est. reading time 1 mintue
  1. Create the archive
  2. Compress the archive
  3. Additional operations includes listing, deleting or updating the archive files.

How to compress a whole directory in Linux or Unix

You need to use the tar command as follows (syntax of tar command):
tar -zcvf archive-name.tar.gz source-directory-name

  • -z : Compress archive using gzip program in Linux or Unix
  • -c : Create archive on Linux
  • -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

  • -x: Extract files from given archive

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 -

Compress an entire directory to a single file

To compress directory named /home/vivek/bin/ in to a /tmp/bin-backup.tar.gz type the tar command on Linux:
tar -zcvf /tmp/bin-backup.tar.gz /home/vivek/bin/

You can compress multiple directories too:
tar -zcvf my-compressed.tar.gz /path/to/dir1/ /path/to/dir2/

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

How to use bzip2 compression instead of gzip compression

The syntax is:
tar -jcvf my-compressed.tar.bz2 /path/to/dir1/

  • -j : Compress archive using bzip2 program in Linux or Unix
  • -c : Make archive on Linux
  • -v : Verbose output
  • -f my-compressed.tar.bz2 : Store archive in my-compressed.tar.bz2 file


You learend how to comporess a whole directory in Linux, macOS, *BSD and Unix-like systems using the zip command/tar command. See also:

See the zip and tar command man page documentation by typing the following man command:
man tar
man zip

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 72 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
72 comments… add one
  • Alpesh Mar 17, 2006 @ 11:43

    Excellent !
    Step by step tutorial, easy to understand.


  • BinhNT Feb 27, 2007 @ 8:14

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

  • Matthew Cupples Mar 15, 2007 @ 23:20

    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?

    • Paulo A. Costa Feb 18, 2012 @ 0:53

      try: gunzip -rv /directoryname

      • Angie. LW Jun 23, 2016 @ 8:35

        how about archive a folder?

  • rokcy Mar 16, 2007 @ 8:25

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

    • AAAA Jan 17, 2016 @ 8:22

      so verbose…

      • BBBB Jan 20, 2016 @ 16:15

        much compression wow

        • CCCC Jan 21, 2016 @ 22:44

          mom’s spaghetti

  • omprakash Mar 19, 2007 @ 7:53

    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.

  • Leion Aug 16, 2007 @ 2:39

    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?

  • Mark Henry Apr 2, 2008 @ 13:39

    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?

  • Eduardo Romero Apr 2, 2008 @ 17:35

    To zip the files try:

    zip /home/jerry/prog/*

  • 🐧 nixCraft Apr 2, 2008 @ 19:22


    The -r option recurse into directories,

    zip -r .
    zip -r /path/to/directory
  • val Apr 7, 2008 @ 13:01

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

  • Elambarithi Apr 22, 2008 @ 13:58

    easy to understand..

  • Jeff Dec 14, 2008 @ 6:21

    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”

  • Sebastian May 31, 2009 @ 21:17

    THANKS! :)

  • Chaitanya Jul 28, 2009 @ 7:04

    Hey Man its really gr8
    Thanks for the help

  • Jen Aug 6, 2009 @ 21:09

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

  • Hippy Mar 25, 2010 @ 0:18

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

  • ihraz Apr 6, 2010 @ 7:09


  • Shounka Apr 15, 2010 @ 6:55

    Thanks, its really very helpful!

  • Tuhin Paul Jun 27, 2010 @ 3:23

    Thank you very much.
    Truly very helpful.

  • eta Aug 15, 2010 @ 1:29

    Thanks for compress in linux tutorial.

  • Praveen Oct 13, 2010 @ 10:09


    Thanks for ur excellent help it did a lot for meeeeeeeeeeeeeee

  • vishal sapkal Oct 20, 2010 @ 15:15

    very good
    can u give how to compress detail directory

  • sekhar Nov 24, 2010 @ 5:29

    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 ?

  • Tsrlinkan Jan 25, 2011 @ 22:06

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

  • avi Feb 9, 2011 @ 12:12


  • Adeel Feb 12, 2011 @ 12:25

    Thanks, easy to understand and excellent guide.

  • HiTux Mar 21, 2011 @ 17:16

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

  • Nicola Hamwi Apr 9, 2011 @ 12:20

    Excellent tutorial !!!

  • Saeasy May 17, 2011 @ 22:58

    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

  • Junaid Jul 7, 2011 @ 6:46

    Nice article

  • Daniel Clem Aug 16, 2011 @ 9:02

    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,

  • Mike Sep 29, 2011 @ 11:02

    Thanks for helping out – very straightforward.
    Well done.

  • matthew Sep 30, 2011 @ 5:20

    Nice clear and concise tutorial !! Well done


  • Pratik Jan 17, 2012 @ 8:57

    Thanks a ton :)

  • Paulo A. Costa Feb 18, 2012 @ 0:52

    @Matthew Cupples:

    try: gunzip -rv /directoryname

  • Matthias Feb 21, 2012 @ 9:53

    Thanks, man!

  • Ahmed Adel Feb 29, 2012 @ 0:18

    Very useful
    Thank you

  • sudhi May 14, 2012 @ 3:30

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

  • Surjit May 30, 2012 @ 7:47

    GReat !!. now becomes expert in ssh commands

  • great Jul 24, 2012 @ 12:02

    Great tutorial cyberciti!

  • Mark Oct 5, 2012 @ 15:02

    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.

  • gery Nov 26, 2012 @ 3:11

    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.

  • brahmabotlagunta Dec 6, 2012 @ 21: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.

  • marsh Feb 21, 2013 @ 15:21

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

    • Kolimba Mar 15, 2013 @ 10:46

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

  • chuche May 18, 2013 @ 3:49

    Thanks for the help!

  • kishore Jun 28, 2013 @ 10:02

    Its really awesome command…… Thanx a lot…

  • Habib Oct 29, 2013 @ 19:49


    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,


  • mike Jan 21, 2014 @ 20:11

    omg —

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

    “can i do this on directories?”

    fakn try it!

  • Nollan Oct 9, 2014 @ 2:17

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

  • khalid Feb 17, 2015 @ 9:40

    Excellent, Thanks

  • murah Aug 2, 2015 @ 5:23

    thanks, i’ve been looking this tut to compress my directory

  • ofer Sep 10, 2015 @ 5:15


  • Persian Gulf Sep 10, 2015 @ 9:28

    Great tips, Thanks.

  • Kainlite Sep 11, 2015 @ 20:41

    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

  • Barbara Ngeniform Oct 8, 2015 @ 18:03

    these commands worked great. but how do I recover a non recoverable tar.gz file. is that possible?

  • Rajesh Mylsamy Dec 15, 2015 @ 10:10

    Thanks. Good explanation.

  • Renni Dec 22, 2015 @ 23:48

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

  • AguThadeus Jan 13, 2016 @ 9:34

    Thank you very much, it helped a lot

  • TheHorror Feb 8, 2016 @ 9:01

    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

  • gstream Apr 12, 2016 @ 3:37

    Thank you very much. Nice tutorial.

  • Houssine Oul Jun 18, 2016 @ 23:39

    Compress a Whole Linux or UNIX Directory using SSH : tar -zcvf archive-name.tar.gz /

  • tj Sep 28, 2016 @ 10:44

    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/'


  • Raja Oct 25, 2016 @ 4:22

    I’m using non GNU.. and in your explanation, May I knw what is file1,file2 and dir3 in your command for non-GNU?? Please

  • Prahlad Apr 5, 2017 @ 9:21

    very helpful.

  • jack Mar 1, 2021 @ 7:54


  • Michael R. Apr 5, 2021 @ 12:05

    Very thorough and easy to understand. Thank you to the author of this article!

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum