第8周

麒麟系统压缩解压命令

一、tar(最常用,保留权限,运维首选)

参数说明:

  • -c 创建包;-x 解压;-t 查看包内文件
  • -z gzip(tar.gz);-j bz2(tar.bz2);-J xz(tar.xz)
  • -v 显示过程;-f 指定文件名;-C 解压到指定目录
# 1. 压缩 tar.gz
tar -zcvf test.tar.gz test/
# 2. 解压 tar.gz
tar -zxvf test.tar.gz
# 3. 查看包内容
tar -ztvf test.tar.gz
# 4. 解压到指定目录
tar -zxvf test.tar.gz -C /home/kylin/tmp

# tar.bz2
tar -jcvf test.tar.bz2 test/
tar -jxvf test.tar.bz2

# tar.xz
tar -Jcvf test.tar.xz test/
tar -Jxvf test.tar.xz

# 只打包不压缩(tar)
tar -cvf test.tar test/
tar -xvf test.tar

二、zip /unzip(Windows 互通,重点:中文乱码)

# 压缩文件夹(-r 递归)
zip -r test.zip test/
# 解压
unzip test.zip
# 解压到指定目录
unzip test.zip -d ./out
# 查看压缩包内文件
unzip -l test.zip
# 【重点】Windows过来的zip,中文乱码,GBK编码解压
unzip -O GBK test.zip
# 带密码解压
unzip -P 123456 test.zip

-O GBK 大写 O,解决 Windows 压缩中文文件名乱码统信软件-...

三、7z(高压缩率,推荐)

命令:7z / 7za

# 压缩 a=add
7z a test.7z test/
# 加密压缩(回车后输入密码)
7z a -p test.7z test/
# 解压 x(保留目录结构,推荐)
7z x test.7z
# e 解压,不保留目录,全部平铺
7z e test.7z
# 查看包内容
7z l test.7z

四、rar(默认不自带,需要单独装 unrar)

# 解压rar(保留目录)
unrar x test.rar
# 解压,平铺所有文件
unrar e test.rar
# 查看
unrar l test.rar

五、gzip /bzip2(单文件压缩,不打包文件夹)

gzip test.txt        # 压缩,源文件消失
gzip -k test.txt     # -k 保留原文件
gunzip test.txt.gz

bzip2 test.txt
bunzip2 test.txt.bz2

六、常见坑

  1. zip 中文乱码:unzip -O GBK xxx.zip
  2. tar 不要把 - f 放前面,-f必须在最后
  3. 分卷 7z:直接解压第一个 xxx.001,7z x xxx.001
  4. 服务器无图形,优先 tar、7z

七、小技巧

静默解压(不输出日志)

unzip -q test.zip
tar -zxf test.tar.gz
posted @ 2026-10-28 08:03  +6  阅读(11)  评论(0)    收藏  举报