How to compress (tar/gzip) an entire directory in Linux (command line)

How to compress (tar/gzip) an entire directory in Linux (command line)

Last Updated: 1/24/2105

This article applies to the following services:
  • VPS (Linux only)
  • Dedicated servers (Linux only)

You'll want to use the tar command to compress the directory (and everything below the directory)

For a firm understanding of what you are doing, it is good to understand the switches you are using.

-z = Compress archive using gzip program
-c = Create archive
-v = Verbose (i.e. display the progress while creating the archive)
-f = Archive file name
-x = Extract files
-C = Path to folder you want to extract the files to

An example of compressing a folder:

tar zcvf nameofcompressedfile.tar.gz  /path/to/folder/compressthisfolder

An example of decompressing a folder:

NOTE: Make sure you are in the folder that you want to unpack the files into.

tar zvfz nameofcompressedfile.tar.gz

If you want to extract the files to a particular directory, you'll want to use the following command.  (In this example, we will extract to /tmp

tar zvfz nameofcompressedfile.tar.gz -C /tmp

  • 7 Users Found This Useful
Was this answer helpful?

Related Articles

How to install Linux Maldat (Linux Malware Scanner)

How to install Linux Maldat (Linux Malware Scanner)Last Updated: 1/07/2015This article applies to...

How to delete all error_log files on cPanel Servers

How to delete all error_log files on cPanel ServersLast Updated: 1/07/2015This article applies to...

How to find the file size of folders in a directory via Linux command line

How to find the file size of folders in a directory via Linux command lineLast Updated:...

How to reboot Apache on a cPanel machine from the Linux command line

How to reboot Apache on a cPanel machine from the Linux command lineLast Updated: 1/07/2015This...

How to export (mysqldump) and import a mysql database via command line

How to export (mysqldump) and import a mysql database via command lineLast Updated: 1/24/2015This...