May 25, 2017

How to Compress and Extract a complete Linux or UNIX Directory

Administration Logo

Sometimes it is useful to compress a complete directory. For example to backup the files or to send them to an other server. Transfering a compressed directory will save you some time. In this tutorial we show you how to compress and extract a complete directory on a Linux or UNIX system.

Root access is required to edit the following files and to execute commands. Log in as root (su) or simply prepend sudo to all commands that require root privileges.

Compress a directory

Compress a directory with the following command line. Here are the options we are using:

  • -z: Write the archive through gzip
  • -c: Create a new tar archive
  • -v: Verbose, show the files being worked on as tar is running
  • -f: Specify the name of an archive file

In this example we compress the directory /home/moe with the following command:

tar -zcvf moe.tar.gz /home/moe

The above command will create the archive file moe.tar.gz in the current directory.

Extract a directory

Extract a directory with the following command line. Here are the options we are using:

  • -z: Read the archive through gzip
  • -x: Extract the tar archive
  • -v: Verbose, show the files being worked on as tar is running
  • -f: Specify the name of an archive file
tar -zxvf moe.tar.gz

The above command will extract the archive file moe.tar.gz in the current directory. If you want to extract the archive file in an other directory you can use the -C option. For example to extract the archive file moe.tar.gz in the /tmp directory please use the following commad:

tar -zxvf moe.tar.gz -C /tmp

Speak Your Mind