Skip to main content
How To

Zip a Folder in Ubuntu Command Line

Learn how to zip a folder in Ubuntu command line. Also learn to exclude files and subdirectories while creating zip file.

Abhishek

When you try to zip a folder in Ubuntu, you'll notice that it creates an empty zip file.

root@learnubuntu:~# zip toto.zip toto
  adding: toto/ (stored 0%)

The reason here is that by default, zip expects a file, not a folder. And like many other Linux commands, you'll have to use the recursive option to deal with the directories.

To zip a folder, use it like this:

zip -r output.zip input_folder

Let's see it in a bit more detail and with proper examples.

Zip a directory

Many Linux commands like rm, cp, scp etc use the recursive option -r while dealing with the folders. The zip command is no different.

As you have already seen above, the way to create a zip archive from a folder is to use it in the following fashion:

zip -r output.zip input_folder

Let me show it with an example. I try to zip a folder named toto in the usual (but incorrect) way:

root@learnubuntu:~# zip toto.zip toto
  adding: toto/ (stored 0%)
root@learnubuntu:~#

And as you can see, the zipped folder doesn't store the contents of the folder.

root@learnubuntu:~# ls -lh toto.zip
-rw-r--r-- 1 root root 160 Oct 27 05:12 toto.zip
root@learnubuntu:~#

Now, I use the same command but with the recursive option -r:

root@learnubuntu:~# zip -r toto.zip toto
updating: toto/ (stored 0%)
  adding: toto/ads.txt (deflated 80%)
  adding: toto/gnome-console-voiceover (deflated 57%)
  adding: toto/members.2022-05-29.csv (deflated 59%)
  adding: toto/cronjob-cheatsheet.png (deflated 8%)
  adding: toto/routes.yaml (deflated 27%)
  adding: toto/bash.pdf (deflated 22%)
  adding: toto/apt-get.pdf (deflated 8%)
  adding: toto/.member.csv (deflated 59%)

You can clearly see that the files are being added to the zipped folder now.

Zip folder in Ubuntu command line
Unlocator Smart DNS
Remove geographic blocks from streaming services using Unlocator Smart DNS. Simple to use and with a full free trial included.

Zip several folders and files in one zip file

You are not limited to zipping a single folder. You can zip multiple folders into one.

In fact, you can combine folders and files.

zip -r output.zip file1 folder1 file2 file3 folder2

Here's an example:

root@learnubuntu:~# zip -r single.zip auth.log toto logs alternatives.log 
  adding: auth.log (deflated 89%)
  adding: toto/ (stored 0%)
  adding: toto/ads.txt (deflated 80%)
  adding: toto/gnome-console-voiceover (deflated 57%)
  adding: toto/members.2022-05-29.csv (deflated 59%)
  adding: toto/cronjob-cheatsheet.png (deflated 8%)
  adding: toto/routes.yaml (deflated 27%)
  adding: toto/bash.pdf (deflated 22%)
  adding: toto/apt-get.pdf (deflated 8%)
  adding: toto/.member.csv (deflated 59%)
  adding: logs/ (stored 0%)
  adding: logs/toto/ (stored 0%)
  adding: logs/toto/ads.txt (deflated 80%)
  adding: logs/toto/gnome-console-voiceover (deflated 57%)
  adding: logs/toto/members.2022-05-29.csv (deflated 59%)
  adding: logs/toto/cronjob-cheatsheet.png (deflated 8%)
  adding: logs/toto/routes.yaml (deflated 27%)
  adding: logs/toto/bash.pdf (deflated 22%)
  adding: logs/toto/apt-get.pdf (deflated 8%)
  adding: logs/toto/.member.csv (deflated 59%)
  adding: logs/alternatives.log (deflated 39%)
  adding: logs/auth.log (deflated 89%)
  adding: logs/fontconfig.log (deflated 86%)
  adding: alternatives.log (deflated 39%)

Zip folder but exclude some files and subdirectories

You may not need all the files of a folder while creating the zip archive file. The good news is that you can exclude files and sub-directories while zipping them.

You can provide the files and subdirectories to exclude with option -x.

zip -r output.zip my_file my_folder -x my_folder/my_sub_folder

Do note that the option -x comes after the output and input files.

In the example below, you can see the file bash.pdf and subdirectory new is not

root@learnubuntu:~# zip -r output.zip auth.log toto -x toto/new toto/bash.pdf 
  adding: auth.log (deflated 89%)
  adding: toto/ (stored 0%)
  adding: toto/ads.txt (deflated 80%)
  adding: toto/gnome-console-voiceover (deflated 57%)
  adding: toto/members.2022-05-29.csv (deflated 59%)
  adding: toto/new/ (stored 0%)
  adding: toto/cronjob-cheatsheet.png (deflated 8%)
  adding: toto/routes.yaml (deflated 27%)
  adding: toto/.member.csv (deflated 59%)

As you can see, it still adds the sub-directory but not its contents. If you don't want the subdirectory at all, use it like this:

zip -r output.zip my_file my_folder -x "my_folder/my_sub_folder/*"

You can see that subdirectory new is not included at all this time.

root@learnubuntu:~# zip -r simple.zip auth.log toto -x "toto/new/*" toto/bash.pdf 
  adding: auth.log (deflated 89%)
  adding: toto/ (stored 0%)
  adding: toto/ads.txt (deflated 80%)
  adding: toto/gnome-console-voiceover (deflated 57%)
  adding: toto/members.2022-05-29.csv (deflated 59%)
  adding: toto/cronjob-cheatsheet.png (deflated 8%)
  adding: toto/routes.yaml (deflated 27%)
  adding: toto/.member.csv (deflated 59%)

Conclusion

Well, there is a lot more to the zip command in Linux. The focus here was on zipping a folder in Ubuntu and this article covers it well.

Let me know if you still have some doubts.