Compressing and decompressing files is a common operation in Linux. Sometimes, we need to compress large files into smaller files for easy transfer and storage. At the same time, we also need to decompress the file to get the raw data. In this article, we will introduce the compression and decompression commands commonly used in Linux.
1. Detailed explanation of tar command
The tar command is one of the most commonly used compression and decompression commands in Linux. It can pack multiple files and directories into a single archive. tar archives are usually .tar
given the extension.
1.1 Compressed files
Compressing files with the tar command is very simple. Below is an example:
tar -cvf archive.tar file1 file2 file3
This command compresses the files file1
, file2
and , file3
into an archive.tar
archive named . The option -c
means to create an archive, and the option -v
means to display the progress of the archive on the screen.
If you want to package the entire directory into one archive, you can use the following command:
tar -cvf archive.tar directory/
This command directory
packs all the files and subdirectories in archive.tar
the directory into an archive named .
1.2 Unzip the file
To unzip tarball
the file, you can use the following command:
tar -xvf archive.tar
This command will tarball
extract all files and directories from the file. The option -x
means to extract the files, and the option -v
means to display the extraction progress on the screen.
If you want to extract the files into a specific directory, you can use the following command:
tar -xvf archive.tar -C /path/to/directory
This command tarball
extracts all the files in the file into the specified directory. option -C
means change directory.
1.3 Compression and archiving at the same time
Sometimes, we need to compress and archive at the same time. The tar command provides an option -z
to compress the archive into gzip format, and you can use the gzip command to decompress the gzip format file. Below is an example:
tar -czvf archive.tar.gz file1 file2 file3
This command compresses a file file1
, file2
and file3
zip into a gzip-formatted archive. option -z
means to compress the archive.
To decompress a file in gzip format, the following command can be used:
tar -xzvf archive.tar.gz
This command will extract all files and directories from the gzipped file. option -x
means to extract the file, option -z
means to decompress the archive in gzip format, option -v
means to display the extraction progress on the screen.
2. Detailed explanation of gzip command
gzip is a separate compression tool that can compress files into gzip format. gzip files usually use .gz
the extension.
2.1 Compressed files
To compress files, you can use the following command:
gzip file1
This command file1
compresses the file into a gzip formatted file file1.gz
.
2.2 Unzip the file
To decompress a file in gzip format, the following command can be used:
gzip -d file1.gz
This command decompresses a gzip-formatted file file1.gz
into a raw file file1
.
3. Detailed explanation of bzip2 command
bzip2 is another commonly used compression tool, which can compress files into bzip2 format. bzip2 files usually use .bz2
the extension.
3.1 Compressed files
To compress files, you can use the following command:
bzip2 file1
This command file1
compresses the file into a bzip2 formatted file file1.bz2
.
3.2 Unzip the file
To decompress a file in bzip2 format, the following command can be used:
bzip2 -d file1.bz2
This command decompresses a file in bzip2 format file1.bz2
into a raw file file1
.
4. Detailed explanation of the zip command
Zip is a commonly used compression format, usually used in Windows systems. However, Linux systems also support the zip format. Zip files usually use .zip
the extension.
4.1 Compressed files
To compress files, you can use the following command:
zip archive.zip file1 file2 file3
This command compresses and compresses files file1
into file2
a file3
zip-format archive archive.zip
.
If you want to package the entire directory into one archive, you can use the following command:
zip -r archive.zip directory/
This command directory
packs all the files and subdirectories in a directory into an archive.zip
archive named .
4.2 Unzip the file
To unzip a file in zip format, you can use the following command:
unzip archive.zip
This command will extract all files and directories from the zip format file.
If you want to extract the files into a specific directory, you can use the following command:
unzip archive.zip -d /path/to/directory
This command extracts all the files in the zip format file into the specified directory.
V. Summary
The four tools are introduced above, but you only need to master one or two. Here we recommend you to master the tar command and the zip command. These two commands are also the most used compression tools in the Linux environment. tar is suitable for packaging a large number of files, gzip is suitable for files that need to be stored or transmitted for a long time, bzip2 is suitable for files that need to save storage space, and zip is suitable for transferring files between different operating systems. Which tool to choose depends on your specific needs.