压缩工具
  compress/uncompress:对应 .Z 结尾的压缩格式文件
  压缩格式:gz、bz2、xz、zip、Z
  gzip  压缩文件并删除源文件(生成.gz的文件)
  gunzip 解压缩文件(gzip -d有相同的功能)
  bzip2   压缩文件(压缩比例比gzip更高后缀为.bz2)
  bunzip2 解压缩文件(bzip -d有相同的功能)

  压缩算法不同,压缩比也会不同
gzip
  gzip /PATH/TO/SOMEFILE 压缩完成后会删除原文件
  -d 解压缩
  -# 指定压缩比(1-9),默认是6
  -c 将压缩结果送往标准输出,可以使用重定向将其保存为压缩文件,从而保留原文件
  例子:
  gzip -c /PATH/TO/SOMEFILE > SOMEFILE.gz
gunzip
  gunzip /PATH/TO/SOMEFILE.gz  解压完成后会删除原文件
  zcat /PATH/TO/SOMEFILE.gz      不解压的情况,查看文本文件的内容
  z系列命令,可以在不经解压的情况下,直接操作gzip压缩文件
  zcat  直接显示压缩文件的内容
  zless 直接逐行显示压缩文件的内容
  zdiff 直接报告压缩文件的差异内容
  zcmp 直接报告压缩文件的差异处
演示:
[root@centos7 ~]# cp /var/log/messages /tmp/test/
[root@centos7 ~]# ll /tmp/test/
总用量 288
-rw-r----- 1 root root 0 2月 20 13:41 a
-rw-rw-rw- 1 root root 0 2月 20 13:41 b.danger
-r--r----- 1 root root 0 2月 20 13:41 c
-rwxrwxr-x 1 root root 0 2月 20 13:41 d
-rwxrwxrwx 1 root root 0 2月 20 13:41 e.danger
-rw-r--r-- 1 root root 0 2月 20 13:41 f
-rw-r--r-- 1 root root 0 2月 20 13:41 g
-rw------- 1 root root 292504 2月 20 16:28 messages
[root@centos7 ~]# ll -h /tmp/test/messages
-rw------- 1 root root 286K 2月 20 16:28 /tmp/test/messages

# 压缩,删除原文件,保留压缩后以.gz结尾的文件
[root@centos7 ~]# gzip /tmp/test/messages

[root@centos7 ~]# ll -h /tmp/test
总用量 44K
-rw-r----- 1 root root 0 2月 20 13:41 a
-rw-rw-rw- 1 root root 0 2月 20 13:41 b.danger
-r--r----- 1 root root 0 2月 20 13:41 c
-rwxrwxr-x 1 root root 0 2月 20 13:41 d
-rwxrwxrwx 1 root root 0 2月 20 13:41 e.danger
-rw-r--r-- 1 root root 0 2月 20 13:41 f
-rw-r--r-- 1 root root 0 2月 20 13:41 g
-rw------- 1 root root 41K 2月 20 16:28 messages.gz

# 解压缩,原来的压缩文件被删除,保留解压缩后的文件
[root@centos7 ~]# gunzip /tmp/test/messages.gz
[root@centos7 ~]# ll -h /tmp/test
总用量 288K
-rw-r----- 1 root root 0 2月 20 13:41 a
-rw-rw-rw- 1 root root 0 2月 20 13:41 b.danger
-r--r----- 1 root root 0 2月 20 13:41 c
-rwxrwxr-x 1 root root 0 2月 20 13:41 d
-rwxrwxrwx 1 root root 0 2月 20 13:41 e.danger
-rw-r--r-- 1 root root 0 2月 20 13:41 f
-rw-r--r-- 1 root root 0 2月 20 13:41 g
-rw------- 1 root root 286K 2月 20 16:28 messages

# zcat可以查看压缩的文件,不建议对大文件使用zcat命令查看
[root@centos7 ~]# zcat /tmp/test/messages.gz

#=====================================================================================
# 解压缩
[root@centos7 ~]# gzip -d /tmp/test/messages.gz
[root@centos7 ~]# ll /tmp/test/
总用量 288
-rw-r----- 1 root root 0 2月 20 13:41 a
-rw-rw-rw- 1 root root 0 2月 20 13:41 b.danger
-r--r----- 1 root root 0 2月 20 13:41 c
-rwxrwxr-x 1 root root 0 2月 20 13:41 d
-rwxrwxrwx 1 root root 0 2月 20 13:41 e.danger
-rw-r--r-- 1 root root 0 2月 20 13:41 f
-rw-r--r-- 1 root root 0 2月 20 13:41 g
-rw------- 1 root root 292504 2月 20 16:28 messages

# 将压缩或解压缩的结果输出至标准输出(gzip -c FILE > /PATH/TP/SOMEFILE.gz)
[root@centos7 ~]# gzip -c /tmp/test/messages > /tmp/test/messages.gz
[root@centos7 ~]# ll /tmp/test/
总用量 332
-rw-r----- 1 root root 0 2月 20 13:41 a
-rw-rw-rw- 1 root root 0 2月 20 13:41 b.danger
-r--r----- 1 root root 0 2月 20 13:41 c
-rwxrwxr-x 1 root root 0 2月 20 13:41 d
-rwxrwxrwx 1 root root 0 2月 20 13:41 e.danger
-rw-r--r-- 1 root root 0 2月 20 13:41 f
-rw-r--r-- 1 root root 0 2月 20 13:41 g
-rw------- 1 root root 292504 2月 20 16:28 messages
-rw-r--r-- 1 root root 41791 2月 20 16:44 messages.gz

# 解压缩到标准输出
[root@centos7 ~]# rm -f /tmp/test/messages
[root@centos7 ~]# gzip -d -c /tmp/test/messages.gz > /tmp/test/messages
[root@centos7 ~]# ll /tmp/test/
总用量 332
-rw-r----- 1 root root 0 2月 20 13:41 a
-rw-rw-rw- 1 root root 0 2月 20 13:41 b.danger
-r--r----- 1 root root 0 2月 20 13:41 c
-rwxrwxr-x 1 root root 0 2月 20 13:41 d
-rwxrwxrwx 1 root root 0 2月 20 13:41 e.danger
-rw-r--r-- 1 root root 0 2月 20 13:41 f
-rw-r--r-- 1 root root 0 2月 20 13:41 g
-rw-r--r-- 1 root root 292504 2月 20 16:50 messages
-rw-r--r-- 1 root root 41791 2月 20 16:44 messages.gz

bzip2
  比gzip有着更大压缩比的压缩工具,使用格式近似
  bzip2 /PATH/TO/SOMEFILE
  -d 解压缩
  -# 指定压缩比(1-9),默认是6
  -k 压缩解压时保留原文件
bunzip2
  bunzip2 /PATH/TO/SOMEFILE.bz2 解压完成后会删除原文件
  bzcat /PATH/TO/SOMEFILE.bz2 不解压的情况,查看文本文件的内容
演示:
# 压缩
[root@centos7 ~]# bzip2 /tmp/test/messages

[root@centos7 ~]# ll -h /tmp/test/
总用量 72K
-rw-r----- 1 root root 0 2月 20 13:41 a
-rw-rw-rw- 1 root root 0 2月 20 13:41 b.danger
-r--r----- 1 root root 0 2月 20 13:41 c
-rwxrwxr-x 1 root root 0 2月 20 13:41 d
-rwxrwxrwx 1 root root 0 2月 20 13:41 e.danger
-rw-r--r-- 1 root root 0 2月 20 13:41 f
-rw-r--r-- 1 root root 0 2月 20 13:41 g
-rw-r--r-- 1 root root 26K 2月 20 16:50 messages.bz2 #压缩后的结果
-rw-r--r-- 1 root root 41K 2月 20 16:44 messages.gz

# 解压缩
[root@centos7 ~]# bunzip2 /tmp/test/messages.bz2
[root@centos7 ~]# ll -h /tmp/test/
总用量 332K
-rw-r----- 1 root root 0 2月 20 13:41 a
-rw-rw-rw- 1 root root 0 2月 20 13:41 b.danger
-r--r----- 1 root root 0 2月 20 13:41 c
-rwxrwxr-x 1 root root 0 2月 20 13:41 d
-rwxrwxrwx 1 root root 0 2月 20 13:41 e.danger
-rw-r--r-- 1 root root 0 2月 20 13:41 f
-rw-r--r-- 1 root root 0 2月 20 13:41 g
-rw-r--r-- 1 root root 286K 2月 20 16:50 messages # 解压缩后的结果
-rw-r--r-- 1 root root 41K 2月 20 16:44 messages.gz

# -k 选项不用指明重定向的文件,自动保留源文件在当前文件中
[root@centos7 ~]# bzip2 -k /tmp/test/messages
[root@centos7 ~]# ll -h /tmp/test/
总用量 360K
-rw-r----- 1 root root 0 2月 20 13:41 a
-rw-rw-rw- 1 root root 0 2月 20 13:41 b.danger
-r--r----- 1 root root 0 2月 20 13:41 c
-rwxrwxr-x 1 root root 0 2月 20 13:41 d
-rwxrwxrwx 1 root root 0 2月 20 13:41 e.danger
-rw-r--r-- 1 root root 0 2月 20 13:41 f
-rw-r--r-- 1 root root 0 2月 20 13:41 g
-rw-r--r-- 1 root root 286K 2月 20 16:50 messages
-rw-r--r-- 1 root root 26K 2月 20 16:50 messages.bz2
-rw-r--r-- 1 root root 41K 2月 20 16:44 messages.gz

xz
  xz /PATH/TO/SOMEFILE
  -d 解压缩
  -# 指定压缩比(1-9),默认是6
  -k 压缩解压时保留原文件
unxz
  unxz /PATH/TO/SOMEFILE.xz 解压完成后会删除原文件
  xzdec /PATH/TO/SOMEFILE.xz 解压文件显示到终端上
  xzcat /PATH/TO/SOMEFILE.xz 不解压的情况,查看文本文件的内容

注意:以上压缩工具只能压缩文件,不能压缩目录,如果指定目录中的所有文件/PATH/TO/*会将目录中的每个文件压缩成一个压缩文件,所以在压缩目录的时候还需要归档工具
演示:
[root@centos7 ~]# xz /tmp/test/messages
[root@centos7 ~]# ll -h /tmp/test/
总用量 96K
-rw-r----- 1 root root 0 2月 20 13:41 a
-rw-rw-rw- 1 root root 0 2月 20 13:41 b.danger
-r--r----- 1 root root 0 2月 20 13:41 c
-rwxrwxr-x 1 root root 0 2月 20 13:41 d
-rwxrwxrwx 1 root root 0 2月 20 13:41 e.danger
-rw-r--r-- 1 root root 0 2月 20 13:41 f
-rw-r--r-- 1 root root 0 2月 20 13:41 g
-rw-r--r-- 1 root root 26K 2月 20 16:50 messages.bz2
-rw-r--r-- 1 root root 41K 2月 20 16:44 messages.gz
-rw-r--r-- 1 root root 21K 2月 20 16:50 messages.xz

zip  既归档又压缩的工具(window下面的.zip的文件可以直接在linux下解压)
  zip FILENAME.zip FILE1 FILE2 ... 压缩多个文件到一个目录中,压缩后不删除原文件
  unzip FILENAME.zip
  如果要压缩默认目录,要通过指定目录的所有文件/PATH/TO/*才能压缩整个目录下的文件,如果指定的是/PATH/TO/就只会压缩TO这个目录本身并不包括目录中的文件
  zip /PATH/TO/ /PATH/TO/SOMEFILE.zip 只是压缩了TO目录
  zip /PATH/TO/* /PATH/TO/SOMEFILE.zip 压缩了TO目录中的所有文件
演示:
# 对目录进行归档并压缩
[root@centos7 test]# zip /tmp/test/tao.zip tao
adding: tao/ (stored 0%)
[root@centos7 test]# ll
总用量 188
-rw-r--r-- 1 root root 12097 2月 20 17:42 boot.log
-rw-r--r-- 1 root root 26074 2月 20 16:50 messages.bz2
-rw-r--r-- 1 root root 41791 2月 20 16:44 messages.gz
-rw-r--r-- 1 root root 21420 2月 20 16:50 messages.xz
-rw-r--r-- 1 root root 40960 2月 20 17:51 mylog.tar
-rw-r----- 1 root root 0 2月 20 17:42 pacemaker.log
drwxr-xr-x 2 root root 121 2月 20 17:43 tao
-rw-r--r-- 1 root root 7232 2月 20 18:15 tao.tar.gz
-rw-r--r-- 1 root root 158 2月 20 18:26 tao.zip
-rw-r--r-- 1 root root 2800 2月 20 17:42 wpa_supplicant.log
-rw-r--r-- 1 root root 18303 2月 20 17:42 Xorg.0.log
-rw------- 1 root root 105 2月 20 17:42 yum.log

[root@centos7 test]# rm -fr tao

# 解压缩
[root@centos7 test]# unzip tao.zip
Archive: tao.zip
creating: tao/
[root@centos7 test]# ll
总用量 188
-rw-r--r-- 1 root root 12097 2月 20 17:42 boot.log
-rw-r--r-- 1 root root 26074 2月 20 16:50 messages.bz2
-rw-r--r-- 1 root root 41791 2月 20 16:44 messages.gz
-rw-r--r-- 1 root root 21420 2月 20 16:50 messages.xz
-rw-r--r-- 1 root root 40960 2月 20 17:51 mylog.tar
-rw-r----- 1 root root 0 2月 20 17:42 pacemaker.log
drwxr-xr-x 2 root root 6 2月 20 17:43 tao
-rw-r--r-- 1 root root 7232 2月 20 18:15 tao.tar.gz
-rw-r--r-- 1 root root 158 2月 20 18:26 tao.zip
-rw-r--r-- 1 root root 2800 2月 20 17:42 wpa_supplicant.log
-rw-r--r-- 1 root root 18303 2月 20 17:42 Xorg.0.log
-rw------- 1 root root 105 2月 20 17:42 yum.log

tar 归档压缩工具(archive归档本身并不意味着压缩) 
  主要参数:
  -f 指定创建或展开归档文件名(只要操作归档文件就要用-f)
  -c 创建归档文件
  -v 显示创建的详细过程
  -x 展开归档文件
  -r 将文件添加到已经存在的归档文件中(压缩后是不能用r的,只有归档的时候用)
  -z 用gzip来压缩和解压缩文件
  -j 用bz2来压缩和解压缩文件
  -J 用xz来压缩和解压缩文件
  -t 不展开归档文件情况下查看文件中的内容
  -C 展开或解压文件到指定目录(如果没指定该参数,默认是当前目录)
  --xattrs 归档时,保留文件的扩展属性信息

  -zcf 归档并调用gzip压缩
  -zxf 调用gzip解压缩并展开归档,-z选项可省略(解压时自动判断用什么工具压缩)

  -jcf 归档并调用bzip2压缩
  -jxf 调用bzip2解压缩并展开归档,-j选项可省略

  -Jcf 归档并调用xz压缩
  -Jxf 调用xz解压缩并展开归档,-J选项可省略

  例如:
  tar -cvf home.tar /home 将home目录打包成home.tar(只是打包并没有压缩)
  tar -rvf home.tar /etc 将etc目录添加到已存在的打包文件home.tar里面
  tar -czvf home.tar.gz /home 将home目录打包并压缩成home.tar.gz
  tar -cvjf home.tar.bz2 /home 将home目录打包并压缩成.bz2的格式
  tar -xzvf home.tar.gz 将home.tar.gz这个备份文件还原并解压缩
  tar -tvf home.tar | more 查看home.tar备份文件的类容,并以分屏方式显示在显示器上
  tar -cvf /dev/st0 /home/shrek 可以将st0磁带机备份到/home目录下面

注意:f选项一定要写在最后一个;如果是解开压缩归档文件可以不指定压缩选项zjJ,tar会通过文件后缀判断是什么压缩工具压缩的
  归档演示:
[root@centos7 ~]# ls /tmp/test/tao
boot.log fstab issue pacemaker.log wpa_supplicant.log Xorg.0.log yum.log

# 对所有以 .log 结尾的文件进行归档
[root@centos7 ~]# tar -cf /tmp/test/mylog.tar /tmp/test/tao/*.log
[root@centos7 ~]# ll /tmp/test/
总用量 136
-rw-r--r-- 1 root root 26074 2月 20 16:50 messages.bz2
-rw-r--r-- 1 root root 41791 2月 20 16:44 messages.gz
-rw-r--r-- 1 root root 21420 2月 20 16:50 messages.xz
-rw-r--r-- 1 root root 40960 2月 20 17:45 mylog.tar # 归档后的文件
drwxr-xr-x 2 root root 121 2月 20 17:43 tao

# 展开归档
[root@centos7 test]# tar xf mylog.tar
[root@centos7 test]# ll
总用量 176
-rw-r--r-- 1 root root 12097 2月 20 17:42 boot.log
-rw-r--r-- 1 root root 26074 2月 20 16:50 messages.bz2
-rw-r--r-- 1 root root 41791 2月 20 16:44 messages.gz
-rw-r--r-- 1 root root 21420 2月 20 16:50 messages.xz
-rw-r--r-- 1 root root 40960 2月 20 17:51 mylog.tar
-rw-r----- 1 root root 0 2月 20 17:42 pacemaker.log
drwxr-xr-x 2 root root 121 2月 20 17:43 tao
-rw-r--r-- 1 root root 2800 2月 20 17:42 wpa_supplicant.log
-rw-r--r-- 1 root root 18303 2月 20 17:42 Xorg.0.log
-rw------- 1 root root 105 2月 20 17:42 yum.log

# -C 展开归档至指定文件中
[root@centos7 test]# mkdir /tmp/newtest
[root@centos7 test]# tar xf mylog.tar -C /tmp/newtest
[root@centos7 test]# ll /tmp/newtest
总用量 40
-rw-r--r-- 1 root root 12097 2月 20 17:42 boot.log
-rw-r----- 1 root root 0 2月 20 17:42 pacemaker.log
-rw-r--r-- 1 root root 2800 2月 20 17:42 wpa_supplicant.log
-rw-r--r-- 1 root root 18303 2月 20 17:42 Xorg.0.log
-rw------- 1 root root 105 2月 20 17:42 yum.log

# 查看归档文件中的文件列表
[root@centos7 test]# tar tf mylog.tar
boot.log
pacemaker.log
wpa_supplicant.log
Xorg.0.log
yum.log

归档并压缩演示:
# 对目录进行归档并压缩
[root@centos7 test]# tar zcf /tmp/test/tao.tar.gz tao
[root@centos7 test]# ll /tmp/test
总用量 184
-rw-r--r-- 1 root root 12097 2月 20 17:42 boot.log
-rw-r--r-- 1 root root 26074 2月 20 16:50 messages.bz2
-rw-r--r-- 1 root root 41791 2月 20 16:44 messages.gz
-rw-r--r-- 1 root root 21420 2月 20 16:50 messages.xz
-rw-r--r-- 1 root root 40960 2月 20 17:51 mylog.tar
-rw-r----- 1 root root 0 2月 20 17:42 pacemaker.log
drwxr-xr-x 2 root root 121 2月 20 17:43 tao # 原文件
-rw-r--r-- 1 root root 7232 2月 20 18:15 tao.tar.gz # 归档压缩后的文件
-rw-r--r-- 1 root root 2800 2月 20 17:42 wpa_supplicant.log
-rw-r--r-- 1 root root 18303 2月 20 17:42 Xorg.0.log
-rw------- 1 root root 105 2月 20 17:42 yum.log

# 删除原文件
[root@centos7 test]# rm -fr tao

# 展开归档,其中 z 可省略,tar命令会自动识别其为压缩文件
[root@centos7 test]# tar xf tao.tar.gz
[root@centos7 test]# ll
总用量 184
-rw-r--r-- 1 root root 12097 2月 20 17:42 boot.log
-rw-r--r-- 1 root root 26074 2月 20 16:50 messages.bz2
-rw-r--r-- 1 root root 41791 2月 20 16:44 messages.gz
-rw-r--r-- 1 root root 21420 2月 20 16:50 messages.xz
-rw-r--r-- 1 root root 40960 2月 20 17:51 mylog.tar
-rw-r----- 1 root root 0 2月 20 17:42 pacemaker.log
drwxr-xr-x 2 root root 121 2月 20 17:43 tao # 展开后的文件
-rw-r--r-- 1 root root 7232 2月 20 18:15 tao.tar.gz
-rw-r--r-- 1 root root 2800 2月 20 17:42 wpa_supplicant.log
-rw-r--r-- 1 root root 18303 2月 20 17:42 Xorg.0.log
-rw------- 1 root root 105 2月 20 17:42 yum.log

cpio 归档工具(把文件复制为归档文件)
  cpio命令是通过重定向的方式将文件进行打包备份,还原恢复的工具,它可以解压以“.cpio”或者“.tar”结尾的文件。
  用法:
  cpio[选项] > 文件名或者设备名
  cpio[选项] < 文件名或者设备名
  选项:
  -o 将文件拷贝打包成文件或者将文件输出到设备上
  -i 解包,将打包文件解压或将设备上的备份还原到系统
  -t 预览,查看文件内容或者输出到设备上的文件内容
  -v 显示打包过程中的文件名称
  -d 解包生成目录,在cpio还原时,自动的建立目录
  -c 一种较新的存储方式
示例:
  将etc目录备份:
  find . /etc -print |cpio -ov > etc.cpio
  内容预览
  cpio -tv < etc.cpio
  要解包文件
  cpio -iv < etc.cpio
  cpio -idv < etc.cpio

压缩工具   compress/uncompress:对应 .Z 结尾的压缩格式文件  压缩格式:gz、bz2、xz、zip、Z    gzip       压缩文件并删除源文件(生成.gz的文件)    gunzip    解压缩文件(gzip -d有相同的功能)    bzip2      压缩文件(压缩比例比gzip更高后缀为.bz2)    bunzip2  解压缩文件(bzip -d有相同的功能)
  压缩算法不同,压缩比也会不同  gzip  gzip /PATH/TO/SOMEFILE  压缩完成后会删除原文件    -d    解压缩    -#    指定压缩比(1-9),默认是6    -c    将压缩结果送往标准输出,可以使用重定向将其保存为压缩文件,从而保留原文件  例子:  gzip -c /PATH/TO/SOMEFILE > SOMEFILE.gz  gunzip  gunzip /PATH/TO/SOMEFILE.gz  解压完成后会删除原文件  zcat /PATH/TO/SOMEFILE.gz      不解压的情况,查看文本文件的内容  z系列命令,可以在不经解压的情况下,直接操作gzip压缩文件    zcat        直接显示压缩文件的内容    zless       直接逐行显示压缩文件的内容    zdiff       直接报告压缩文件的差异内容    zcmp      直接报告压缩文件的差异处演示:    [root@centos7 ~]# cp /var/log/messages /tmp/test/    [root@centos7 ~]# ll /tmp/test/    总用量 288    -rw-r----- 1 root root      0 2月  20 13:41 a    -rw-rw-rw- 1 root root      0 2月  20 13:41 b.danger    -r--r----- 1 root root      0 2月  20 13:41 c    -rwxrwxr-x 1 root root      0 2月  20 13:41 d    -rwxrwxrwx 1 root root      0 2月  20 13:41 e.danger    -rw-r--r-- 1 root root      0 2月  20 13:41 f    -rw-r--r-- 1 root root      0 2月  20 13:41 g    -rw------- 1 root root 292504 2月  20 16:28 messages    [root@centos7 ~]# ll -h /tmp/test/messages    -rw------- 1 root root 286K 2月  20 16:28 /tmp/test/messages
    # 压缩,删除原文件,保留压缩后以.gz结尾的文件    [root@centos7 ~]# gzip /tmp/test/messages
    [root@centos7 ~]# ll -h /tmp/test    总用量 44K    -rw-r----- 1 root root   0 2月  20 13:41 a    -rw-rw-rw- 1 root root   0 2月  20 13:41 b.danger    -r--r----- 1 root root   0 2月  20 13:41 c    -rwxrwxr-x 1 root root   0 2月  20 13:41 d    -rwxrwxrwx 1 root root   0 2月  20 13:41 e.danger    -rw-r--r-- 1 root root   0 2月  20 13:41 f    -rw-r--r-- 1 root root   0 2月  20 13:41 g    -rw------- 1 root root 41K 2月  20 16:28 messages.gz
    # 解压缩,原来的压缩文件被删除,保留解压缩后的文件    [root@centos7 ~]# gunzip /tmp/test/messages.gz    [root@centos7 ~]# ll -h /tmp/test    总用量 288K    -rw-r----- 1 root root    0 2月  20 13:41 a    -rw-rw-rw- 1 root root    0 2月  20 13:41 b.danger    -r--r----- 1 root root    0 2月  20 13:41 c    -rwxrwxr-x 1 root root    0 2月  20 13:41 d    -rwxrwxrwx 1 root root    0 2月  20 13:41 e.danger    -rw-r--r-- 1 root root    0 2月  20 13:41 f    -rw-r--r-- 1 root root    0 2月  20 13:41 g    -rw------- 1 root root 286K 2月  20 16:28 messages
    # zcat可以查看压缩的文件,不建议对大文件使用zcat命令查看    [root@centos7 ~]# zcat /tmp/test/messages.gz
    #=====================================================================================    # 解压缩    [root@centos7 ~]# gzip -d /tmp/test/messages.gz    [root@centos7 ~]# ll /tmp/test/    总用量 288    -rw-r----- 1 root root      0 2月  20 13:41 a    -rw-rw-rw- 1 root root      0 2月  20 13:41 b.danger    -r--r----- 1 root root      0 2月  20 13:41 c    -rwxrwxr-x 1 root root      0 2月  20 13:41 d    -rwxrwxrwx 1 root root      0 2月  20 13:41 e.danger    -rw-r--r-- 1 root root      0 2月  20 13:41 f    -rw-r--r-- 1 root root      0 2月  20 13:41 g    -rw------- 1 root root 292504 2月  20 16:28 messages
    # 将压缩或解压缩的结果输出至标准输出(gzip -c FILE > /PATH/TP/SOMEFILE.gz)    [root@centos7 ~]# gzip -c /tmp/test/messages > /tmp/test/messages.gz    [root@centos7 ~]# ll /tmp/test/    总用量 332    -rw-r----- 1 root root      0 2月  20 13:41 a    -rw-rw-rw- 1 root root      0 2月  20 13:41 b.danger    -r--r----- 1 root root      0 2月  20 13:41 c    -rwxrwxr-x 1 root root      0 2月  20 13:41 d    -rwxrwxrwx 1 root root      0 2月  20 13:41 e.danger    -rw-r--r-- 1 root root      0 2月  20 13:41 f    -rw-r--r-- 1 root root      0 2月  20 13:41 g    -rw------- 1 root root 292504 2月  20 16:28 messages    -rw-r--r-- 1 root root  41791 2月  20 16:44 messages.gz
    # 解压缩到标准输出    [root@centos7 ~]# rm -f /tmp/test/messages    [root@centos7 ~]# gzip -d -c /tmp/test/messages.gz > /tmp/test/messages    [root@centos7 ~]# ll /tmp/test/    总用量 332    -rw-r----- 1 root root      0 2月  20 13:41 a    -rw-rw-rw- 1 root root      0 2月  20 13:41 b.danger    -r--r----- 1 root root      0 2月  20 13:41 c    -rwxrwxr-x 1 root root      0 2月  20 13:41 d    -rwxrwxrwx 1 root root      0 2月  20 13:41 e.danger    -rw-r--r-- 1 root root      0 2月  20 13:41 f    -rw-r--r-- 1 root root      0 2月  20 13:41 g    -rw-r--r-- 1 root root 292504 2月  20 16:50 messages    -rw-r--r-- 1 root root  41791 2月  20 16:44 messages.gz
  bzip2    比gzip有着更大压缩比的压缩工具,使用格式近似    bzip2 /PATH/TO/SOMEFILE      -d    解压缩      -#    指定压缩比(1-9),默认是6      -k    压缩解压时保留原文件  bunzip2    bunzip2 /PATH/TO/SOMEFILE.bz2     解压完成后会删除原文件    bzcat /PATH/TO/SOMEFILE.bz2        不解压的情况,查看文本文件的内容演示:    # 压缩    [root@centos7 ~]# bzip2 /tmp/test/messages
    [root@centos7 ~]# ll -h /tmp/test/    总用量 72K    -rw-r----- 1 root root   0 2月  20 13:41 a    -rw-rw-rw- 1 root root   0 2月  20 13:41 b.danger    -r--r----- 1 root root   0 2月  20 13:41 c    -rwxrwxr-x 1 root root   0 2月  20 13:41 d    -rwxrwxrwx 1 root root   0 2月  20 13:41 e.danger    -rw-r--r-- 1 root root   0 2月  20 13:41 f    -rw-r--r-- 1 root root   0 2月  20 13:41 g    -rw-r--r-- 1 root root 26K 2月  20 16:50 messages.bz2 #压缩后的结果    -rw-r--r-- 1 root root 41K 2月  20 16:44 messages.gz
    # 解压缩    [root@centos7 ~]# bunzip2 /tmp/test/messages.bz2    [root@centos7 ~]# ll -h /tmp/test/    总用量 332K    -rw-r----- 1 root root    0 2月  20 13:41 a    -rw-rw-rw- 1 root root    0 2月  20 13:41 b.danger    -r--r----- 1 root root    0 2月  20 13:41 c    -rwxrwxr-x 1 root root    0 2月  20 13:41 d    -rwxrwxrwx 1 root root    0 2月  20 13:41 e.danger    -rw-r--r-- 1 root root    0 2月  20 13:41 f    -rw-r--r-- 1 root root    0 2月  20 13:41 g    -rw-r--r-- 1 root root 286K 2月  20 16:50 messages  # 解压缩后的结果    -rw-r--r-- 1 root root  41K 2月  20 16:44 messages.gz
    # -k 选项不用指明重定向的文件,自动保留源文件在当前文件中    [root@centos7 ~]# bzip2 -k /tmp/test/messages    [root@centos7 ~]# ll -h /tmp/test/    总用量 360K    -rw-r----- 1 root root    0 2月  20 13:41 a    -rw-rw-rw- 1 root root    0 2月  20 13:41 b.danger    -r--r----- 1 root root    0 2月  20 13:41 c    -rwxrwxr-x 1 root root    0 2月  20 13:41 d    -rwxrwxrwx 1 root root    0 2月  20 13:41 e.danger    -rw-r--r-- 1 root root    0 2月  20 13:41 f    -rw-r--r-- 1 root root    0 2月  20 13:41 g    -rw-r--r-- 1 root root 286K 2月  20 16:50 messages    -rw-r--r-- 1 root root  26K 2月  20 16:50 messages.bz2    -rw-r--r-- 1 root root  41K 2月  20 16:44 messages.gz
  xz    xz /PATH/TO/SOMEFILE      -d    解压缩      -#    指定压缩比(1-9),默认是6      -k    压缩解压时保留原文件  unxz    unxz /PATH/TO/SOMEFILE.xz        解压完成后会删除原文件    xzdec /PATH/TO/SOMEFILE.xz       解压文件显示到终端上    xzcat /PATH/TO/SOMEFILE.xz        不解压的情况,查看文本文件的内容以上压缩工具只能压缩文件,不能压缩目录,如果指定目录中的所有文件/PATH/TO/*会将目录中的每个文件压缩成一个压缩文件,所以在压缩目录的时候还需要归档工具演示:    [root@centos7 ~]# xz /tmp/test/messages    [root@centos7 ~]# ll -h /tmp/test/    总用量 96K    -rw-r----- 1 root root   0 2月  20 13:41 a    -rw-rw-rw- 1 root root   0 2月  20 13:41 b.danger    -r--r----- 1 root root   0 2月  20 13:41 c    -rwxrwxr-x 1 root root   0 2月  20 13:41 d    -rwxrwxrwx 1 root root   0 2月  20 13:41 e.danger    -rw-r--r-- 1 root root   0 2月  20 13:41 f    -rw-r--r-- 1 root root   0 2月  20 13:41 g    -rw-r--r-- 1 root root 26K 2月  20 16:50 messages.bz2    -rw-r--r-- 1 root root 41K 2月  20 16:44 messages.gz    -rw-r--r-- 1 root root 21K 2月  20 16:50 messages.xz
  zip  既归档又压缩的工具(window下面的.zip的文件可以直接在linux下解压)      zip FILENAME.zip FILE1 FILE2 ...        压缩多个文件到一个目录中,压缩后不删除原文件    unzip FILENAME.zip    如果要压缩默认目录,要通过指定目录的所有文件/PATH/TO/*才能压缩整个目录下的文件,如果指定的是/PATH/TO/就只会压缩TO这个目录本身并不包括目录中的文件    zip /PATH/TO/    /PATH/TO/SOMEFILE.zip  只是压缩了TO目录    zip /PATH/TO/*  /PATH/TO/SOMEFILE.zip  压缩了TO目录中的所有文件演示:    # 对目录进行归档并压缩    [root@centos7 test]# zip /tmp/test/tao.zip tao      adding: tao/ (stored 0%)    [root@centos7 test]# ll    总用量 188    -rw-r--r-- 1 root root 12097 2月  20 17:42 boot.log    -rw-r--r-- 1 root root 26074 2月  20 16:50 messages.bz2    -rw-r--r-- 1 root root 41791 2月  20 16:44 messages.gz    -rw-r--r-- 1 root root 21420 2月  20 16:50 messages.xz    -rw-r--r-- 1 root root 40960 2月  20 17:51 mylog.tar    -rw-r----- 1 root root     0 2月  20 17:42 pacemaker.log    drwxr-xr-x 2 root root   121 2月  20 17:43 tao    -rw-r--r-- 1 root root  7232 2月  20 18:15 tao.tar.gz    -rw-r--r-- 1 root root   158 2月  20 18:26 tao.zip    -rw-r--r-- 1 root root  2800 2月  20 17:42 wpa_supplicant.log    -rw-r--r-- 1 root root 18303 2月  20 17:42 Xorg.0.log    -rw------- 1 root root   105 2月  20 17:42 yum.log
    [root@centos7 test]# rm -fr tao
    # 解压缩    [root@centos7 test]# unzip tao.zip    Archive:  tao.zip       creating: tao/    [root@centos7 test]# ll    总用量 188    -rw-r--r-- 1 root root 12097 2月  20 17:42 boot.log    -rw-r--r-- 1 root root 26074 2月  20 16:50 messages.bz2    -rw-r--r-- 1 root root 41791 2月  20 16:44 messages.gz    -rw-r--r-- 1 root root 21420 2月  20 16:50 messages.xz    -rw-r--r-- 1 root root 40960 2月  20 17:51 mylog.tar    -rw-r----- 1 root root     0 2月  20 17:42 pacemaker.log    drwxr-xr-x 2 root root     6 2月  20 17:43 tao    -rw-r--r-- 1 root root  7232 2月  20 18:15 tao.tar.gz    -rw-r--r-- 1 root root   158 2月  20 18:26 tao.zip    -rw-r--r-- 1 root root  2800 2月  20 17:42 wpa_supplicant.log    -rw-r--r-- 1 root root 18303 2月  20 17:42 Xorg.0.log    -rw------- 1 root root   105 2月  20 17:42 yum.log

  tar  归档压缩工具( archive归档本身并不意味着压缩)   主要参数:    -f        指定创建或展开归档文件名(只要操作归档文件就要用-f)    -c        创建归档文件    -v        显示创建的详细过程    -x        展开归档文件    -r        将文件添加到已经存在的归档文件中(压缩后是不能用r的,只有归档的时候用)    -z        用gzip来压缩和解压缩文件    -j         用bz2来压缩和解压缩文件    -J         用xz来压缩和解压缩文件    -t         不展开归档文件情况下查看文件中的内容    -C        展开或解压文件到指定目录(如果没指定该参数,默认是当前目录)    --xattrs  归档时,保留文件的扩展属性信息
    -zcf        归档并调用gzip压缩    -zxf        调用gzip解压缩并展开归档,-z选项可省略(解压时自动判断用什么工具压缩)
    -jcf        归档并调用bzip2压缩    -jxf        调用bzip2解压缩并展开归档,-j选项可省略
    -Jcf        归档并调用xz压缩    -Jxf        调用xz解压缩并展开归档,-J选项可省略
  例如:  tar -cvf home.tar /home            将home目录打包成home.tar(只是打包并没有压缩)  tar -rvf home.tar /etc                将etc目录添加到已存在的打包文件home.tar里面  tar -czvf home.tar.gz /home      将home目录打包并压缩成home.tar.gz  tar -cvjf home.tar.bz2 /home     将home目录打包并压缩成.bz2的格式  tar -xzvf home.tar.gz                 将home.tar.gz这个备份文件还原并解压缩  tar -tvf home.tar | more            查看home.tar备份文件的类容,并以分屏方式显示在显示器上  tar -cvf /dev/st0 /home/shrek  可以将st0磁带机备份到/home目录下面
注意:f选项一定要写在最后一个;如果是解开压缩归档文件可以不指定压缩选项zjJ,tar会通过文件后缀判断是什么压缩工具压缩的归档演示:    [root@centos7 ~]# ls /tmp/test/tao    boot.log  fstab  issue  pacemaker.log  wpa_supplicant.log  Xorg.0.log  yum.log
    # 对所有以 .log 结尾的文件进行归档    [root@centos7 ~]# tar -cf /tmp/test/mylog.tar /tmp/test/tao/*.log    [root@centos7 ~]# ll /tmp/test/    总用量 136    -rw-r--r-- 1 root root 26074 2月  20 16:50 messages.bz2    -rw-r--r-- 1 root root 41791 2月  20 16:44 messages.gz    -rw-r--r-- 1 root root 21420 2月  20 16:50 messages.xz    -rw-r--r-- 1 root root 40960 2月  20 17:45 mylog.tar   # 归档后的文件    drwxr-xr-x 2 root root   121 2月  20 17:43 tao   
    # 展开归档    [root@centos7 test]# tar xf mylog.tar    [root@centos7 test]# ll    总用量 176    -rw-r--r-- 1 root root 12097 2月  20 17:42 boot.log    -rw-r--r-- 1 root root 26074 2月  20 16:50 messages.bz2    -rw-r--r-- 1 root root 41791 2月  20 16:44 messages.gz    -rw-r--r-- 1 root root 21420 2月  20 16:50 messages.xz    -rw-r--r-- 1 root root 40960 2月  20 17:51 mylog.tar    -rw-r----- 1 root root     0 2月  20 17:42 pacemaker.log    drwxr-xr-x 2 root root   121 2月  20 17:43 tao    -rw-r--r-- 1 root root  2800 2月  20 17:42 wpa_supplicant.log    -rw-r--r-- 1 root root 18303 2月  20 17:42 Xorg.0.log    -rw------- 1 root root   105 2月  20 17:42 yum.log
    # -C 展开归档至指定文件中    [root@centos7 test]# mkdir /tmp/newtest    [root@centos7 test]# tar xf mylog.tar -C /tmp/newtest    [root@centos7 test]# ll /tmp/newtest    总用量 40    -rw-r--r-- 1 root root 12097 2月  20 17:42 boot.log    -rw-r----- 1 root root     0 2月  20 17:42 pacemaker.log    -rw-r--r-- 1 root root  2800 2月  20 17:42 wpa_supplicant.log    -rw-r--r-- 1 root root 18303 2月  20 17:42 Xorg.0.log    -rw------- 1 root root   105 2月  20 17:42 yum.log
    # 查看归档文件中的文件列表    [root@centos7 test]# tar tf mylog.tar    boot.log    pacemaker.log    wpa_supplicant.log    Xorg.0.log    yum.log
归档并压缩演示:    # 对目录进行归档并压缩    [root@centos7 test]# tar zcf /tmp/test/tao.tar.gz tao    [root@centos7 test]# ll /tmp/test    总用量 184    -rw-r--r-- 1 root root 12097 2月  20 17:42 boot.log    -rw-r--r-- 1 root root 26074 2月  20 16:50 messages.bz2    -rw-r--r-- 1 root root 41791 2月  20 16:44 messages.gz    -rw-r--r-- 1 root root 21420 2月  20 16:50 messages.xz    -rw-r--r-- 1 root root 40960 2月  20 17:51 mylog.tar    -rw-r----- 1 root root     0 2月  20 17:42 pacemaker.log    drwxr-xr-x 2 root root   121 2月  20 17:43 tao           # 原文件    -rw-r--r-- 1 root root  7232 2月  20 18:15 tao.tar.gz    # 归档压缩后的文件    -rw-r--r-- 1 root root  2800 2月  20 17:42 wpa_supplicant.log    -rw-r--r-- 1 root root 18303 2月  20 17:42 Xorg.0.log    -rw------- 1 root root   105 2月  20 17:42 yum.log
    # 删除原文件    [root@centos7 test]# rm -fr tao        
    # 展开归档,其中 z 可省略,tar命令会自动识别其为压缩文件    [root@centos7 test]# tar xf tao.tar.gz    [root@centos7 test]# ll    总用量 184    -rw-r--r-- 1 root root 12097 2月  20 17:42 boot.log    -rw-r--r-- 1 root root 26074 2月  20 16:50 messages.bz2    -rw-r--r-- 1 root root 41791 2月  20 16:44 messages.gz    -rw-r--r-- 1 root root 21420 2月  20 16:50 messages.xz    -rw-r--r-- 1 root root 40960 2月  20 17:51 mylog.tar    -rw-r----- 1 root root     0 2月  20 17:42 pacemaker.log    drwxr-xr-x 2 root root   121 2月  20 17:43 tao           # 展开后的文件    -rw-r--r-- 1 root root  7232 2月  20 18:15 tao.tar.gz    -rw-r--r-- 1 root root  2800 2月  20 17:42 wpa_supplicant.log    -rw-r--r-- 1 root root 18303 2月  20 17:42 Xorg.0.log    -rw------- 1 root root   105 2月  20 17:42 yum.log
cpio  归档工具(把文件复制为归档文件)cpio命令是通过重定向的方式将文件进行打包备份,还原恢复的工具,它可以解压以“.cpio”或者“.tar”结尾的文件。    用法:        cpio[选项] > 文件名或者设备名        cpio[选项] < 文件名或者设备名    选项:        -o:将文件拷贝打包成文件或者将文件输出到设备上        -i:解包,将打包文件解压或将设备上的备份还原到系统        -t:预览,查看文件内容或者输出到设备上的文件内容        -v:显示打包过程中的文件名称        -d:解包生成目录,在cpio还原时,自动的建立目录        -c:一种较新的存储方式    示例:        将etc目录备份:            find . /etc -print |cpio -ov > etc.cpio        内容预览            cpio -tv < etc.cpio        要解包文件            cpio -iv < etc.cpio            cpio -idv < etc.cpio

Linux 文件压缩的更多相关文章

  1. Linux 文件压缩与归档

    .note-content { font-family: "Helvetica Neue", Arial, "Hiragino Sans GB", STHeit ...

  2. Linux文件压缩与打包笔记

    linux 文件压缩与打包笔记 压缩原理:通过算法去掉空位,1Bytes=8bits , 可能存储的真正有用的数据并没有占满一个字节空间 , 还有就是可能有重复的数据,通过某种算法从这些方面进行压缩处 ...

  3. Linux文件压缩和解压缩命令

    Linux文件压缩和解压缩命令: tar 命令(打包并压缩的话,原文件也会默认存在) -c 建立打包档案 -x 解包 -t 查看包里的类容 -r 向包里追加文件 -v 显示打包过程 -f 文件 比如: ...

  4. Linux文件压缩、解压缩及归档工具一

    主题Linux文件压缩.解压缩及归档工具 压缩工具很重要的,因为要经常到互联网下载包 一compress/uncompress compress [-dfvcVr] [-b maxbits] [fil ...

  5. linux文件压缩与文件夹压缩(打包)

    目录 一:linux文件压缩 1.linux常见的压缩包有哪些? 2.bzip压缩(文件) 二:打包(文件夹压缩) 1.打包命令 2.参数 3.参数解析(实战) 4.注意事项 简介: win中的压缩包 ...

  6. linux文件压缩与打包

    在linux中常见的压缩命令 首先,在linux中压缩文件的扩展名大多是 *.gz gzip程序压缩的文件 *.bz2 bzip2程序压缩的文件 *.tar tar程序打包的数据,并没有压缩过 *.t ...

  7. Linux使用——Linux命令——Linux文件压缩和解压使用记录

    一:tar(可压缩可解压) tar命令是Unix/Linux系统中备份文件的可靠方法,几乎可以工作于任何环境中,它的使用权限是所有用户.但是tar本身只是一个文件打包工具,只有和其他工具组合时才具有压 ...

  8. Linux 文件压缩、打包

    文件压缩 计算机使用byte单位来计量.实际上,计算机最小的计量单位是bit.1byte = 8 bit.如果记录1这个数字,00000001,1会在最右边占一个1个bit 其他7个bit会被填上0. ...

  9. Linux文件压缩与解压命令

    1  .zip 格式压缩与解压 压缩命令 zip 压缩文件名 源文件 zip  -r   压缩目录名       源目录 解压命令 unzip 文件名 td@td-Lenovo-IdeaPad-Y41 ...

随机推荐

  1. git分支合并概念

    git merge命令用于合并指定分支到当前分支. git merge命令用于合并指定分支到当前分支. git merge命令用于合并指定分支到当前分支. 创建与合并分支 阅读: 931277 在版本 ...

  2. Justinmind使用教程(1)——概述部分

    Justinmind(http://www.justinmind.com/),类似于Axure的一个原型设计工具.就眼下而言,最适合移动端进行原型设计的工具,预计抛开Axure几条街了,可是眼下国内站 ...

  3. 怎样从C++代码直接訪问android framework层的WifiService

    说究竟,Java层的service就是就C++层的binder的封装.所以从原理上来讲通过C++代码直接訪问android framework层的service是全然可能的,这篇文章以訪问WifiSe ...

  4. HDOJ1796 How many integers can you find(dfs+容斥)

    How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  5. iWatch报错: Authorization request cancled

    iWatch报错: Optional (Error Domin = com.apple.healthkit Code = 5 "Autherization request canceled& ...

  6. wpf Textbox 点击选中全部文本

    用法:依赖属性 SelectTextOnFocus.Active = True public class SelectTextOnFocus : DependencyObject { public s ...

  7. POJ3190 Stall Reservations 贪心

    这是个典型的线程服务区间模型.一些程序要在一段时间区间上使用一段线程运行,问至少要使用多少线程来为这些程序服务? 把所有程序以左端点为第一关键字,右端点为第二关键字从小到大排序.从左向右扫描.处理当前 ...

  8. Build website project by roslyn through devenv.com

    1.fetch the source code2.compile controls project3.copy files under bin folder of controls to bin fo ...

  9. Windows显示我的电脑到桌面以及给一些程序设置快捷键

    Windows显示我的电脑到桌面,我测试的是windows server 2012和windows10  1.按Win(键盘上的微软徽标键)+R,输入: rundll32.exe shell32.dl ...

  10. css中单位的使用

    css中许多的属性都需要添加长度,而长度一般由数字和单位构成,如1px,1.5em,2vh:也可以省略单位,如line-height:1.5,表示行高为字体大小的1.5倍: 长度单位一般也分为相对长度 ...