How To Compress And Extract Files Via SSH

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:

1. Zip

To compress a file or folder to a zip file:

zip -r filename.zip file-name

For example:

zip -r hello.zip myfile.doc

To compress multiple files and/or folders to a .zip file:

zip -r filename.zip file1 file2 file3 ...

For example:

zip -r hello.zip a.txt b.php c.zip ...

2. Bz2

To compress a file (ONLY) to a bz2 file:

Bzip2 -zk file

3. Gz

To compress a file (ONLY) to a gz file:

gzip -c file > file.gz

4. Tar

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 ...

5. Tar.bz2

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 ...

6. Tar.gz

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:

1. Zip

To extract a zip file:

unzip filename.zip

For example:

unzip wordpress.zip #Extract wordpress.zip at the current folder in SSH#

Or:

unzip admin/haloseeker.com/hello.zip #Extract hello.zip at 'haloseeker.com' folder in 'admin' folder.#

2. Bz2

To extract a bz2 file:

bunzip2 filename.bz2 

3. Gz

To extract a gz file:

gzip -d filename.gz 

4. Tar

To extract a tar file:

tar -xvf filename.tar

5. Tar.bz2

To extract a tar.bz2 file:

tar -xvjf filename.tar.bz2 

6. Tar.gz

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)

Bonus:

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.

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
bandar Recent comment authors
  Subscribe  
Notify of
bandar
Guest

First off I would like to say wonderful blog!
I had a quick question which I’d like to ask if you
don’t mind. I was curious to know how you
center yourself and clear your head prior to writing.
I’ve had a tough time clearing my thoughts in getting my thoughts out there.
I truly do enjoy writing however it just seems like the first 10 to 15 minutes are generally lost just trying to figure out how to begin. Any
ideas or hints? Thank you!