Packaging and File Compression in Linux

File packaging and file compression are commonly used data transmission methods in linux, which can ensure the integrity of data; this article mainly introduces several commonly used packaging and compression methods in work

1. Application

1.1 Data transfer between linux and windows

Sometimes we will copy the data in linux to windows for viewing. The commonly used file compression formats are zip and rar

zip file compression format

compression command illustrate
zip a.zip ./* Compress the files in the current directory into a.zip, excluding the files in the folder
zip -r a.zip ./* Recursively compress files in the current directory into a.zip, including files in folders
unzip a.zip Unzip the file to the current folder
unzip ./a.zip -d ./folder Unzip the file into the ./folder folder

rar file compression format

compression command illustrate
rar a t.rar ./* Compress the files in the current directory into a.zip, including the files in the folder
rar a -r t.rar ./* Recursively compress files in the current directory into a.zip, including files in folders
unrar e t.rar Unzip files to the current folder, just unzip the files, but include the files in the folder
unrar x t.rar Unzip files to current folder, just unzip files and folders
unrar e ./t.rar -d ./folder Unzip the file into the ./folder folder
unrar x ./t.rar -d ./folder Extract the files into the ./folder folder, including the folder
unrar and ./t.zip -o ./folder Unzip the file into the ./folder folder
unrar x ./t.rar -o ./folder Extract the files into the ./folder folder, including the folder

1.2 Data transfer in linux

tar && gz
packs the file through tar, and then compresses the file through gz

compressed file type illustrate
tar -cvf t.tar ./* Pack all files and folders in the current directory into t.tar
tar -xvf t.tar Extract t.tar to the current directory
tar -xvf t.tar -C /home/user/ Extract t.tar to the specified directory

Compress while packing

compressed file type illustrate
tar -zcvf t.tar.gz ./* Pack all files and folders in the current directory into t.tar.gz
tar -jcvf t.tar.bz2 ./* Pack all files and folders in the current directory into t.tar.bz2
tar -zxvf t.tar.gz Extract t.tar.gz to the current directory
tar -zxvf t.tar.gz -C /home/user/ Extract t.tar.gz to the specified directory

Explanation of compression parameters:

-c: create a new compressed file
-z: use the gzip algorithm to compress to a .gz file
-v: display the compression process
-f: specify the compressed file name

Explanation of decompression parameters:

-x: unpack/extract a compressed file
-z: uncompress a .gz file compressed using the gzip algorithm
-v: display the decompression process
-f: specify the compressed file name

Cyclic decompression of tar.gz files

for file in *.tar.gz; do tar -zxvf "$file"; done
root@**:**# ls
md.tar.gz  txt.tar.gz
root@**:**# ls for file in *.tar.gz; do tar -zxvf "$file"; done
./1.md
./2.md
./d.md
./a.txt
./b.txt

2. Description and introduction of advantages and disadvantages

2.1 gz

.gz is a common type of file compression found on Linux systems. It is compressed and generated by the GNU compression tool Gzip, and its underlying layer uses the DEFLATE algorithm. The .gz file format is a lossy compression format that is easy to use and is widely used in file compression for Internet transmission and storage. Here are some features of .gz file compression:

  • High compression rate: The .gz file compression method adopts DEFLATE algorithm and Lempel-Ziv algorithm, so the compression rate is relatively high, which can effectively reduce storage space.

  • Fast compression speed: .gz files are implemented based on the DEFLATE algorithm, which is fast and will not be too slow when compressing larger files.

  • Support single file compression: .gz file can compress a single file into a .gz compressed package, which is convenient for transmission and sharing.

  • Does not support multi-file compression: .gz files do not support multi-file compression, you need to pack the files to be compressed into a tar package first, and then compress the tar package into a .tar.gz file.

  • Generate a .gz file after compression: After the .gz file is compressed, the extension of the file name becomes .gz, for example, after the info.txt file is compressed, the file name is info.txt.gz.

In short, the .gz file compression type is one of the common Linux file compression types. It has the advantages of high compression rate and fast compression speed, and is widely used in file transmission and storage.

2.2 zip

In Linux systems, ZIP is a common type of file compression, compared to tar, gzip and bzip2, ZIP compression format is a cross-platform compression format, often used to share files between windows and Linux systems. Here are some characteristics of the ZIP file compression type:

  • Moderate Compression: Compared to gzip and bzip2, ZIP generally has a moderate compression rate.
  • Multiple files can be compressed: ZIP supports compressing multiple files into one ZIP archive.
  • Support file encryption: ZIP files can be encrypted to protect file privacy.
  • Easy to use: The ZIP compression tool can be used on Linux and Windows systems, and it is very convenient to use.
  • Faster compression: ZIP compression is generally slower than bzip2 and faster than gzip.

In conclusion, ZIP is a common type of cross-platform file compression that is very convenient in many scenarios. ZIP can effectively compress multiple files into one file, and can also add or delete individual files, with medium compression rate and fast processing speed, and can password protect your files.

When using the zip command, the following options can be used to achieve different functions:

-r: recurse the directory to the compressed file;
-q: do not display any output information when performing the operation;
-m: move the source file to the compressed file;
-u: compress only the files that do not exist in the source file;
- j: Only store the file name and content, without compressing the directory structure information;
-1 ~ -9: Specify the compression level, the larger the value, the higher the compression efficiency and the longer the compression time;

2.3 rar

In Linux systems, RAR is a commonly used archive compression format, which can be used on Linux, Windows, macOS and other platforms. The RAR compression format is not as commonly used in Linux as other compression formats such as zip, tar, gzip, bzip2, etc., but the RAR compression format is very common in Windows.

Features of the RAR archive compression format include:

  1. High compression ratio: The RAR format has a relatively high compression ratio, allowing you to store more files in a small space.

  2. Volume compression: RAR can divide a large file into multiple small files for easy transmission.

  3. Support encryption: Through encryption, the privacy of files can be better protected.

  4. Self-extracting: Allows you to create a self-extracting package, which can be directly decompressed without any compression software.

In Linux, you can create, extract and manage RAR archives with RAR archive software such as unrar and rar. In terms of basic operations, RAR compression commands are similar to commands in gzip, zip and other formats, including creating compressed files, decompressing files, etc.

2.4 tar

In Linux, the tar command is a command for packaging and archiving, which can pack one or more files or directories into a tar archive file, which can be compressed into a gzip format file with the -z option or compressed with the -j option. Compressed into a bzip2 format file.

The basic syntax of tar is as follows:

tar [选项] 文件名.tar [要被打包的文件]

Among them, the options can be:

  • -c: Create a new tar archive.
  • -x: Extract files from a tar archive.
  • -v: Displays ongoing operations.
  • -f: Specifies the name of the archive file.
  • -z: Use gzip format for compression.
  • -j: Use bzip2 format for compression.
  • -t: List the contents of the tar archive.
  • -r: Add files to the tar archive.
  • -u: Update an existing tar archive.
  • -P: Preserve the full file path.

Guess you like

Origin blog.csdn.net/shouhu010/article/details/130562986