How To Work with Archives in SSH

While working with remote servers it is not unusual to require moving large numbers of files at once. As such, using compression and archival tools can be extremely useful. There are a number of archival/compression tools available.

A few of the most popular ones and the ones we’ll be looking at today in this tutorial are Tar, Tar+Gunzip, BZip2, and Zip.

Each of these compression algorithms can be run remotely and locally on Linux and Mac servers and local machines in an SSH session. Allowing for mass file migration on the fly.

Note: The following command line examples are platform independent, except for Zip commands.

TAR

Tar is an excellent tool for compiling and compressing content. With this command, in combination with a number of options, you can create, maintain and extract files archived in the tar format. Tar actually stands for tape archive. Developed in the early days of Unix, Tar was in backing up computer files to tape-based storage devices. Today it is used to collect, distribute and archive files while preserving file system attributes.

To use the tar command, navigate to the working directory containing the folder you wish to archive and issue the command tar followed by the relevant options, the name of the archive you wish to create and then the name of the folder you wish to archive. Assuming you wish to compress the folder “/collapse” in the root folder “/example”, enter the following command:

tar -c -f collapse.tar collapse/

In the example above the options used were c and f. The “c” switch creates a new archive and the “f” switch declares an archive file.

Other options are as follows:

AAppend tar files to an archive.
DCalculate any differences between the archive and the file system.
–deleteDelete from the archive.
RAppend files to the end of a tar archive.
TList the contents of an archive.
–test-labelTest the archive label, and exit.
UAppend files, but only those that are newer than the copy in the archive.
>XExtract files from an archive.

 

The above command will compress the folder collapse (with everything inside it) that’s in the current working directory to create a single file called collapse.tar.

In instances where you would like to archive several files instead of a single folder, slightly alter the syntax of the same command, listing the desired files separated from each other with a single space instead of the single folder.

tar -c -f collapse.tar file1.doc file2.php file3.c

This command will compile the three files (file1, file2, and file3) into a single file called collapse.tar.

To extract a tar file simply state the command followed by the “x” switch and the archive file. For example:

tar -x -f collapse.tar

Tar+Gzip

Tar+Gunzip provides more advanced options with better compression. To use it we’ll simply add two new options. The Tar+Gunzip syntax is as follows:

tar -cvzf collapse.tar.gz collapse

As you can see, we’re still using the tar command. However, we now have the “v” and “z” switches. The “z” switch is what actually instructs tar to create the archive as a gzip archive. The “v” switch tells tar to compress the folder in verbose form.

What’s happening here is that tar is packaging all the files in the folder into one collective file, while Gzip is applying its superior compression algorithm to the file.

Extracting gzip files which have been created in this manner is, however, a two-fold process.

First, the file needs to be decompressed. This is done by executing the gunzip command on the file. Example:

gunzip collapse.tar.gz

This removes the compression from the file and renders is a .tar file. Now the tar extract function must be run to separate the files:

tar -xvf collapse.tar

Bzip2

The Bzip archive tool while being a little different actually uses the same command as Tar file: tar. To use the tar command for Bzip files simply include the “j” option. Now the example command changes to:

tar -c -f -j collapse.bz2 collapse/

And, just as stated before, to extract the file simply change the “c” switch to “x”.

tar -x -f -j collapse.bz2

ZIP

The classic archive format is .zip. It is most popular with Windows users given programs like Winzip and Windows native support of zip files. You can uncompress files in the ZIP format using the command unzip and, respectively, you can compress content into a zip file with the command zip. The compression power of the ZIP command is, however, less efficient when compared to GZIP or BZIP2, so a file in the zip format containing the same contents will be larger in size. To pack a zip archive just use the zip command followed by the name of the file (including the .zip extension).

Note: Zip is not native to Windows so the follows commands may not apply, unless a Zip command line feature has been installed. The native function for Windows is Compress, however that function is outside the scope of this article.

For example:

zip collapse.zip collapse/

As mentioned before, to extract a zip file, change the syntax to run the unzip function instead.

Unzip collapse.zip

Using this function will uncompress the zip file, placing its contents, in the current working directory.

 

Check out the top 3 Best web hosting services

HostArmada
$1.79 /mo
Starting price
Visit HostArmada
Rating based on expert review
  • User Friendly
    4.5
  • Support
    4.5
  • Features
    4.5
  • Reliability
    4.5
  • Pricing
    4.0
IONOS
$1.00 /mo
Starting price
Visit IONOS
Rating based on expert review
  • User Friendly
    4.5
  • Support
    4.0
  • Features
    4.5
  • Reliability
    4.5
  • Pricing
    4.3
Ultahost
$2.90 /mo
Starting price
Visit Ultahost
Rating based on expert review
  • User Friendly
    4.3
  • Support
    4.8
  • Features
    4.5
  • Reliability
    4.0
  • Pricing
    4.8

How to use SSH to Back Up Your WordPress Website Hosted on a Linux VPS

The default directory path for websites in Linux Servers is usually./var/www/htm
1 min read
Idan Cohen
Idan Cohen
Marketing Expert

How To Set Up SSH for an Ubuntu 16.04 VPS From a Linux Client

How to set up ssh private key authentication on an Ubuntu 16.04 server with a Li
2 min read
Max Ostryzhko
Max Ostryzhko
Senior Web Developer, HostAdvice CTO

How to Set Up SSH on an Ubuntu 16.04 VPS or Dedicated Server

This tutorial shows you how to set up secure shell (SSH) on an Ubuntu 16.04 virt
2 min read
Max Ostryzhko
Max Ostryzhko
Senior Web Developer, HostAdvice CTO

How To Transfer Files From a Remote Server to another Remote Server Using SSH

A relatively common situation is requiring the move of a website from one server
4 min read
Eliran Ouzan
Eliran Ouzan
Web Designer & Hosting Expert
HostAdvice.com provides professional web hosting reviews fully independent of any other entity. Our reviews are unbiased, honest, and apply the same evaluation standards to all those reviewed. While monetary compensation is received from a few of the companies listed on this site, compensation of services and products have no influence on the direction or conclusions of our reviews. Nor does the compensation influence our rankings for certain host companies. This compensation covers account purchasing costs, testing costs and royalties paid to reviewers.
Click to go to the top of the page
Go To Top