Linux command to compress files: gzip/bzip2/xz

Table of contents

1. Commonly used compressed file commands

2. Features of file compression format

3. Example of gzip command to compress files

Four, bzip2 command compressed file example

Five, xz command compressed file example


1. Commonly used compressed file commands

In Linux, you can use the following command to compress files:

  1. tar command: pack files or directories into .tar files

    tar -cvf filename.tar file1 file2 ...
    

    This packs the specified files or directories into a .tar file and prints the filenames on the screen. If compression is required, the following command can be used:

    tar -zcvf filename.tar.gz file1 file2 ...
    

    This will pack the specified files or directories into a .tar.gz file.

  2. gzip command: Compress files into .gz format

    gzip filename
    

    This will compress the specified file into a .gz file and delete the original file. If you need to keep the original file and create a compressed file, you can use the following command:

    gzip -c filename > filename.gz
    

  3. zip command: Compress files into .zip format

    zip filename.zip file1 file2 ...
    

    This will pack the specified files or directories into a .zip file.

Note: These commands have many options and parameters, which can be adjusted according to actual needs.

2. Features of file compression format

In Linux, there are many formats available for file compression, each with different compression ratios and performance. The following are common compression formats and their characteristics:

  1. gzip format: use the gzip tool for file compression, the compression is relatively high, but the compression speed is slow, and it is suitable for occasions that require a high compression ratio.

  2. bzip2 format: use the bzip2 tool for file compression, the compression ratio is higher than gzip, but the compression and decompression speeds are slower than gzip, suitable for occasions that require a higher compression ratio.

  3. xz format: use the xz tool for file compression, the compression ratio is higher than bzip2, but the compression speed is very slow, suitable for occasions that require extremely high compression ratio.

The compression ratio and performance of different formats depend on the file type and size, so you can choose the appropriate format according to the actual situation. Usually, gzip is the format with the best balance of performance and compression ratio.

In Linux, you can use the gzip command for file compression and decompression. The gzip tool uses the Lempel-Ziv encoding (LZ77) algorithm for compression, and can only compress a single file.

3. Example of gzip command to compress files

The following is an example of file compression using the gzip command:

  1. Compressed file:

    gzip filename.ext
    

    This command will compress the file "filename.ext" and save the compressed file as "filename.ext.gz".

  2. Compress the file and specify the compression ratio:

    gzip -9 filename.ext
    

    This command compresses the file "filename.ext" with the highest compression ratio (the default compression ratio of gzip is 6), and saves the compressed file as "filename.ext.gz".

  3. Compress multiple files:

    gzip file1.ext file2.ext file3.ext
    

    This command will compress multiple files, each resulting in a corresponding ".gz" compressed file.

The following is an example of file decompression using the gzip command:

  1. unzip files:

    gzip -d filename.ext.gz
    

    This command will decompress the file "filename.ext.gz" and save the decompressed file as "filename.ext".

  2. Unzip multiple files:

    gzip -d file1.ext.gz file2.ext.gz file3.ext.gz
    

    This command will unpack multiple files, each producing a corresponding unpacked file.

Four, bzip2 command compressed file example

In addition to the gzip format, the bzip2 command can also be used in Linux to compress and decompress files. The bzip2 tool uses the Burrows-Wheeler transform and Huffman coding (BWTS) algorithm for compression. The compression ratio is higher than that of the gzip format, but the compression and decompression speed is relatively slow.

The following is an example of file compression using the bzip2 command:

  1. Compressed file:

    bzip2 filename.ext
    

    This command will compress the file "filename.ext" and save the compressed file as "filename.ext.bz2".

  2. Compress the file and specify the compression ratio:

    bzip2 -9 filename.ext
    

    This command compresses the file "filename.ext" with the highest compression ratio (bzip2 default compression ratio is 9), and saves the compressed file as "filename.ext.bz2".

  3. Compress multiple files:

    bzip2 file1.ext file2.ext file3.ext
    

    This command will compress multiple files, each resulting in a corresponding ".bz2" compressed file.

The following is an example of file decompression using the bzip2 command:

  1. unzip files:

    bzip2 -d filename.ext.bz2
    

    This command will decompress the file "filename.ext.bz2" and save the decompressed file as "filename.ext".

  2. Unzip multiple files:

    bzip2 -d file1.ext.bz2 file2.ext.bz2 file3.ext.bz2
    

    This command will unpack multiple files, each producing a corresponding unpacked file.

In addition to gzip and bzip2 formats, Linux can also use the xz command for file compression and decompression. The xz tool uses the LZMA2 algorithm for compression, which has a higher compression ratio than the bzip2 format, but the compression and decompression speed is relatively slow.

The following is an example of file compression using the xz command:

  1. Compressed file:

    xz filename.ext
    

    This command will compress the file "filename.ext" and save the compressed file as "filename.ext.xz".

  2. Compress the file and specify the compression ratio:

    xz -9 filename.ext
    

    This command compresses the file "filename.ext" with the highest compression ratio (the default compression ratio of xz is 6), and saves the compressed file as "filename.ext.xz".

  3. Compress multiple files:

    xz file1.ext file2.ext file3.ext
    

    This command will compress multiple files, each of which will generate a corresponding ".xz" compressed file.

Five, xz command compressed file example

The following is an example of file decompression using the xz command:

  1. unzip files:

    xz -d filename.ext.xz
    

    This command will decompress the file "filename.ext.xz" and save the decompressed file as "filename.ext".

  2. Unzip multiple files:

    xz -d file1.ext.xz file2.ext.xz file3.ext.xz
    

    This command will unpack multiple files, each producing a corresponding unpacked file.

Guess you like

Origin blog.csdn.net/songpeiying/article/details/132104817