Summarize the compression and decompression tools in Linux and how to use them in detail

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 .targiven 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, file2and , file3into an archive.tararchive named . The option -cmeans to create an archive, and the option -vmeans 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 directorypacks all the files and subdirectories in archive.tarthe directory into an archive named .

1.2 Unzip the file

To unzip tarballthe file, you can use the following command:

tar -xvf archive.tar

This command will tarballextract all files and directories from the file. The option -xmeans to extract the files, and the option -vmeans 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 tarballextracts all the files in the file into the specified directory. option -Cmeans 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 -zto 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, file2and file3zip into a gzip-formatted archive. option -zmeans 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 -xmeans to extract the file, option -zmeans to decompress the archive in gzip format, option -vmeans 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 .gzthe extension.

2.1 Compressed files

To compress files, you can use the following command:

gzip file1

This command file1compresses 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.gzinto 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 .bz2the extension.

3.1 Compressed files

To compress files, you can use the following command:

bzip2 file1

This command file1compresses 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.bz2into 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 .zipthe 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 file1into file2a file3zip-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 directorypacks all the files and subdirectories in a directory into an archive.ziparchive 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.

Guess you like

Origin blog.csdn.net/qq_45172832/article/details/132000239