Bash Command to Zip a file or directory


If you want to zip a file or a directory while being on a Bash Shell on Terminal, you can make use of the zip command.

Before taking a look at how to use this command, make sure that the zip package is available on your OS - Ubuntu/macOS/Linux. It is common that it is not available if you use an image on Docker Container.

# zip
bash: zip: command not found

Installing zip package:

# apt install zip

....
Preparing to unpack .../unzip_6.0-26ubuntu3.1_arm64.deb ...
Unpacking unzip (6.0-26ubuntu3.1) ...
Selecting previously unselected package zip.
Preparing to unpack .../zip_3.0-12build2_arm64.deb ...
Unpacking zip (3.0-12build2) ...
Setting up unzip (6.0-26ubuntu3.1) ...
Setting up zip (3.0-12build2) ...

Now let's try to zip a file.

# zip data.zip data.txt 
  adding: data.txt (deflated 13%)
Zip a file using bash command

Now let's try to zip a directory. Make sure to add -r flag to zip files within the directory recursively.

# zip -r all_reports.zip reports/

updating: reports/ (stored 0%)
  adding: reports/report_2023.csv (deflated 36%)
  adding: reports/report_2022.csv (deflated 54%)
  adding: reports/report_2021.csv (deflated 55%)
How to zip a directory using bach command
-

Facing issues? Have Questions? Post them here! I am happy to answer!


Author Info:

Rakesh (He/Him) is a seasoned developer with over 10 years of experience in web and app development, and a deep knowledge of operating systems. Author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap