Skip to main content

How do I Compress a Whole Linux or UNIX Directory?

highlighted text

Originaly posted by Vivek Gite on

February 11, 2006 last updated September 11, 2015

You are here

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

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

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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
Enter the characters shown in the image.
Joycelyn (not verified) - Mon, 02/05/2018 - 09:52

slot online

slot game

online gambling sites for real money

slot game

pogo casino slots

Edgar (not verified) - Thu, 03/29/2018 - 02:34

online gambling casino

online casino

online casino slots

slot game

online casino games

Alvaro (not verified) - Sun, 01/07/2018 - 23:08

cheap viagra alternative

generic viagra

viagra for sale with no prescription

Hector (not verified) - Fri, 12/15/2017 - 11:02

buy cialis dubai

buy cialis online

cialis cheap prices

Veronique (not verified) - Fri, 12/15/2017 - 16:19

order cialis paypal

generic cialis

cialis pills sale

Gwendolyn (not verified) - Fri, 12/29/2017 - 22:19

where to buy cheap viagra

viagra generique

cheap viagra canada pharmacy

Moises (not verified) - Sat, 12/30/2017 - 07:51

profile buy cheap viagra

viagra generic

viagra online order india

Cierra (not verified) - Tue, 01/02/2018 - 07:40

buy viagra boots

generic viagra india

viagra cheap online no prescription

Clarita (not verified) - Thu, 01/04/2018 - 23:59

viagra online order india

cheap generic viagra

viagra cheap shipping

Tammy (not verified) - Mon, 01/01/2018 - 15:29

viagra pills for sale

viagra generique

cheap viagra pharmacy

Poppy (not verified) - Tue, 01/02/2018 - 18:58

online casino visa electron


best online casino bonuses for us players

Mae (not verified) - Mon, 01/08/2018 - 05:24

viagra cheap pills

viagra generic

viagra online generic cheap

Chi (not verified) - Thu, 01/11/2018 - 18:40

autocad 2011 lt download


buy autocad lt 2017

Roseanna (not verified) - Thu, 01/11/2018 - 18:31

autocad software cost in india

auto cad

autocad 2012 for sale

Aurelio (not verified) - Mon, 02/12/2018 - 02:17

cialis 20mg pills sale
generic tadalafil
buy cialis tijuana

Roseanna (not verified) - Thu, 12/14/2017 - 17:40

cheapest price generic cialis


cheap cialis and levitra

Cameron (not verified) - Fri, 12/15/2017 - 05:03

order cialis online prescription

buy cialis online

lilly cialis pills

Rhoda (not verified) - Wed, 12/27/2017 - 07:28

order viagra in south africa

viagra generique

Raymon (not verified) - Mon, 01/01/2018 - 07:28

online casinos usa allowed

casino real money

mobile casinos us players

Lavonne (not verified) - Wed, 01/03/2018 - 20:07

how to order viagra+singapore

viagra generic 100mg

order viagra online legal

Wayne (not verified) - Thu, 01/04/2018 - 10:12

order viagra australia

generic viagra reviews

can you buy viagra over the counter

Jay (not verified) - Mon, 01/01/2018 - 02:13

online gambling the netherlands

casino online

casino game software download

Patsy (not verified) - Mon, 01/01/2018 - 21:03

cheap viagra safe

generic viagra online

cheap generic viagra overnight delivery


Aliaksandr Yakauleu,

How do I Compress a Whole Linux or UNIX Directory? |


Error message

  • Warning: Cannot modify header information - headers already sent by (output started at /home/mynet/web/ in drupal_send_headers() (line 1232 of /home/mynet/web/
  • PDOException: SQLSTATE[HY000]: General error: 2006 MySQL server has gone away: INSERT INTO {watchdog} (uid, type, message, variables, severity, link, location, referer, hostname, timestamp) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6, :db_insert_placeholder_7, :db_insert_placeholder_8, :db_insert_placeholder_9); Array ( [:db_insert_placeholder_0] => 0 [:db_insert_placeholder_1] => cron [:db_insert_placeholder_2] => %type: !message in %function (line %line of %file). [:db_insert_placeholder_3] => a:6:{s:5:"%type";s:12:"PDOException";s:8:"!message";s:238:"SQLSTATE[HY000]: General error: 2006 MySQL server has gone away: SELECT 1 AS expression FROM {variable} variable WHERE ( (name = :db_condition_placeholder_0) ); Array ( [:db_condition_placeholder_0] =&gt; drupal_http_request_fails ) ";s:9:"%function";s:14:"variable_set()";s:5:"%file";s:64:"/home/mynet/web/";s:5:"%line";i:991;s:14:"severity_level";i:3;} [:db_insert_placeholder_4] => 3 [:db_insert_placeholder_5] => [:db_insert_placeholder_6] => [:db_insert_placeholder_7] => [:db_insert_placeholder_8] => [:db_insert_placeholder_9] => 1575642102 ) in dblog_watchdog() (line 163 of /home/mynet/web/
The website encountered an unexpected error. Please try again later.