When running a blog or website in a VPS, Dedicated Server or Cloud Server, you will need to upload and/or download something for installation or backup. And it will make the upload or download process more fast that if you compress the files or folders first and extract them when the process is completed.
So, how to compress and extract files in a remote server? The very first step in the process would be to identify the exact archive type by looking at the file extension. The most common archive types are zip (ending with .zip), tar (.tar), Tar+Gunzip (.tar.gz), Bzip (.bz2) and Rar (.rar).
Each archive type has its own command for compressing/extracting as listed below:
I. How to compress files or folders via SSH
For different compressed formats, you need to use different command lines:
To compress a file or folder to a zip file:
zip -r filename.zip file-name
zip -r hello.zip myfile.doc
To compress multiple files and/or folders to a .zip file:
zip -r filename.zip file1 file2 file3 ...
zip -r hello.zip a.txt b.php c.zip ...
To compress a file (ONLY) to a bz2 file:
Bzip2 -zk file
To compress a file (ONLY) to a gz file:
gzip -c file > file.gz
To compress one file or folder to a tar file:
tar -cvf xxx.tar file
To compress multiple files and/or folders to a tar file:
tar -cvf xxx.tar file1 file2 folder1 folder2 ...
To compress one file or folder to a tar.bz2 file:
tar -cvjf filename.tar.bz2 file-example
To compress multiple files and/or folders to a tar.bz2 file:
tar -cvjf filename.tar.bz2 file1 file2 folder1 folder2 ...
To compress one file or folder to a tar.gz file:
tar -cvzf filename.tar.gz file-example
To compress multiple files and/or folders to a tar.gz file:
tar -cvzf filename.tar.gz file1 file2 folder1 folder2 ...
II. How to extract file via SSH
To extract a file will be easier, since you don’t need to worry about folders:
To extract a zip file:
unzip wordpress.zip #Extract wordpress.zip at the current folder in SSH#
unzip admin/haloseeker.com/hello.zip #Extract hello.zip at 'haloseeker.com' folder in 'admin' folder.#
To extract a bz2 file:
To extract a gz file:
gzip -d filename.gz
To extract a tar file:
tar -xvf filename.tar
To extract a tar.bz2 file:
tar -xvjf filename.tar.bz2
To extract a tar.gz file:
tar -xvzf filename.tar.gz
By the way, you need to replace the above “filename”s of the compressed files with the real file names.
The z (-z) is for gzipping which occurs subsequent to the tar (additional compression)
The j (-j) is for bzipping which occurs subsequent to the tar (additional compression)
The d (-d) is for decompressing which is created using gzip command.
The c (-c) is for creating the file.
The v (-v) is for verbose (which I won’t be in this explanation).
The f (-f) is for filename (which is the name that follows — what you call it)
Besides remote servers, the above command lines are also available for a Mac OS computer with the Terminal application. Read more: The Basic SSH Commands – List of Common SSH Commands.