linux文件压缩和打包

文件压缩

gzip 命令:

介绍:压缩文件,不可压缩目录,压缩后格式为filename.gz 原文件消失

格式:gzip [option] filename

用法:

  • -#(1-9) :选择压缩比,后缀名为 .gz ,比例越高,压缩越慢,压缩后文件越小

  • -d :解压缩,gzip -d同gunzip

zcat :查看.gz压缩文件内容

[root@localhost ~]# ls
1.txt.bak  anaconda-ks.cfg  package-lock.json
[root@localhost ~]# gzip 1.txt.bak 
[root@localhost ~]# ls
1.txt.bak.gz  anaconda-ks.cfg  package-lock.json
[root@localhost ~]# zcat 1.txt.bak.gz 
123
[root@localhost ~]# gzip -d 1.txt.bak.gz 
[root@localhost ~]# ls
1.txt.bak  anaconda-ks.cfg  package-lock.json

bzip2 命令

介绍:后缀名为 .bz2 ,不可压缩目录 ,需要包 bzip2

用法:bzip2 [option] filename

  • bzip2 filename

  • -d : 解压

bzcat :查看.bz2压缩文件内容

zip 命令

介绍:将file压缩成file.zip 要先指定压缩后的文件名,原文件不消失

格式:zip [option] file.zip file ...

用法:

  • zip file.zip file1 file2 ... :压缩多个文,需要zip包

  • -r dir.zip dir:压缩目录

unzip 命令:

介绍 :解压后会提示是否替换原文件,需要unzip包

格式:unzip [option] dir.zip

用法:

  • unzip dir.zip

  • -d /dir :解压到指定目录

xz 命令:

用法:

  • -d :解压

  • -f --force :解压后覆盖同名文件

xzcat 查看.xz的压缩文件

文件打包

tar 命令:

介绍:打包工具

格式:tar [option] xx.tar filename ...

用法:

  • -c :建立包

  • -v :可视化

  • -f :

    使用存档文件或设备存档, tar -cvf 1.tar 111 222 333 :将多个文件打包,原文件不消失

  • --exclude file1 [--exclude file2]:打包目录时过滤文件

  • -tf 1.tar:查看.tar的文件

  • -C /dir :解包到指定目录

  • -x : 解包

  • -z :gzip 方式压缩,也是默认压缩方式,-zcvf 1.tar.gz 111 222:打包并压缩为gz文件

  • -j :bzip2方式压缩 ,-jcvf 1.tar.bz2 111 222 :打包并压缩为bz2文件 ,-jxvf 解压包 ,-tf 查看包

  • -J:xz 方式压缩,-Jcvf 1.tar.xz 111 222 :打包并压缩为xz文件 ,-Jxvf 解压包 ,-tJf 查看包

最后更新于

这有帮助吗?