Linux常规压缩解压与tar打包

压缩与解压缩

  • Linux独有压缩格式及命令工具:
    • gzip---> .gz
    • bzip2---> .bz2
    • xz---> .xz
  • 压缩命令格式
    • gzip [选项...] 文件名
      • 常用选项:-d 解压缩
    • bzip2 [选项...] 文件名
      • 常用选项:-d 解压缩
    • xz [选项...] 文件名
      • 常用选项:-d 解压缩
  • 查看压缩文件内容
    • zcat [选项...] 文件名 #查看gzip格式压缩文件
    • bzcat [选项...] 文件名
    • xzcat [选项...] 文件名
[root@localhost ~]# cp /etc/services /opt
[root@localhost ~]# cd /opt
[root@localhost opt]# ll services 
-rw-r--r--. 1 root root 670293 4月  17 17:06 services
[root@localhost opt]# ll -h services 
-rw-r--r--. 1 root root 655K 4月  17 17:06 services

#使用gzip格式对文件进行压缩
[root@localhost opt]# gzip services 
[root@localhost opt]# ls
services.gz
[root@localhost opt]# ll -h services.gz 
-rw-r--r--. 1 root root 133K 4月  17 17:06 services.gz

#不解压查看压缩文件内容
[root@localhost opt]# zcat services.gz 

#解压文件
[root@localhost opt]# gzip -d services.gz 

#使用bzip2格式对文件进行压缩
[root@localhost opt]# bzip2 services 
[root@localhost opt]# ls
services.bz2
[root@localhost opt]# ll -h services.bz2 
-rw-r--r--. 1 root root 122K 4月  17 17:06 services.bz2

#不解压查看文件内容
[root@localhost opt]# bzcat services.bz2 

#解压文件
[root@localhost opt]# bzip2 -d services.bz2 

#使用xz格式对文件进行压缩
[root@localhost opt]# xz services 
[root@localhost opt]# ls
services.xz
[root@localhost opt]# ll -h services.xz 
-rw-r--r--. 1 root root 98K 4月  17 17:06 services.xz

#解压文件
[root@localhost opt]# xz -d services.xz 

tar打包工具

  • tar命令用在linux下用于对文件/目录打包,使用 tar 程序打出来的包常称为 tar 包,tar 包文件通常都是以 .tar 结尾

  • tar 命令格式:tar 选项 打包后名字 被打包文件

  • 常用选项:

    • -c 创建打包文件
    • -f 指定打包后的文件名称
    • -z 调用gzip压缩工具 -J 调用xz压缩工具 -j 调用bzip2压缩工具
    • -t 列出打包文档内容
    • -x 释放打包文件
    • -C 指定解压路径
    • -v 显示详细信息
  • tar命令范例

#同时打包多个文件/目录并使用gzip格式压缩
[root@localhost opt]# tar -czf xxx.tar.gz /etc/passwd /etc/fstab /home

#将压缩包数据解压到/media目录
[root@localhost opt]# tar -xf xxx.tar.gz -C /media/
[root@localhost opt]# ls /media/etc
[root@localhost opt]# rm -rf xxx.tar.gz 

#同时打包多个文件/目录并使用xz格式压缩
[root@localhost opt]# tar -cJf xx.tar.xz /etc/hostname /etc/services /home

#错误语法,f选项要放到所有选项右边
[root@localhost opt]# tar -ft xx.tar.xz 
tar: 您必须从"-Acdtrux"或是"--test-label"选项中指定一个
请用“tar --help”或“tar --usage”获得更多信息。

#不解压查看压缩包数据
[root@localhost opt]# tar -tf xx.tar.xz 
etc/hostname

#将压缩包数据解压到/tmp目录
[root@localhost opt]# tar -vxf xx.tar.xz -C /tmp
[root@localhost opt]# ls /tmp

#同时打包多个文件/目录并使用bzip2格式压缩
[root@localhost opt]# tar -cjf abc.tar.bz2 /etc/hostname /etc/group /home

#解压缩
[root@localhost opt]# tar -xf abc.tar.bz2 -C /media/

posted on 2021-11-20 03:27  极客飞扬  阅读(548)  评论(0编辑  收藏  举报

导航