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:[donotprint][/donotprint]

=> Create the archive

=> Compress the archive

=> Additional operations includes listing or updating the archive

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 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
See also:

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Leave a Reply

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

Previous Post

How to Make Website WCAG Compliant?

Next Post

Link download Kali Linux 2020.1 (ISO + Torrent)

Related Posts