How to zip a folder in Ubuntu Linux / Debian Linux

am a new Ubuntu Linux user. I would like to compress a folder named data in my home directory. How do I zip a folder in Ubuntu Linux? How do I compress a directory in Ubuntu Linux?

To compress archive files, use zip command. The zip is a compression and file packaging utility for Linux and Unix command. A companion program called unzip unpacks zip archives. This page shows how to compress a folder on Ubuntu Linux or Debian Linux using the CLI and GUI methods.

Tutorial details
Difficulty levelEasy
Root privilegesYes
RequirementsLinux terminal
CategoryArchiving
OS compatibilityDebian  Linux  Mint  Pop!_OS  Ubuntu
Est. reading time4 minutes

zip a folder in Ubuntu Linux using the cli

First install the zip command using apt command or apt-get command. Open the terminal and type the following command:
sudo apt install zip unzip

How do I use zip command to compress a folder?

The syntax is
zip -r filename.zip folder
zip -r filename.zip folder1 folder2
zip -r filename.zip /path/to/folder1 /path/to/file2

To create compressed archive named data.zip of data folder in the current directory, run:
zip -r data.zip data/

Verify file with the ls command:
ls -l data.zip
You can encrypt data.zip with a password by passing the -e option:
zip -r -e data.zip data/
Sample outputs:

Enter password: 
Verify password: 
  adding: data/ (stored 0%)
  adding: data/music/ (stored 0%)
  adding: data/nightmare.jpg (deflated 2%)
  adding: data/resolv.conf (deflated 16%)
  adding: data/network.jpg (deflated 0%)
  adding: data/acct/ (stored 0%)
  adding: data/acct/MSR-201711.PDF (deflated 4%)
  adding: data/acct/0XL72233P04252837.pdf (deflated 32%)

Printing a specific zip archive content

Try passing the -sf as follows:
zip -sf </path/to/compressed.zip>
zip -sf /tmp/foo.zip

Outputs:

 etc/adduser.conf
  etc/apg.conf
  etc/appstream.conf
  etc/brltty.conf
  etc/ca-certificates.c
....
...
  etc/usb_modeswitch.conf
  etc/xattr.conf
Total 40 entries (117653 bytes)

Getting help

zip command has many more options as follows:

OptionDescription
-ffreshen: only changed files
-uupdate: only changed or new files
-ddelete entries in zipfile
-mmove into zipfile (delete OS files)
-rrecurse into directories
-jjunk (don’t record) directory names
-0store only
-lconvert LF to CR LF (-ll CR LF to LF)
-1compress faster
-9compress better
-qquiet operation
-vverbose operation/print version info
-cadd one-line comments
-zadd zipfile comment
-@read names from stdin
-omake zipfile as old as latest entry
-xexclude the following names
-iinclude only the following names
-Ffix zipfile (-FF try harder)
-Ddo not add directory entries
-Aadjust self-extracting exe
-Jjunk zipfile prefix (unzipsfx)
-Ttest zipfile integrity
-XeXclude eXtra file attributes
-ystore symbolic links as the link instead of the referenced file
-eencrypt
-ndon’t compress these suffixes
-h2show more help

Compress a directory in Ubuntu Linux

The zip command syntax is as follows to compress a directory in Ubuntu Linux:
zip -r compressed_data.zip /path/to/foldername
zip -r compressed_data.zip /home/vivek/Jan-2018

zip a folder in Ubuntu Linux using the GUI method

To access and organize your files you use “Files” app (file manager)”. Use the Files file manager to browse and organize the files on your computer. Open it. Select folder name such as data and right click the “Compress…“:

data.zip created using GUI method 599W

Gif 01: Compressing a folder in Ubuntu Linux using GUI method i.e. Files file manager

Password protecting zip file

We can encrypt and password protect our zip file as follows:
zip -r -e output.zip /path/to/folder/
We can also use and state password on the CLI to encrypt zipfile entries:
zip -r -e -P 'YOUR_PASSWORD_HERE' output.zip /path/to/folder/

Unziping files in Linux

To extract all files/directories/folder from given archives into the current directory:
unzip file.zip
unzip foo.zip bar.zip

You can extract files/directories/folders from archives to a specific directory path. For example, extract files into the /tmp/ directory:
unzip file.zip -d /tmp
Want to list the contents of a specific archive without extracting them? Try:
unzip -l archive.zip

Conclusion

You just learned how to compress a directory in Ubuntu Linux or Debian Linux using the zip command. The same command used to zip a folder in Linux. For more info, read it using the man command/help command. For example:
man zip
man unzip
zip --help | grep -Ew -- '-(r|f|u)'

  • 0 Comments
  • 4 Views
  • Share:

We may use cookies or any other tracking technologies when you visit our website, including any other media form, mobile website, or mobile application related or connected to help customize the Site and improve your experience. learn more

Allow