In this article, we will show you how to zip and unzip files using SSH. You will learn about compression and extraction methods on your server for better file management.
Compress and decompress file using SSH: everything you need to know
Compressing and extracting files is not only common on desktop computers. You may need to do the same on your VPS. Compressing and decompressing files makes it easy to download and move data.
To perform these actions, you must execute a specific command through the SSH (Secure Shell) protocol on your server.
This process requires you to use an SSH client, such as (Windows) or terminal shell (macOS and Linux), to access your server. If you own a VPS in , you can get the login credential in the tab servers of the hPanel. Don’t know how to connect? Our can help you.
Without further delay, let’s learn how to zip and unzip files using SSH.
Unzip files via SSH connection
First of all, you need to navigate to the directory where your file is. For example, if your file is in the directory public_html/filesthe following command will work:
cd public_html/files
How to unzip .tar.gz
The following command will allow you to uncompress the contents of a .tar.gz file:
tar -zxvf ArchiveName.tar.gz
You will see the visual result of all the files being unzipped and you will be able to run commands again when the process is finished.
How to unzip.tar
This type of compressed file can be extracted with the following command:
tar -xvf ArchiveName.tar
The visual output of the process will be displayed and will be complete when you can type commands again.
How to unzip .zip
This command will take care of decompression of .zip files:
unzip ArchiveName.zip
When the process is finished, the commands will be executable again.
Create files via SSH connection
Now that we know how to unzip all these types of files, we’ll also learn how to compress your files into whatever format you want.
How to create .tar.gz
The method shown below will allow you to compress the selected files into one archive:
tar -zcf NewArchive.tar.gz yourfile1.php yourfile2.php yourfile3.txt
If your intention is to compress an entire directory, this will also work:
tar -zcf NewArchive.tar.gz DirectoryName
As a general rule, the file generation is finished when the commands can be re-executed in your terminal window.
How to create .tar
The syntax for creating .tar files is very similar:
tar -zcf archive-name.tar.gz filename1.php filename2.php filename3.php
If you want to archive a directory, just type the directory name instead of listing the files.
tar -zcf archive-name.tar.gz DirectoryName
When you can type a command again, it means your file has been created.
How to create .zip
This type of file is even easier to create. The syntax is as follows:
zip archive-name.zip filename1.php filename2.php filename3.php
Directory compression also uses the same syntax:
zip archive-name DirectoryName
The screen will show the progress and when finished will allow you to type commands again.
conclusion
Congratulations! Now you know how to zip and unzip files via SSH for easier file management.
There are many file formats available and you can choose the one that suits you best. Please note that the formats .tar Y .tar.gz use a similar command (tar) while the files .zip Y .rar they have their ownzip Y rare respectively). So make sure you enter the correct command for the desired file format.
Let us know in the comments if you have any questions!
Gustavo is passionate about creating websites. He focuses on the application of SEO strategies at for Spain and Latin America, as well as the creation of high-level content. When he is not applying new WordPress tricks you can find him playing the guitar, traveling or taking an online course.