Creating compressed archives in Linux streamlines backups, sharing, and deployment by packing multiple items into a single file. Using the zip format keeps archives compatible with Windows, macOS, and Linux, which simplifies collaboration and transfer across heterogeneous environments.
The zip utility reads a list of files and directories from the command line and writes a compressed archive using the Deflate algorithm by default. File names, directory structure, and timestamps are stored inside the archive so that unpacking reconstructs the layout relative to the extraction directory.
Archive creation does not modify the original files, but command choices still affect which paths are captured, how deep directory recursion goes, and where the resulting .zip file is written. Clear paths, careful use of options such as -r, and quick verification with listing commands reduce the risk of missing or unintended content in the final archive.
Steps to zip files and folders in Linux:
- Open a terminal on the Linux system.
$ echo $SHELL /bin/bash
- Navigate to the directory that contains the files or folders to compress.
$ cd /path/to/directory/ $ ls file1.txt file2.txt folder1 folder2
Commands operate on paths relative to the current working directory unless absolute paths are used.
- Create a .zip archive from a single file.
$ zip report.zip report.txt adding: report.txt (deflated 63%)
Archive file report.zip is created in the current directory if it does not already exist.
- Create a .zip archive from multiple files and folders.
$ zip archive.zip file1.txt file2.txt folder1/ adding: file1.txt (stored 0%) adding: file2.txt (deflated 53%) adding: folder1/ (stored 0%) adding: folder1/note.txt (deflated 47%)
Any directory arguments cause contained files to be added relative to that directory name.
- Include an entire directory tree recursively using the -r option.
$ zip -r project-backup.zip folder2/ adding: folder2/ (stored 0%) adding: folder2/src/ (stored 0%) adding: folder2/src/main.c (deflated 60%) adding: folder2/docs/ (stored 0%) adding: folder2/docs/readme.md (deflated 55%)
Option -r walks subdirectories so every file under folder2 is stored in the archive.
- Exclude specific patterns while zipping recursively when large or transient files are present.
$ zip -r logs-archive.zip folder1/ -x "*.tmp" "*.bak" adding: folder1/ (stored 0%) adding: folder1/app.log (deflated 75%)
Option -x filters out paths that match the given patterns, which prevents unnecessary files from inflating archive size.
- Confirm that the .zip archive exists and review its size.
$ ls -lh total 12M -rw-r--r-- 1 user user 4.0M Sep 14 12:00 report.zip -rw-r--r-- 1 user user 5.5M Sep 14 12:01 archive.zip -rw-r--r-- 1 user user 2.5M Sep 14 12:02 project-backup.zip
File size depends on the original data and how well it compresses; some binary formats may show little reduction.
- List the contents of a .zip archive without extracting any files.
$ unzip -l project-backup.zip Archive: project-backup.zip Length Date Time Name --------- ---------- ----- ---- 4096 2024-09-14 11:58 folder2/ 12345 2024-09-14 11:58 folder2/src/main.c 4096 2024-09-14 11:58 folder2/docs/ 2345 2024-09-14 11:58 folder2/docs/readme.md --------- ------- 18886 4 filesListing output confirms which files are stored in the archive and how they are laid out inside the directory structure.
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.
Comment anonymously. Login not required.
