Linux packaging and compression commands zip, bzip2, gzip, xz, tar

Purpose of packaging and compression: backup, restore

Why do you need to compress the package?

  • Save space when backing up
  • Save time when transferring over the network

What is pack compression:

  • Packaging: Putting many files together into one file
  • Compression: operations that reduce disk space usage

One, zip, bzip2

Zip and bzip2 can only make a compressed package for files, but cannot compress folders .

zip command format:
compression: zip [package compressed file] [original file]
decompression: unzip [package compressed file]

#安装zip,unzip,它不是Linux默认安装
[root@master 410]# yum  install zip unzip -y
#压缩文件
[root@master 410]# zip  passwd.zip         passwd
  adding: passwd (deflated 62%)
  #解压
[root@master backup]# unzip  passwd.zip 

bzip2 command format:
compression: bzip2 [original file]
view: bzcat [package compressed file]
decompression: bunzip2 [package compressed file]

#安装bzip2,它不是Linux默认安装
[root@master lianxi]# yum  install bzip2 -y
#压缩文件
[root@master lianxi]# bzip2 feng.yaml 
#查看文件
[root@master lianxi]# bzcat  feng.yaml.bz2 
#解压文件
[root@master lianxi]# bunzip2 feng.yaml.bz2 

Two, gzip, xz

gzip, xz can only make a compressed package for the file, and cannot xz the folder
. gzip is the default compression tool installed in the linux system

#直接在原文件上进行压缩,添加后缀名.gz
[root@master backup]# gzip passwd  
#查看压缩文件里的内容
[root@master backup]# zcat passwd.gz   
#解压文件
[root@master backup]# gunzip  passwd.gz   
#直接在原文件上进行压缩,添加后缀名.xz
[root@master backup]# xz passwd
#查看压缩文件里的内容
[root@master backup]# xzcat passwd.xz
#解压文件
[root@master backup]# unxz passwd.xz

Compared
with xz, the compression effect is better than that of gzip, which occupies less space after compression, and takes a long time to compress;
gzip has a fast compression speed, but the effect is average.

If the file is relatively small, you can use xz or gzip.
If the file is relatively large, it is recommended to use xz compression, which can save more disk space, but it takes a long time.

Three, tar

Purpose: make archives, release archives

Format:
tar [options] [archive file name] [source file or directory]

Common command options

-c Create a package file in .tar format
-x Unpack the package file in .tar format
-v Output detailed information
-f Indicates to use the archive file
-t View the files in the package
-p Keep the original attributes of the original file
-P Keep the original file The absolute path
-z calls gzip to compress
-J calls xz to compress
-j calls bzip2 to compress
–exclude excludes directories

Compression: (It is recommended to add [.tar. (compression method used)] to the compressed file name for easy identification)
For example: package the passwd file in the current directory with gzip:

#将当前目录下的passwd文件打包成passwd.tar.gz压缩包
[root@master luo]# tar  czf  passwd.tar.gz   passwd   

If the file names are the same, the later packaged files will overwrite the original files

1.tf View the contents of the compressed file

[root@master luo]# tar  tf  hosts.tar.gz 

2. Decompress xf

[root@master luo]# tar xf hosts.tar.gz 

Save the compressed package with an absolute path

[root@master luo]# tar czf  /weihong/passwd.tar.gz   /etc/passwd
tar: 从成员名中删除开头的“/

When decompressing files, do not specify a path, and decompress to the current location by default

-C Unzip the file and specify the storage path

[root@master luo]# tar xf /weihong/passwd.tar.gz   -C /nongda_weihong/

Files and folders can be packed together into a zip file

[root@master luo]# tar  czf   /lianxi/luoyawei/boot_passwd_log.tar.gz   /boot   /etc/passwd  /var/log
tar: 从成员名中删除开头的“/
#将/boot目录下的除grub目录以外的所有文件都备份到/bak目录下叫no-grub.tar.gz
[root@master luo]# tar --exclude=/boot/grub   -czf /bak/no-grub.tar.gz  /boot
#排除多个文件或者文件夹
[root@master luo]# tar --exclude=/boot/{grub2,grub,efi}  -czf /lianxi/luoyawei/no_grub2_boot.tar.gz  /boot

おすすめ

転載: blog.csdn.net/zheng_long_/article/details/129431601