20180930 壓縮工具gzip、bzip二、xz

Linux常見壓縮文件擴展名以下:
.zip,.gz,.bz2,.xz,.tar.gz,.tar.bz2,.tar.xzcentos

壓縮文件解壓後可能與源文件大小不一致。工具

gzip壓縮工具

  • 命令格式: gzip 文件名
  • 不能壓縮目錄
[root@centos01 d6]# ls -lh 1.txt
-rw-r--r--. 1 root root 1.3M Sep 30 07:38 1.txt
[root@centos01 d6]#
[root@centos01 d6]# gzip 1.txt  # 壓縮1.txt
[root@centos01 d6]# ls
1.txt.gz
[root@centos01 d6]# ls -lh 1.txt.gz
-rw-r--r--. 1 root root 358K Sep 30 07:38 1.txt.gz

[root@centos01 d6]# gzip -d 1.txt.gz # 解壓縮1.txt.gz
[root@centos01 d6]# ls
1.txt

[root@centos01 d6]# gunzip 1.txt.gz # gunzip解壓1.txt.gz
[root@centos01 d6]# ls
1.txt

[root@centos01 d6]# gzip -1 1.txt # 默認壓縮級別6,範圍1-9 
[root@centos01 d6]# ls
1.txt.gz

[root@centos01 d6]# zcat 1.txt.gz | head -n 5 # 查看壓縮文件(加壓後打開)
# This is an example configuration file for the LVM2 system.
# It contains the default settings that would be used if there was no
# /etc/lvm/lvm.conf file.
#
# Refer to 'man lvm.conf' for further information including the file layout.

[root@centos01 d6]# gzip -c 1.txt > 1.txt.gz  # 壓縮時不刪除文件,並輸出到指定文件
[root@centos01 d6]# ls
1.txt  1.txt.gz

[root@centos01 d6]# gunzip -c 1.txt.gz > 1.cp.txt # 不刪除壓縮文件,解壓爲指定文件
[root@centos01 d6]# ls
1.cp.txt  1.txt  1.txt.gz

bzip2壓縮工具

  • 安裝工具: yum install -y bzip2
  • 命令格式: bzip2 文件名
  • 不支持壓縮目錄
[root@centos01 d6]# bzip2 1.txt  # 壓縮1.txt
[root@centos01 d6]# ls -l
total 1840
-rw-r--r--. 1 root root 1361328 Sep 30 07:50 1.cp.txt
-rw-r--r--. 1 root root  149229 Sep 30 07:38 1.txt.bz2
-rw-r--r--. 1 root root  365757 Sep 30 07:48 1.txt.gz

[root@centos01 d6]# bzip2 -d 1.txt.bz2 # 解壓縮1.txt.bz2
[root@centos01 d6]# ls
1.cp.txt  1.txt  1.txt.gz

[root@centos01 d6]# bzip2 1.txt
[root@centos01 d6]# bunzip2 1.txt.bz2  # 解壓縮1.txt.bz2

[root@centos01 d6]# bzip2 -c 1.txt > 1.tmp.bz2 # 壓縮時不刪除文件,並輸出到指定文件
[root@centos01 d6]# ls
1.cp.txt  1.tmp.bz2  1.txt  1.txt.gz

[root@centos01 d6]# bzip2 -d -c 1.tmp.bz2 > 1.cp2.txt # 壓縮時不刪除文件,並輸出到指定文件
[root@centos01 d6]# ls
1.cp2.txt  1.cp.txt  1.tmp.bz2  1.txt  1.txt.gz

[root@centos01 d6]# bzip2 -9 1.cp.txt  # -9級別,範圍1-9
[root@centos01 d6]# ls
1.cp2.txt  1.cp.txt.bz2  1.tmp.bz2  1.txt  1.txt.gz

[root@centos01 d6]# bzcat 1.tmp.bz2 | head -n 10  # bz2壓縮文件查看
# This is an example configuration file for the LVM2 system.
# It contains the default settings that would be used if there was no
# /etc/lvm/lvm.conf file.
#
# Refer to 'man lvm.conf' for further information including the file layout.
#
# Refer to 'man lvm.conf' for information about how settings configured in
# this file are combined with built-in values and command line options to
# arrive at the final values used by LVM.
#

xz壓縮工具

  • 命令格式: xz 2.txt
  • 使用基本和gzip、bzip2同樣
[root@centos01 d6]# xz 2.txt
[root@centos01 d6]# xz -d 2.txt.xz
[root@centos01 d6]# xz 2.txt
[root@centos01 d6]# unxz 2.txt.xz
[root@centos01 d6]# xz -c 2.txt > 2.cp.txt.xz
[root@centos01 d6]# xz -d -c 2.cp.txt.xz > 2.cp.txt
[root@centos01 d6]# xz 2.txt
[root@centos01 d6]# xzcat 2.txt.xz | head -n 10
# This is an example configuration file for the LVM2 system.
# It contains the default settings that would be used if there was no
# /etc/lvm/lvm.conf file.
#
# Refer to 'man lvm.conf' for further information including the file layout.
#
# Refer to 'man lvm.conf' for information about how settings configured in
# this file are combined with built-in values and command line options to
# arrive at the final values used by LVM.
#
相關文章
相關標籤/搜索