Introduction
In this lab, you will learn how to compress and decompress files using the tar
, zip
, and unzip
commands.
In this lab, you will learn how to compress and decompress files using the tar
, zip
, and unzip
commands.
tar
Commandtar
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.
Open a terminal and navigate to the /home/labex/project/
directory.
cd /home/labex/project/
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
tar
CommandTo 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
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
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
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
zip
Commandzip
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
The zip
command has a -q
option that can be used to compress files quietly.
zip -r -q archive1.zip file1 file2 file3
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
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
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
unzip
Commandunzip
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
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
The unzip
command has a -q
option that can be used to decompress files quietly.
unzip -q archive.zip
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!