How to Zip a File in Linux?


Introduction

Linux, the popular open-source operating system, comes with a set of useful tools and commands that you can leverage for everyday tasks. One such task is file compression, and Linux provides various methods to zip or compress files and directories. This guide will walk you through how to zip a file in Linux using commands like 'gzip', 'zip', and 'tar'.

Using the 'gzip' Command

'gzip' (GNU zip) is a widely-used command for file compression in Linux. Here is an example of how to use it −

gzip filename.txt

This command will compress the file 'filename.txt' and rename it to 'filename.txt.gz'. The original file 'filename.txt' will be deleted and replaced with the zipped version.

However, 'gzip' only works on single files. If you want to compress a directory, you'll need to use 'tar' in combination with 'gzip'.

Using the 'tar' and 'gzip' Commands Together

The 'tar' command is a tape archiver that can store and extract files from an archive file. Here is how to use it in conjunction with 'gzip' to compress a directory −

tar -czvf archive_name.tar.gz directory_name/

This command creates a gzip-compressed tar archive named 'archive_name.tar.gz' from the directory 'directory_name'. The 'c' flag creates a new archive, 'z' compresses the archive, 'v' provides verbose output, and 'f' allows you to specify an archive file name.

The output might look something like this −

directory_name/
directory_name/file1.txt
directory_name/file2.txt

Using the 'zip' Command

The 'zip' command is not installed by default in most Linux distributions, but you can install it using the package manager for your specific distribution. For example, on Ubuntu or Debian, you can install it with −

sudo apt-get install zip

Once installed, you can zip a single file like this −

zip archive_name.zip filename.txt

This will create a zip archive named 'archive_name.zip' containing 'filename.txt'.

To zip a directory, use the '-r' (recursive) option −

zip -r archive_name.zip directory_name/

This command recursively compresses the directory 'directory_name' and its contents into 'archive_name.zip'.

The output would be −

adding: directory_name/ (stored 0%)
   adding: directory_name/file1.txt (deflated 36%)
   adding: directory_name/file2.txt (deflated 29%)

Using the 'gzip' Command with Different Compression Levels

The 'gzip' command supports various levels of compression, from 1 (fastest, least compression) to 9 (slowest, maximum compression). The default level is 6. Here is how to use a different level −

gzip -9 filename.txt

This command will compress 'filename.txt' using the maximum compression level.

Using 'tar' and 'gzip' Commands to Exclude Files

If you want to compress a directory but exclude certain files, you can do so using the '--exclude' option with 'tar' −

tar -czvf archive_name.tar.gz --exclude='directory_name/file_to_exclude.txt' directory_name/

This command will create a compressed tar archive of 'directory_name', but the file 'file_to_exclude.txt' will not be included.

Using the 'zip' Command to Add or Remove Files

The 'zip' command also allows you to add or remove files from an existing zip archive −

zip -d archive_name.zip file_to_remove.txt

This command will remove 'file_to_remove.txt' from 'archive_name.zip'.

zip archive_name.zip file_to_add.txt

This command will add 'file_to_add.txt' to 'archive_name.zip'.

Using the 'bzip2' Command

'bzip2' is another compression tool you can use. It often provides better compression rates than 'gzip', but it is also slower.

bzip2 filename.txt

This command will compress 'filename.txt' into 'filename.txt.bz2'. The original file will be replaced by the compressed version. To decompress, use 'bunzip2' −

bunzip2 filename.txt.bz2

This command will decompress 'filename.txt.bz2' back into 'filename.txt'.

Again, just like 'gzip', 'bzip2' does not support compressing directories. You'll need to use it with 'tar' for that purpose −

tar -cjvf archive_name.tar.bz2 directory_name/

This command creates a 'bzip2'-compressed tar archive named 'archive_name.tar.bz2' from the directory 'directory_name'. The 'j' option is used for 'bzip2' compression.

Password Protecting Zip Files

The 'zip' command allows you to password protect your zip archives using the '-e' (encrypt) option −

zip -e secure.zip file.txt

When you run this command, you'll be prompted to enter a password. The file will be compressed into 'secure.zip', and a password will be required to unzip the contents.

Using 'gzip' with Multiple Files

Although 'gzip' primarily works with single files, you can compress multiple files into a single gzip archive by first using 'tar' to bundle the files together −

tar -cf - file1.txt file2.txt | gzip > files.tar.gz

This command creates a tar file ('-cf -') from 'file1.txt' and 'file2.txt', then pipes the tar file ('|') to 'gzip', and redirects the output to 'files.tar.gz'.

Listing the Contents of a Tar or Zip File

You can list the contents of a tar or zip file without extracting it using the '-tf' or '-l' option respectively −

tar -tf archive.tar.gz

This command lists the contents of 'archive.tar.gz'.

unzip -l archive.zip

This command lists the contents of 'archive.zip'.

Using the '7zip' Command

'7zip' is another powerful file archiver with high compression ratios. However, it's not typically installed by default on most Linux distributions. You can install it using your package manager. For example, on Ubuntu or Debian −

sudo apt-get install p7zip-full

To compress a file or directory with '7zip', use the '7z' command −

7z a archive.7z filename.txt

This command compresses 'filename.txt' into 'archive.7z'. The 'a' option indicates we are adding to an archive.

7z a archive.7z directory_name/

This command compresses the 'directory_name' directory into 'archive.7z'.

To extract a '7z' file, use the 'e' (extract) or 'x' (extract with full paths) option −

7z e archive.7z

This command extracts the files from 'archive.7z' to the current directory.

Unzipping Files in Linux

Unzipping files is also a straightforward process in Linux. You can use the 'gunzip' command for 'gzip' files, 'tar' for 'tar.gz' files, and 'unzip' for 'zip' files.

For example −

gunzip filename.txt.gz

This command will decompress 'filename.txt.gz' back into 'filename.txt'.

tar -xzvf archive_name.tar.gz

This command will extract the contents of 'archive_name.tar.gz' into the current directory.

unzip archive_name.zip

This command will extract the contents of 'archive_name.zip' into the current directory.

Conclusion

File compression is an important task for managing storage and transferring files in Linux. Whether you're using 'gzip', 'tar', or 'zip', these commands offer reliable and flexible ways to zip files and directories on a Linux system. Understanding these commands is a fundamental part of becoming proficient in Linux system administration.

Updated on: 17-Jul-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements