UNIX commands — Compressing directories and files


Websites will often allow you to download a single compressed file that contains many other files. This makes it easier and faster to download through your browser since the file is smaller than the folder containing all the files. These compressed files usually end with these extensions:

  • .zip
  • .gz
  • .tar.gz
  • .tar.bz2

You can decompress these on your computer using your computer's software. Otherwise, you could upload the compressed file to your web server and decompress it there using the commands found in the following article.

But what if you need to do the opposite? What if you need to compress folders on your web server so you can download them to your local computer. All you need to do is log into your server via SSH and run the commands shown below.

Compressing a directory

Compressing to a .zip file

You can use the 'zip' command to compress a folder full of files. For example, if you have a WordPress site named example.com, you may want to compress all files and folders within it before downloading. 

The following command compresses the directory named example.com and creates a new zip file named example.com.zip. The original directory remains untouched.

[server]$ zip -r example.com.zip example.com

You can use the maximum compression setting (-9) which will attempt to compress all files. However, this may be slower:

[server]$ zip -9 -r example.com.zip example.com

Compressing to a .tar.gz file

The following command uses 'tar' to compress an images directory into a file named image_backup.tar.gz.

[server]$ tar zcvf image_backup.tar.gz images

The original folder will continue to exist.

Compressing to a .tar.bz2 file

The following command uses 'tar' to compress an images directory into a file named image_backup.tar.bz2.

[server]$ tar cjvf image_backup.tar.bz2 images

The original folder will continue to exist.

Compressing files

Compressing a file using GZIP

The commands above show how to compress an entire directory. However sometimes you'll only need to compress a single file. To accomplish this you can use gzip. The following command compresses a file named test.log and renames it test.log.gz. Please note that the 'test.log' file will no longer exist since it's been compressed.

[server]$ gzip test.log

If you still need the 'test.log' file to exist, run the following instead. This creates a gzipped copy of the file:

[server]$ gzip -c test.log > test.log.gz

Confirming your folder has been compressed

Once you run the commands above, check to confirm the directory has been compressed. You can do this using du -sh for both the old and new directories.

[server]$ du -sh example.com.zip
30M example.com_backup
[server]$ du -sh example.com
82M example.com

You can see above that the compressed backup is smaller in size than the original folder.

What command compresses a directory the most?

The following shows how much a WordPress site is compressed using the different compression options.

First, check the size of the WordPress directory. This shows it's 82M.

[server]$ du -sh example.com
82M example.com

The following are the results of the different compression methods.

[server]$ du -sh example.com.zip
30M example.com.zip
[server]$ du -sh example.com.tar.gz
27M example.com.tar.gz
[server]$ du -sh example.com.tar.bz2
24M example.com.tar.bz2

Overall, tar.bz2 compresses the most.

See also

Did this article answer your questions?

Article last updated PST.

Still not finding what you're looking for?