How To Zip (Compress) and Unzip (Extract) Files
|Compressing several files and folders into a single distributable file is the most common way done by many computer users around the world. In Windows, you can simply use Winzip, WinRAR, 7Zip, and so on to zipping and unzipping file. If you’ve installed full Linux distros with GUI, you can also find and use similar app to do similar tasks. But what about a Linux server which you can only access it via SSH client? Realizing that many files are zipped and distributed as .zip file, also that you may need to zip several files into one .zip file so you can download it or transfer it elsewhere, so here it is I tell you how to do zipping and unzipping in a Linux-based VPS or Dedicated server via SSH client (in this case I use Putty, in Linux or Mac you can use Terminal).
Purpose:
I will show you how to zip / compress file and to unzip / decompress / extract file in Linux server via SSH
How to Unzip / extract File?
Step 1.
Open Putty or Terminal then login to your server via SSH
read: How to use Putty to SSH
Step 2.
Once you are logged into your server via SSH, now navigate to the directory where the .zip file you wish to unzip is located there.
read: How to navigate from one directory to another in SSH
Step 3.
Then type following command to try unzipping
unzip [filename].zip
example
unzip screenshot.zip
you may be successful but you may not.
error message:
– bash: unzip: command not found
In this case you have to install it first.
Step 4.
Use following command:
for Debian and Ubuntu:
apt-get install unzip
for Red Hat Linux/Fedora/CentOS users:
yum install unzip
Step 5.
That’s it. Now you can try unzipping it again using command in step 3 above.
How to zip / compress file?
Step 1.
Open Putty or Terminal then login to your server via SSH
Step 2.
Once you are logged into your server via SSH, now navigate to the directory where the files and folders you wish to zip / compress are located there.
Step 3.
Use following command:
zip [zip file name] [file 1] [file 2] [file 3] [file and so on]
example:
zip example.zip 1.txt 2.txt 3.txt
In this example we’ll compress files 1.txt, 2.txt and 3.txt into a single example.zip file
you may be successful but you may not.
error message:
– bash: zip: command not found
In this case you have to install it first.
Step 4.
Use following command to install zip function:
for Debian and Ubuntu:
apt-get install zip
for Red Hat Linux/Fedora/CentOS users:
yum install zip
Step 5.
That’s it. Now you can try zipping it again using command in step 3 above.
FAQs
Q: How to zip all files in current directory?
A: use following command:
zip [zip file name] *
example
zip example2.zip *
p.s: Basically you don’t have to add .zip extension / suffix because the zip command will add this automatically. But we use .zip as suffix to make you understand easier regarding the command and to distinctive which file name should be the .zip name. Here’s another example
zip example4 *
output:
Q: How to zip up an entire directory including all sub-directories
A: use this commane:
zip -r [zip file name] *
example:
zip -r picpic *
Q: How to test a .zip file validity?
A: use this command:
unzip -tq [filename].zip
example:
unzip -tq picpic.zip
Q: How to extract / unzip certain file from a .zip file?
A: use this command:
unzip [filename].zip [thefile]
example:
unzip picpic.zip 1.txt
Q: How to list all files from a .zip file?
A: use this command:
unzip -l [filename].zip
example:
unzip -l picpic.zip
Q: How to extract / unzip all files / folders into the certain directory?
A: use this command:
unzip [filename].zip -d /directoryname
example:
unzip picpic.zip -d /pictures
That’s all most common the zipping and unzipping command you should know. All of those commands are pretty basic command and the most-used ones to manage .zip files. I thing mastering all of those is enough to be able to manage unmanaged VPS or Dedicated server. Especially for newbies..
Thanks 🙂 Really helpful (y)
you are welcome 🙂 hope the tutorial is easy to understand
Nice post with clear instruction and snapshots. I really liked it.
Hello,
I had installed zip by root, and now I can’t use it with other account but root.
How can we enable this command for all user?
Please help me.
Thanks.
Nice tutorial, but I think your missing something.
How do you zip entire directory and creating each files into separate .zip files.
Ex:
satu.txt, dua.txt, tiga.txt, dst.txt
into
satu.zip, dua.zip, tiga.zip, dst.zip
and added if you will…
by the time .zip files created, also remove the original files (satu.txt, dua.txt, tiga.txt, dst.txt) automatically. Maybe after convincing the .zip files is not damage.
Sorry it’s sound alot of work, but it’s what I needed for now and maybe for others as well.
Anyway, It is a nice post you have.
You are awesome.
Thanks, very useful.
thanks lot perfectly described zip and unzip commands.
THKS FROM BRAZIL MAN