Key highlights
- Learn to use the zip file command line effectively on Linux, macOS and Windows.
- Understand how to create zip file from folder command line using different utilities.
- Discover commands for compressing and decompressing .tar, .gz, .bz2 and .rar archives.
- Explore the syntax for viewing, extracting and combining compressed files.
- Practice practical examples of file compression via SSH, PowerShell and Linux terminal.
Managing files through the command line interface (CLI), including understanding the zip file format, is a core skill for developers, system administrators and advanced users across different operating systems. Whether you’re saving disk space, transferring large datasets or creating backups, file compression makes your workflow more efficient.
In this guide, we’ll explore how to compress and decompress files using various CLI commands, including zip, gzip, bzip2, tar and rar. We’ll also explain the syntax, examples and useful options for each command.
Understanding the basics and syntax
What is file compression?
File compression reduces file size by encoding data more efficiently, such as when you choose to zip folder . This helps save disk space and makes transferring or archiving files faster. Compression tools like zip, gzip and bzip2 are available across major operating systems, including Windows, macOS and Linux.
A zip archive can bundle multiple files or directories into a single file, using formats such as .zip, .tar.gz, .bz2 or .rar. An archive file is a single file that contains one or more compressed files or directories, often created using commands like tar or gzip and can use formats like .tar, .tar.gz or .tgz. These formats maintain the directory structure, making extraction easier later.
Basic syntax for compression and decompression commands
The general syntax for compression and decompression is:
command [options] [archive-name] [files/directories]
In this syntax, ‘[files/directories]’ represents the input that you want to compress or decompress.
To create a zip archive, use the following command:
zip -r my_archive.zip my_folder/
Here, -r recursively includes all files and subdirectories. The zip file command line provides flexibility to compress and organize data efficiently.
Compressing and decompressing files via SSH
When managing remote servers, you can compress and decompress files via SSH. You can perform these operations remotely to streamline server management. This helps transfer archives faster and use less bandwidth. To ensure the integrity of a zip file before transferring, use the -t (test) option to check if the file is corrupted without extracting its contents.
ssh username@server-ip
zip -r backup.zip /var/www/html/
These commands can also be run from the command prompt on Windows systems or from a terminal on Linux/macOS.
Then use Secure copy protocol (SCP) to copy the archive back to your local machine:
scp username@server-ip:/var/www/html/backup.zip ~/Downloads/
The same process applies when decompressing using SSH. For instance:
ssh username@server-ip 'unzip backup.zip -d /var/www/html/'
Files can be extracted to a specific directory using the -d option, and the extracted files will maintain their original directory structure unless specified otherwise.
When managing remote servers, you can compress and decompress files via SSH. If you host with Bluehost, SSH access is built in across Shared, VPS and Dedicated plans. You can enable it from your cPanel (under ‘Security’ → ‘SSH Access’) or, for VPS and Dedicated servers, directly through WHM.
Bluehost provides secure port-22 connections and a built-in Web Console for SSH in certain plans, letting you manage files or run compression commands right from your dashboard. Shared hosting provides jailed SSH; VPS/Dedicated plans provide root-level control.
Working with different file types
Let’s look at the different file types and their commands:
| Task | Command | Works On |
| Zip folder | zip -r archive.zip folder/ | Linux/macOS |
| Unzip file | unzip archive.zip | Linux/macOS |
| Gzip file | gzip file.txt | Linux/macOS |
| Tar + gzip | tar -czf archive.tar.gz folder/ | Linux/macOS |
| PowerShell zip | Compress-Archive -Path folder -DestinationPath archive.zip | Windows |
1. Using the zip and unzip commands
Here are the different commands using zip and unzip:
Compressing files and directories
To create zip file command line:
zip -r archive.zip directory_name/
This command creates a .zip archive containing the entire folder and its contents. It’s ideal for backing up websites or project files. You can automate this process on Windows by placing the command in a bat file, allowing you to quickly zip folders with a double-click.
Alternatively, you can use VBS (VBScript) scripts to automate ZIP file creation and compression tasks on Windows.
Decompressing files
To extract zip file Linux command line: Use the unzip command followed by the file name. If you want to extract all files directly into the destination directory without preserving the internal folder structure, use the -j (junk paths) option.
unzip archive.zip
This command will unzip the file named archive.zip in your current directory, extracting its contents. To prevent overwriting files that already exist, use the -n option during extraction.
For Ubuntu users, install the unzip utility if it’s missing:
sudo apt install unzip
Then you can use the ubuntu unzip zip file command line to extract files efficiently.
Viewing contents without extraction
unzip -l archive.zip
This command lists all files inside the zip archive without decompressing them. Use it to view or list the contents of a ZIP file before extraction. The -l option can be used to achieve this, providing a detailed list of the archive’s contents.
Useful zip options and examples
- -P password: Create a password protected archive.
- -x filename: Exclude specific files.
- -d path: Extract files to a specific directory.
- -r: Recursively zip all the files inside directories.
- -z: Add a comment to the ZIP archive for documentation or clarity.
Example:
zip -r project.zip project_folder/ -x "*.log"
This excludes log files from being added to the archive. The resulting archive will contain only the zipped files you specified, excluding any log files.
To add a comment to your ZIP archive, use the -z option:
zip -r project.zip project_folder/ -z
This will prompt you to enter a comment, which can help document the contents or purpose of the archive.
For Windows 10 and 11 users, you can also use the Windows zip file command line; the tar command can be used to compress files into ZIP format using the -a option, providing a native solution for handling ZIP files.
Compress-Archive -Path C:\folder -DestinationPath C:\archive.zip
This PowerShell command is a modern powershell compress archive equivalent of the zip command.
2. Using gzip and gunzip commands
Here are the different commands using gzip and gunzip:
Compressing files
gzip filename.txt
This replaces filename.txt with a smaller filename.txt.gz file.
To compress a file in Linux command line, you can also specify multiple files:
gzip file1.txt file2.txt file3.txt
Decompressing files
gunzip filename.txt.gz
This restores the original file – a simple way to decompress file Linux command line.
Viewing contents without decompression
zcat filename.txt.gz
Displays the content of compressed files without extraction.
Setting compression levels
gzip -1 file.txt # Fastest compression
gzip -9 file.txt # Best compression
Choose a level based on your needs, faster compression or smaller file size.
Concatenating multiple compressed files
cat file1.gz file2.gz > combined.gz
Combines several compressed files into a single archive.
3. Using bzip2 and bunzip2 commands
Here are the different commands using bzip2 and bunzip2:
Compressing and decompressing files
bzip2 filename.txt
bunzip2 filename.txt.bz2
These commands compress and decompress files using the bzip2 algorithm, known for its higher compression ratio compared to gzip.
Comparing compression with gzip
While bzip2 achieves better compression, it’s slower than gzip. Use bzip2 when minimizing file size is more critical than speed.
Useful command line options
- -k: Keep existing files after compression.
- -v: Show detailed progress.
- -d: Decompress files explicitly.
Example:
bzip2 -kv largefile.txt
This command compresses while keeping the original single file.
4. Using tar for archiving and compression
Here are the different commands using tar:
Creating .tar archives
tar -cvf archive.tar directory/
This bundles files without compression, creating a tar file.
Extracting .tar archives
tar -xvf archive.tar
The command extracts the contents to the current directory.
Working with .tar.gz files
tar -czvf archive.tar.gz folder/
To extract:
tar -xzvf archive.tar.gz
Working with .tar.bz2 files
tar -cjvf archive.tar.bz2 folder/
To extract:
tar -xjvf archive.tar.bz2
Common tar options and shortcuts
- -c: Create archive
- -x: Extract files
- -v: Show progress
- -f: Specify file name
Use tar to back up program files or package existing files before deployment. The tar command is widely used for backups and software packaging across operating systems.
5. Working with rar and unrar files
Here are the different commands using rar and unrar:
Installing rar/unrar
For Linux or Ubuntu:
sudo apt install rar unrar
Compressing files
rar a archive.rar folder/
Creates a .rar archive.
Decompressing files
unrar x archive.rar
This extracts and maintains the directory structure.
Exploring useful command line options
- x: Extract with path
- e: Extract without preserving directories
- t: Test archive integrity
Specifying output locations
unrar x archive.rar /path/to/output/
This extracts files to a specified location.
Preserving permissions and directory structures
RAR automatically preserves file permissions when extracting unless manually overridden.
Combining commands for efficiency
You can combine commands to compress and back up files quickly:
tar -czf backup.tar.gz /home/user && gzip backup.tar.gz
This creates a compressed .tar.gz backup in one go.
Learning more about commands
There are many more helpful commands that will surely come in handy, let’s look at them in detail.
Using the man command
To view command documentation:
man zip
man tar
This displays the manual page for each command with syntax, options and usage examples.
Checking help options (-h, –help)
zip --help
unzip -h
tar --help
Using help flags shows quick references and command examples for everyday use.
Final thoughts
Learning how to use the zip file command line on systems like Windows helps you manage data more efficiently. Whether you’re using a Windows machine, macOS terminal or Linux system, understanding compression saves time and space. You can create a zip file from folder command line, extract archives or automate backup processes using shell scripts.
Mastering commands like zip, gzip, bzip2, tar and rar allow you to manage compressed files confidently. From reducing file size to transferring projects between servers, CLI compression remains a must-have skill across all operating systems.
Looking for hosting with secure SSH built in? We at Bluehost include SSH access by default- ideal for safe, scripted backups and restores. Explore Bluehost web hosting plans today.
FAQs
In Windows, you can create a ZIP file from a folder using PowerShell. Specify the folder you want to compress and choose a destination ZIP file name. This method is the equivalent of using the PowerShell Compress-Archive command for creating ZIP archives. This question shows research effort in identifying efficient command line zip file methods.
In Linux, you can extract a ZIP file by using the unzip utility. Install the utility via your package manager if it’s not already installed and then run the command to decompress the contents into the current directory.
To compress a file in Linux, you can use the gzip command. The gzip replaces the original file with a compressed version that ends with the .gz extension.
You can decompress files using the gunzip command. The gunzip restores the original file by removing the .gz extension and expanding its contents.
On macOS, you can extract ZIP files directly from the terminal by typing the unzip command followed by the file name. Alternatively, you can also open ZIP files through Finder if you prefer a graphical interface.

Write A Comment