linux下文件的压缩与解压,打包并压缩与解包解压

Linux下压缩包常见类型 gz ,bz2, xz ,tar.gz ,tar.bz2 ,tar.xz

gzip压缩工具

压缩
gzip 1.txt 压缩 替换原文件
gzip -* 1.txt 指定压缩级别 *范围1-9,默认6
gzip -c 1.txt > /tmp/1.txt.gz 压缩并指定存放目录

文档查看
zcat 1.txt.gz 查看文件内容,-l 选项 查看压缩详情
wc -l 1.txt 查看文件行数
du -sh *.txt 查看文件大小
不能压缩目录

解压
gzip -d 1.txt.gz / gunzip 1.txt.gz 解压 替换压缩文件
gzip -d -c /tmp/1.txt.gz > /tmp/d6z/2.txt 解压并指定存放目录,且更名
gunzip -c /root/1.txt.gz > /tmp/1.txt.new 解压并指定存放目录,且更名


例:
[root@bogon ~]# cat /etc/*.conf >> 1.txt 追加cat输出的内容进1.txt文件里
[root@bogon ~]# ls
1.txt anaconda-ks.cfg initial-setup-ks.cfg
[root@bogon ~]# du -sh 1.txt 查看1.txt文件大小
6.2M 1.txt
[root@bogon ~]# gzip 1.txt 压缩1.txt文件
[root@bogon ~]# ls
1.txt.gz anaconda-ks.cfg initial-setup-ks.cfg 原文件被替换成压缩后的文件
[root@bogon ~]# du -sh 1.txt.gz 查看压缩文件的大小
788K 1.txt.gz
[root@bogon ~]# wc -l 1.txt.gz 查看压缩文件的内容行数
2864 1.txt.gz
[root@bogon ~]# zcat -l 1.txt.gz
查看压缩文件的压缩详情,不加 l 选项,则为查看压缩文件内容
compressed uncompressed ratio uncompressed_name
805698 2453660 67.2% 1.txt 压缩比
[root@bogon ~]# gzip -d 1.txt.gz 解压
[root@bogon ~]# ls
1.txt anaconda-ks.cfg initial-setup-ks.cfg 解压后的文件替换原来的压缩文件
[root@bogon ~]# gzip -c -3 1.txt > /tmp/1.txt.gz 指定压缩级别为3,并指定压缩文件的存放路径
[root@bogon ~]# ls /tmp
1.txt BB systemd-private-7d54e21553c24121be47d6e6cbb9c91c-chronyd.service-dOePa6
1.txt.gz CC systemd-private-7d54e21553c24121be47d6e6cbb9c91c-vgauthd.service-H8Ndhc
AA DD systemd-private-7d54e21553c24121be47d6e6cbb9c91c-vmtoolsd.service-a5aZLi
[root@bogon ~]# du -sh /tmp/1.txt.gz 查看压缩级别为3的文件大小
860K /tmp/1.txt.gz 用压缩级别3压缩过的文件比默认6级别压缩后的文件要大
[root@bogon ~]# gunzip -c /tmp/1.txt.gz > /root/2.txt.new
指定解压路径,且将解压后的文件重命名为2.txt.new, gunzip与gzip -d同为解压命令
[root@bogon ~]# ls
1.txt 2.txt.new anaconda-ks.cfg initial-setup-ks.cfg


bzip2压缩工具

压缩
bzip2 1.txt 压缩
bzip2 -z 1.txt 忽略调用名称 强制压缩
bzip -* 1.txt *范围1-9,默认9
bzip2 -c 1.txt > /tmp/1.txt.bz2 压缩并指定存放目录

查看
bzcat 1.txt.bz2
file 1.txt.bz2 查看文件属性
不能压缩目录

解压
bzip2 -d 1.txt.bz2 解压
bunzip2 1.txt.bz2 解压
bzip2 -d -c /root/1.txt.bz2 > /tmp/1.txt.new 解压并指定目录,且更名

例:
[root@bogon ~]# ls
1.txt 2.txt.new anaconda-ks.cfg initial-setup-ks.cfg
[root@bogon ~]# bzip2 -c -5 1.txt > /home/1.txt.bz2 指定压缩比为5,且指定存放路径
[root@bogon ~]# ls /home
1.txt.bz2 AA BB CC DD ELON
[root@bogon ~]# du -sh /home/1.txt.bz2 查看压缩文件大小
272K /home/1.txt.bz2
[root@bogon ~]# bzip2 -d -z 1.txt 忽略企其余调用名称,强制压缩
[root@bogon ~]# ls
1.txt.bz2 2.txt.new anaconda-ks.cfg initial-setup-ks.cfg
[root@bogon ~]# du -sh 1.txt.bz2 查看压缩文档大小,此处为默认9级压缩,比5级压缩比更高
176K 1.txt.bz2
[root@bogon ~]# file 1.txt.bz2 查看文件类型,可用于查看没有后缀名的文档
1.txt.bz2: bzip2 compressed data, block size = 900k
[root@bogon ~]# bunzip2 1.txt.bz2 解压 bunzip2命令等同 bzip2 -d命令
[root@bogon ~]# ls
1.txt 2.txt.new anaconda-ks.cfg initial-setup-ks.cfg
[root@bogon ~]# bzip2 -d -c /home/1.txt.bz2 > /root/1.txt.new 解压并指定解压路径,且改名文件
[root@bogon ~]# ls
1.txt 1.txt.new 2.txt.new anaconda-ks.cfg initial-setup-ks.cfg
[root@bogon ~]#


xz压缩工具

压缩
xz 1.txt 压缩
xz -z 1.txt 强制压缩,忽略其余调用名称
xz -c 1.txt > /root/1.txt.xz 指定压缩路径
xz -* 1.txt *范围1-9,默认6

查看文档
file filename 查看文件类型
xzcat 1.txt.xz 查看xz压缩文件下的内容
不能压缩目录

解压
xz -d 1.txt.xz / unxz 1.txt.xz
xz -d -c /root/1.txt.xz > 1.txt.new3

例:
[root@bogon ~]# ls
1.txt 1.txt.new 2.txt.new anaconda-ks.cfg initial-setup-ks.cfg
[root@bogon ~]# xz -9 -c 1.txt > /home/1.txt.xz 指定9级压缩,且指定存放路径
[root@bogon ~]# ls /home
1.txt.xz AA BB CC DD ELON
[root@bogon ~]# xz 1.txt 默认原目录压缩,替换原文件
[root@bogon ~]# ls
1.txt.new 1.txt.xz 2.txt.new anaconda-ks.cfg initial-setup-ks.cfg
[root@bogon ~]# unxz 1.txt.xz 解压,替换原压缩文件
[root@bogon ~]# xz -d -c /home/1.txt.xz > 3.txt.new 解压,并指定存放路径,且改名文件
[root@bogon ~]# ls
1.txt 1.txt.new 2.txt.new 3.txt.new anaconda-ks.cfg initial-setup-ks.cfg
[root@bogon ~]#


zip压缩工具

支持目录压缩解压目录,解压rar文件,须要安装工具,不可查看压缩内容,只能看文件列表

压缩
zip *.txt.zip *.txt 压缩文件,且保留原文件
zip -r *.zip /***/ 压缩目录,且保留 原目录

查看
unzip -l *.zip 查看文件列表,不可查看压缩内容

解压
unzip *.*.zip 解压缩,且询问是否替换覆盖原文件或目录
unzip *.zip -d /*/*/ 解压并指定解压路径,不可直接更名


例:

[root@localhost ~]# ls
1.txt 1.txt.new 2.txt.new 3.txt.new anaconda-ks.cfg initial-setup-ks.cfg
[root@localhost ~]# zip 1.txt.zip 1.txt 压缩文件1.txt
adding: 1.txt (deflated 67% )
[root@localhost ~]# zip -r AA.zip /home/AA 压缩目录/home/AA
adding: home/AA/ (stored 0%)
adding: home/AA/.bash_logout (stored 0%)
adding: home/AA/.bash_profile (deflated 21%)
adding: home/AA/.bashrc (deflated 23%)
adding: home/AA/.mozilla/ (stored 0%)
adding: home/AA/.mozilla/extensions/ (stored 0%)
adding: home/AA/.mozilla/plugins/ (stored 0%)
adding: home/AA/.bash_history (deflated 61%)
adding: home/AA/rar (stored 0%)
[root@localhost ~]# ls
1.txt 1.txt.new 1.txt.zip 2.txt.new 3.txt.new AA.zip anaconda-ks.cfg initial-setup-ks.cfg
原文件1.txt和压缩文件1.txt.zip都在,目录压缩文件AA.zip也存在
[root@localhost ~]# unzip -l 1.txt.zip 查看压缩文件列表,不能查看内容
Archive: 1.txt.zip
Length Date Time Name
--------- ---------- ----- ----
2453660 04-16-2018 22:35 1.txt
--------- -------
2453660 1 file
[root@localhost ~]# unzip 1.txt.zip 解压1.txt.zip文件
Archive: 1.txt.zip
replace 1.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y 会提示是否替换原文件1.txt
inflating: 1.txt
[root@localhost ~]# unzip AA.zip -d /root/456
解压目录压缩文件,并指定存放位置,不能更名,若无目录则会建立。
Archive: AA.zip
creating: /root/456/home/AA/
extracting: /root/456/home/AA/.bash_logout
inflating: /root/456/home/AA/.bash_profile
inflating: /root/456/home/AA/.bashrc
creating: /root/456/home/AA/.mozilla/
creating: /root/456/home/AA/.mozilla/extensions/
creating: /root/456/home/AA/.mozilla/plugins/
inflating: /root/456/home/AA/.bash_history
extracting: /root/456/home/AA/rar
[root@localhost ~]# ls
1.txt 1.txt.new 1.txt.zip 2.txt.new 3.txt.new 456 AA.zip anaconda-ks.cfg initial-setup-ks.cfg
压缩文件,和原文件都存在,且1.txt被替换过
[root@localhost ~]# ls 456/ 查看456/目录原压缩目录被放在下层
home
[root@localhost ~]# unzip AA.zip -d /root 解压目录压缩文件到当前目录
Archive: AA.zip
creating: /root/home/AA/
extracting: /root/home/AA/.bash_logout
inflating: /root/home/AA/.bash_profile
inflating: /root/home/AA/.bashrc
creating: /root/home/AA/.mozilla/
creating: /root/home/AA/.mozilla/extensions/
creating: /root/home/AA/.mozilla/plugins/
inflating: /root/home/AA/.bash_history
extracting: /root/home/AA/rar
[root@localhost ~]# ls
1.txt 1.txt.new 1.txt.zip 2.txt.new 3.txt.new 456 AA.zip anaconda-ks.cfg home initial-setup-ks.cfg
[root@localhost ~]#




tar打包工具

打包
tar -cvf **.tar ***/ 打包文件,原文件还在,c 建立 v可视化 f文件 v能够省略,重复打包覆盖不提示
tar -cvf ***.tar *.txt ***/
tar -cvf ***.tar --exclude *.txt --exclude *.txt ***/ 打包*.txt 文件和***/目录, 并过滤全部“*.txt”文件

查看
tar -tf ***.tar 查看文件列表

解包
tar -xvf ***.tar 解包到当前目录,不提示直接替换

例:
[root@localhost ~]# tar -cvf 123.tar 1.txt 1.txt.new 打包1.txt和1.txt.new文件
1.txt
1.txt.new
[root@localhost ~]# tar -cvf 321.tar 1.txt 456/ 打包1.txt文件和456目录
1.txt
456/
456/1.txt
456/2.txt
456/3.txt
[root@localhost ~]# tar -cvf 111.tar --exclude 1.txt --exclude 2.txt 456/
打包456目录且忽略全部1.txt和2.txt文件
456/
456/3.txt
[root@localhost ~]# tar -cvf 222.tar --exclude 1.txt --exclude 2.txt 1.txt 2.txt 456/
打包1.txt文件,2.txt文件 和 456目录且忽略全部1.txt和2.txt文件
456/
456/3.txt
[root@localhost ~]# tar -tf 123.tar 查看打包文件
1.txt
1.txt.new
[root@localhost ~]# tar -tf 321.tar 查看打包文件
1.txt
456/
456/1.txt
456/2.txt
456/3.txt
[root@localhost ~]# tar -tf 111.tar 查看打包文件
456/
456/3.txt
[root@localhost ~]# tar -tf 222.tar 查看打包文件
456/
456/3.txt
[root@localhost ~]# tar -xvf 123.tar 查看打包文件
1.txt
1.txt.new
[root@localhost ~]# tar -xvf 321.tar 查看打包文件
1.txt
456/
456/1.txt
456/2.txt
456/3.txt
[root@localhost ~]# tar -xvf 111.tar 解包文件,直接替换不提醒
456/
456/3.txt
[root@localhost ~]# tar -xvf 222.tar 解包文件,直接替换不提醒
456/
456/3.txt
[root@localhost ~]# ls
111.tar 1.txt 1.txt.zip 2.txt 321.tar 456 home
123.tar 1.txt.new 222.tar 2.txt.new 3.txt.new anaconda-ks.cfg initial-setup-ks.cfg
[root@localhost ~]#


tar打包并压缩

打包并压缩,解压解包
tar -zcvf ***.tar.gz *** ***/ 打包文件或目录并压缩成gz格式
tar -zxvf ***.tar.gz 解压解包
tar -jcvf ***.tar.bz2 *** ***/ 打包并压缩成bz2
tar -jxvf ***tar.bz2 解压解包
tar -Jcvf ***.tar.xz *** ***/ 打包并压缩成xz
tar -Jxvf ***.tar.xz 解压解包
tar -tf ***.bz2 查看文件列表
tar -tf ***.gz
tar -tf ***.xz

例:
[root@localhost ~]# ls
1.txt 1.txt.new 2.txt 2.txt.new 3.txt.new 456 anaconda-ks.cfg home initial-setup-ks.cfg
[root@localhost ~]# tar -zcvf 123.tar.gz 1.txt 456/ 打包文件和目录并用gz格式压缩
1.txt
456/
456/1.txt
456/2.txt
456/3.txt
[root@localhost ~]# tar -jcvf 321.tar.bz2 2.txt 456/ 打包文件和目录并用bz2格式压缩
2.txt
456/
456/1.txt
456/2.txt
456/3.txt
[root@localhost ~]# tar -Jcvf 345.tar.xz 3.txt.new 456/ 打包文件和目录并用xz格式压缩
3.txt.new
456/
456/1.txt
456/2.txt
456/3.txt
[root@localhost ~]# tar -tf 123.tar.gz 查看tar.gz文件
1.txt
456/
456/1.txt
456/2.txt
456/3.txt
[root@localhost ~]# tar -tf 321.tar.bz2 查看tar.bz2文件
2.txt
456/
456/1.txt
456/2.txt
456/3.txt
[root@localhost ~]# tar -tf 345.tar.xz 查看tar.xz文件
3.txt.new
456/
456/1.txt
456/2.txt
456/3.txt
[root@localhost ~]# ls 原文件和打包压缩过的文件都在
123.tar.gz 1.txt.new 2.txt.new 345.tar.xz 456 home
1.txt 2.txt 321.tar.bz2 3.txt.new anaconda-ks.cfg initial-setup-ks.cfg
[root@localhost ~]# tar -zxvf 123.tar.gz 解压tar.gz文件
1.txt
456/
456/1.txt
456/2.txt
456/3.txt
[root@localhost ~]# tar -jxvf 321.tar.bz2 解压tar.bz2文件
2.txt
456/
456/1.txt
456/2.txt
456/3.txt
[root@localhost ~]# tar -Jxvf 345.tar.xz 解压tar.xz文件
3.txt.new
456/
456/1.txt
456/2.txt
456/3.txt
[root@localhost ~]# ls 原文件被替换过,打包压缩过的文件还在
123.tar.gz 1.txt.new 2.txt.new 345.tar.xz 456 home
1.txt 2.txt 321.tar.bz2 3.txt.new anaconda-ks.cfg initial-setup-ks.cfg
[root@localhost ~]#