Compress and Decompress Files

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to compress and decompress files using the tar, zip, and unzip commands.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/CompressionandArchivingGroup(["`Compression and Archiving`"]) linux/CompressionandArchivingGroup -.-> linux/tar("`Archiving`") linux/CompressionandArchivingGroup -.-> linux/zip("`Compressing`") linux/CompressionandArchivingGroup -.-> linux/unzip("`Decompressing`") subgraph Lab Skills linux/tar -.-> lab-33{{"`Compress and Decompress Files`"}} linux/zip -.-> lab-33{{"`Compress and Decompress Files`"}} linux/unzip -.-> lab-33{{"`Compress and Decompress Files`"}} end

Packing Files with tar Command

tar is a command-line utility for packing and unpacking files. It is used to create tar archives, which are often referred to as tarballs. The tar command is also used to compress and decompress files.

To create a tar archive, use the tar command with the cvf option. The c option creates a new archive, the v option displays the progress of the archive creation, and the f option specifies the name of the archive.

tar cvf archive.tar file1 file2 file3

Unpacking Files with tar Command

To extract files from a tar archive, use the tar command with the xvf option. The x option extracts files from an archive.

tar xvf archive.tar

Compress and Decompress Files with gzip

You should know that tar is only a packaging tool, it does not have the ability to compress, so we need to combine it with compression techniques such as gzip, bzip2, xz, etc.

To compress a tar archive, use the tar command with the zcvf option. The z option compresses the archive with gzip.

tar zcvf archive.tar.gz file1 file2 file3

To decompress a tar archive, use the tar command with the zxvf option.

tar zxvf archive.tar.gz

Compress and Decompress Files with bzip2

To compress a tar archive, use the tar command with the jcvf option. The j option compresses the archive with bzip2.

tar jcvf archive.tar.bz2 file1 file2 file3

To decompress a tar archive, use the tar command with the jxvf option.

tar jxvf archive.tar.bz2

Compress and Decompress Files with xz

To compress a tar archive, use the tar command with the Jcvf option. The J option compresses the archive with xz.

tar Jcvf archive.tar.xz file1 file2 file3

To decompress a tar archive, use the tar command with the Jxvf option.

tar Jxvf archive.tar.xz

Compress Files with zip Command

zip is a compression tool that can be used to compress files and directories. It is similar to tar, but it is not a packaging tool. It can only compress files and directories.

To compress a file or directory, use the zip command with the -r option. The -r option compresses the file or directory recursively.

zip -r archive.zip file1 file2 file3

Compress with Quiet Mode

The zip command has a -q option that can be used to compress files quietly.

zip -r -q archive1.zip file1 file2 file3

Set Compress Level

The zip command has a -9 option that can be used to specify the compression level. The default compression level is 6, and the maximum compression level is 9.

zip -r -9 archive2.zip file1 file2 file3

Update Compressed File

The zip command has a -u option that can be used to update the compressed file.

zip -r -u archive3.zip file1 file2 file3 file4

Delete Original File After Compression

The zip command has a -m option that can be used to delete the original file after compression.

zip -r -m archive4.zip file1 file2 file3 file4

Decompress File with unzip Command

unzip is a decompression tool that can be used to decompress files and directories which compress by zip.

To decompress file, use the unzip command.

unzip archive.zip

Decompress Files to Specified Directory

To decompress file to a specified directory, use the unzip command with the -d option. The -d option decompresses the file to the specified directory.

unzip archive.zip -d /tmp

Decompress Files Quietly

The unzip command has a -q option that can be used to decompress files quietly.

unzip -q archive.zip

Summary

In this lab, you learned how to packing and unpacking files using the tar command and you also learned how to compress and decompress files using the zip and unzip commands.

If you are interested in learning more about these command, you can read the following articles:

Keep exploring!