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 /root/sg-work/zip-demo $ ls file1.txt file2.txt folder1 folder2 report.txt
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 (stored 0%)
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 (stored 0%) adding: folder1/ (stored 0%)
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 (stored 0%) adding: folder2/docs/ (stored 0%) adding: folder2/docs/readme.md (stored 0%)
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 (stored 0%) adding: folder1/note.txt (stored 0%)
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 36K -rw-r--r-- 1 root root 466 Jan 11 05:16 archive.zip -rw-r--r-- 1 root root 4 Jan 11 05:09 file1.txt -rw-r--r-- 1 root root 4 Jan 11 05:09 file2.txt drwxr-xr-x 2 root root 4.0K Jan 11 05:09 folder1 drwxr-xr-x 4 root root 4.0K Jan 11 05:09 folder2 -rw-r--r-- 1 root root 493 Jan 11 05:16 logs-archive.zip -rw-r--r-- 1 root root 838 Jan 11 05:16 project-backup.zip -rw-r--r-- 1 root root 7 Jan 11 05:09 report.txt -rw-r--r-- 1 root root 177 Jan 11 05:16 report.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 --------- ---------- ----- ---- 0 2026-01-11 05:09 folder2/ 0 2026-01-11 05:09 folder2/docs/ 7 2026-01-11 05:09 folder2/docs/readme.md 0 2026-01-11 05:09 folder2/src/ 23 2026-01-11 05:09 folder2/src/main.c --------- ------- 30 5 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.
