Page Contents
How do you zip files in the Ubuntu command line?
You use the zip command in the following fashion:
1 2 3 4 5 |
zip -r output_file file1 file2 folder1 |
The above command will create a compressed output_file.zip file with file1, file2 and contents of folder1.
I recommend using the recursive option -r
as it allows you to add folders and their content. Without this option, you’ll be adding the folder to the zipped file but the directory contents won’t be included.
Let me show the zip command usage in detail with some actual examples.
Using zip command
You should first ensure that you have the zip command installed on your system.
1 2 3 4 5 |
zip --version |
If the above command returns ‘command ‘zip’ not found’ error, you should install it like this:
1 2 3 4 5 |
sudo apt install zip |
Now, I don’t intend to confuse you, but having a basic idea of syntax will always be beneficial.
1 2 3 4 5 |
zip [option] [output_file_name] [input1] [input2] |
[option]
will be used to accomplish different outcomes regarding zipping files, such as setting up passwords.[output_file_name]
is the name of your zipped file. You can add .zip extension to the output file but if you don’t, it is added automatically.[input1] [input2]
can be various directories and files you want to compress into one zip file
Let’s see some examples now.
Zip a single file
I am starting with the simplest example; zipping a single file in the current directory.
1 2 3 4 5 |
zip output_file.zip input_file |
Here’s an example:
1 2 3 4 5 |
$ zip log_compressed auth.log |
The zip command output mentions the compression ratio.
Did you notice that it automatically added the .zip extension to the output filename even though I didn’t mention it?
Zip multiple files
You can provide multiple files the same way.
1 2 3 4 5 |
zip output_file.zip input_file1 input_file2 input_file3 |
It will show the compression levels for all files.
1 2 3 4 5 6 |
$ zip compressed_log auth.log fontconfig.log alternatives.log |
Zip a folder
Like many other Linux commands, you need to use the recursive option -r
while dealing with the directories.
To zip a folder, just add the -r
in the command:
1 2 3 4 5 |
zip -r output_folder input_folder |
Here’s an example:
1 2 3 4 5 |
$ zip -r logs.zip logs |
If you miss the -r
option, you’ll get the zipped folder but it won’t copy any files.
1 2 3 4 5 6 7 |
$ zip logs.zip logs $ zipinfo logs.zip |
You’ll think that you can combine files from multiple folders and directories into a single zip file like this:
1 2 3 4 5 |
zip -r output_file file1 folder1 folder2/file2 |
You are not wrong but there could be a potential problem with that approach that you may not like.
I’ll show an example so that you understand it better. I zip a file, a folder and a file from another folder together.
1 2 3 4 5 6 |
$ zip -r combined new_file logs/ toto/routes.yaml |
If you look at the contents of the output zip file, you’ll see that routes.yml from the toto directory is named toto/routes.yml. Which means
1 2 3 4 5 |
$ zipinfo combined.zip |
This means that if you extract the combined.zip folder, it will have routes.yaml file in toto subdirectory.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
$ tree combined combined ??? logs ? ??? alternatives.log ? ??? auth.log ? ??? fontconfig.log ??? new_file ??? toto ??? routes.yaml |
While that may be desirable in a few cases, sometimes you just want the files from different folders combined into a single folder, without their directory name and structure.
And in those cases, you combine the -r
with -j
. The -j
option leaves out the path and directory names.
1 2 3 4 5 |
zip -rj output_file file1 folder1 folder2/file2 |
This way, you only get the files.
1 2 3 4 5 6 7 8 |
$ zip -rj combined_out new_file logs/ toto/routes.yaml $ zipinfo combined_out.zip |
Remove files from existing zip file
Added something that should not have been added? You can easily remove unnecessary files from the existing zip file.
1 2 3 4 5 |
zip -d existing_zip File_to_remove_1 File_to_remove_2 |
Let me show with an example.
Here’s the content of the miscallaneous.zip file
Now I want to remove files named “HelloWorld.txt” and “Ringtone.mp3”:
1 2 3 4 5 |
zip -d miscellaneous.zip HelloWorld.txt Ringtone.mp3 |
You can see that those files were no longer present in the zip file.
Add additional files to an existing zip file
I often find myself in situations where I forget to add files while zipping them. No worries. You can add extra files to a zipped file thanks to the update option -u
.
1 2 3 4 5 |
zip -u existing_zip File_to_add_1 File_to_add_2 |
Let’s say I want to add two images to a zip file named “miscellaneous.zip” :
1 2 3 4 5 |
zip -u miscellaneous.zip Burj_Khalifa.jpeg Taj_Mahal.jpeg |
You can see that the files have been added to the existing zip file.
Encrypt zip file with password
This is my favorite feature from the entire catalog of zip utility. It may sound complex, but trust me, it’s not!
Add -e
option along with -r
and it’ll ask for an encryption key phrase before proceeding to zip given files.
1 2 3 4 5 |
zip -r -e output_file input_files input_folders |
Here’s an example screenshot. You can see that it asks for the password twice before initiating the zipping process.
When you use the unzip command to extract this zip file, it asks for the passcode.
?? Move the files into the zip file
By default, the zip utility creates a copy of the original file to make a zip file. If you want, you can avoid this by using -m
which will move files directly into the zipped file.
For example, I’ll be adding three random files to a zip file named NoCopiesLeft:
1 2 3 4 5 |
zip -m NoCopiesLeft helloworld.txt list.txt Snake_plant.jpg |
As you can see, there were three files in the working directory and after making the zip file, no original copies were left.
In the end …
Zip is an excellent tool for reducing directory size. There are many more options and usage of the zip command. You can always refer to the man page for additional information.
You can use the zipinfo
command to list the contents of a zipped file without extracting it on the disk.