On my previous tutorial How to compress file in linux using tar, we discuss on how to compress a single or a multiple file into a single compressed file by using a standard linux archiving tool, called “tar”. But it will be a question when someone ask you How can I compress a whole directory under Linux / Unix machine using tar ?.

So with that question this quick post will guide you “how to compress a whole directory using tar“. It is useful when you want to backup files, for example if you want to backup your /home directory. GNU tar command is the best option to do this work.

So How to tar a directory in linux ? To do this you need to use a tar command like this :

$ tar -zcvf archive.tar.gz directory_name

Explaination for the parameters

  • -z : compress using gzip archiver program
  • -c : create the archive
  • -v : Verbose (display progress while creating the archive)
  • -f : Archive file name


This is an example how to use tar to archive your directory, let we assume you have a directory called /home/mortarboyz and you would like to compress this directory, then you can type the command like this :

$ tar -zcvf backup-home-mortarboyz.tar.gz /home/jerry

With the command above, it will create a tar.gz file under your current working directory.

Now how to restore your files ?, to restore just use the following command :

$ tar -zxvf backup-home-mortarboyz.tar.gz

-x : extract files.

Well that’s all for my quick post “How to tar a directory in linux“, hope with this post will help you to compress and backup your directory. If you have some question or something to say, just leave it on the comment, thank you for your visit !.

Leave a Reply

Your email address will not be published. Required fields are marked *