linux compression and decompression

Keywords: Linux


Command:[ root@localhost ~]#tar [options] source file or directory   Common: tar -cvf  

-cPackage multiple files or directories.
-AAppend the tar file to the archive file.
-f package nameSpecifies the file name of the package. The package extension is used to identify the format for the administrator, so you must specify the extension correctly;
-vDisplay the process of packaging files;

Instances: packaging files  

#Format: tar -cvf target file (TAR) original file

[root@VM-4-17-centos ~]# tar -cvf a.tar a.txt

Instances: packaging folders

The same as the packaged file

[root@VM-4-17-centos ~]# tar -cvf databack.tar ./databack

Example: package multiple files and directories into a tar file

[root@VM-4-17-centos ~]# tar -cvf page.tar a.txt ./databack

The above is the packaging, which is different from Windows. In fact, it has not been compressed

zip compression and decompression

zip compression

Zip [options] compressed package name source file or source directory list   Common: zip -rv     Package compressed folder

-rRecursively compress the directory, and compress all files and subdirectories under the specified directory.
-mAfter compressing the file, deleting the original file is equivalent to moving the file to the compressed file.
-vDisplays detailed compression process information.
-qThe execution process of the command is not displayed during compression.
-Compression levelThe compression level is a number from 1 to 9, - 1 means faster compression speed and - 9 means better compression effect.
-uUpdate the compressed file, that is, add a new file to the compressed file.

Case: compressing multiple files

[root@VM-4-17-centos ~]# zip
  adding: (deflated 24%)
  adding: (stored 0%)
  adding: (deflated 70%)

Case: compressed directory is processed recursively with - r  

[root@VM-4-17-centos ~]# zip -r ./databack
  adding: databack/ (stored 0%)
  adding: databack/de178_11.sql (deflated 76%)
  adding: databack/0910running2.sql (deflated 70%)
  adding: databack/testa_091901.sql (deflated 70%)
  adding: databack/running2_0919.sql (stored 0%)
  adding: databack/dataall0919.sql (deflated 78%)
  adding: databack/09102.sql (deflated 78%)

Unzip unzip

#unzip [options] zip package name

-d directory nameUnzip the compressed file to the specified directory.
-nDecompressing does not overwrite existing files.
-oWhen decompressing, the existing files are overwritten without user confirmation.
-vView the details of the compressed file, including the file size, file name and compression ratio contained in the compressed file, but do not decompress.
-tTest the compressed file for damage, but do not unzip it.
-x file listUnzip the file, but do not include the file specified in the file list.

Common case: unzip the compressed package to the specified directory   Use: - d

[root@VM-4-17-centos ~]# unzip -d  ./una/

gzip compression and decompression

gzip compression    * gizip can only compress files, not directories

  gzip [options] source file

-cExport compressed data to standard output and keep the source file.
-dDecompress the compressed file.
-rRecursively compress all files in the specified directory and subdirectories.
-vFor each compressed and decompressed file, the corresponding file name and compression ratio are displayed.
-lFor each compressed file, the following fields are displayed:
  • The size of the compressed file;
  • The size of the uncompressed file;
  • Compression ratio;
  • The name of the uncompressed file.
-NumberUsed to specify the compression level, - 1 has the lowest compression level and the worst compression ratio- 9. The compression ratio is the highest. The default compression ratio is - 6.

Case 1: file compression

# gzip page.tar

Case 2: preserve source file compression

# gzip -c a.tar>a01.tar.gz

gunzip decompression

#gunzip [options] file

-rRecursive processing, decompress all files in the specified directory and subdirectory.
-cOutput the decompressed file to the standard output device.
-fForcibly decompress the file regardless of whether the file already exists.
-lLists the contents of the compressed file.
-vDisplays the command execution process.
-tTest whether the compressed file is normal, but do not decompress it

Direct decompression:

# gunzip databack.tar.gz

Posted by savingc on Wed, 22 Sep 2021 00:34:45 -0700