惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
H
Help Net Security
云风的 BLOG
云风的 BLOG
Apple Machine Learning Research
Apple Machine Learning Research
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
D
Docker
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Blog — PlanetScale
Blog — PlanetScale
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
GbyAI
GbyAI
博客园 - Franky
B
Blog RSS Feed
Stack Overflow Blog
Stack Overflow Blog
L
LangChain Blog
量子位
V
Visual Studio Blog
Y
Y Combinator Blog
小众软件
小众软件
N
Netflix TechBlog - Medium
博客园 - 三生石上(FineUI控件)
Microsoft Security Blog
Microsoft Security Blog
雷峰网
雷峰网

热爱生活与梦想

迁移LeanCloud的Waline评论数据到TiDB/Neon 25年江浙地区暑假自驾游之感慨 使用阿里云灵码继续完成构建 SSL 证书管理器 亲手给儿子做的棒棒糖摩天轮六一礼物 使用腾讯云 CodeBuddy 构建 SSL 证书管理器 修复代码块默认高度相关缺陷 HugoNexT4.7.2 新功能和升级提示 改善代码块折叠和选中功能 支持Github风格的警告样式 新增音乐短代码功能支持 给文章添加摘要和过期提示功能 回顾24年的过去总结 修复Mathjax行内显示公式的问题 给Hugo文章增加阅读更多跳转的锚点定位功能 Win10隐匿的VPN设置和L2TP连接常见错误 Go语言中“糟糕”的日期时间格式化设计 DaoCloud道客提供的免费Docker镜像代理服务 感谢万能淘宝让自己吃到喜欢的美食 WSL运行时遇到未知异常错误无法使用 隐藏的换行符导致Base64加密解密失败 MySQL自带客户端直接免密登录操作 Linux系统中删除目录软链接的注意项 Java程序调用外网API时CA问题 如何不关机重启WSL2恢复虚拟服务 在Windows上安装Podman容器平台做虚拟化 记一次无法弹出移动硬盘的记录 为友情链接添加自动检测脚本 重新激活Github的2FA认证 继续小米手环的健康生活之旅 一次意外的“坠机”引发了我对手机保护壳的深思
Linux中使用tar压缩命令排除文件
凡梦星尘 · 2023-10-28 · via 热爱生活与梦想

众所周知tar命令是在Linux系统中最为常用来解压缩文件的命令之一,之前大部分时候都直接用它来压缩备份或转移的文件内容,因此也未过多关注过它在压缩时的其它可选参数使用。但最近在转移文件遇到其占用空间比较大,考虑到里面有些内容并不是必须,于是想到如何来使用tar命令参数来实现,经过多次尝试,找到了个解决办法——使用exclude-from参数,可灵活控制不需要压缩文件,然后顺手做个记录分享。

注:当排除的内容并不多时,也可以直接使用exclude参数会更方便些。

exclude-from 参数的使用说明相对简单,其后面跟的是排除文件的路径。但是需要注意如下2种不同的情况:

  1. 当排除文件的路径是相对路径时,压缩路径无论是相对路径还是绝对路径都可以;
  2. 当排除文件的路径是绝对路径时,压缩路径也必须是绝对路径。

接下来我们就准备个测试的文件夹和文件,整个目录结构如下,其中以exclude起头的文件夹或文件便是需要排除的内容:

1
2
3
4
5
6
7
8
9
tar_excludes_demo/
├── exclude_file.txt
├── exclude_folder
│   └── tmp.txt
├── folder1
│   └── tmp.txt
├── folder2
│   └── tmp.txt
└── readme.txt

根据参数说明的两种情况进行分类的测试验证:

示例1 相对路径

我们先把需要排除的内容写到文本文件里面,如下:

1
2
3
excludes_relative.txt
tar_excludes_demo/exclude_file.txt
tar_excludes_demo/exclude_folder

然后使用不同相对路径压缩办法测试下效果,加上v参数可以验证是否正确:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 任意目录压缩
tar --exclude-from /root/tar_demo/excludes_relative.txt -czvf tar_excludes_demo.tar.gz /root/tar_demo/tar_excludes_demo
tar: Removing leading '/' from member names
/root/tar_demo/tar_excludes_demo/
/root/tar_demo/tar_excludes_demo/folder1/
/root/tar_demo/tar_excludes_demo/folder1/tmp.txt
/root/tar_demo/tar_excludes_demo/readme.txt
/root/tar_demo/tar_excludes_demo/folder2/
/root/tar_demo/tar_excludes_demo/folder2/tmp.txt

# 父目录外面压缩,同时去除父目录路径
tar --exclude-from tar_demo/excludes_relative.txt  -czvf tar_excludes_demo.tar.gz -C tar_demo tar_excludes_demo
tar_excludes_demo/
tar_excludes_demo/folder1/
tar_excludes_demo/folder1/tmp.txt
tar_excludes_demo/readme.txt
tar_excludes_demo/folder2/
tar_excludes_demo/folder2/tmp.txt

# 父目录里面压缩
tar --exclude-from excludes_relative.txt -czvf tar_excludes_demo.tar.gz tar_excludes_demo
tar_excludes_demo/
tar_excludes_demo/folder1/
tar_excludes_demo/folder1/tmp.txt
tar_excludes_demo/readme.txt
tar_excludes_demo/folder2/
tar_excludes_demo/folder2/tmp.txt

观察压缩的执行过程,可清晰的看出使用相对路径,无论是哪个位置或方式压缩文件都能达到预期要求,成功排除不需要的内容。接下来我们看看绝对路径的效果:

示例2 绝对路径

同样是先把需要排除的内容写到文本文件里面,如下:

1
2
3
excludes_absolute.txt
/root/tar_demo/tar_excludes_demo/exclude_file.txt
/root/tar_demo/tar_excludes_demo/exclude_folder

然后使用绝对路径压缩办法测试下效果,加上v参数可以验证是否正确:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 绝对路径压缩
tar --exclude-from /root/tar_demo/excludes_absolute.txt -czvf tar_excludes_demo.tar.gz /root/tar_demo/tar_excludes_demo
tar: Removing leading '/' from member names
/root/tar_demo/tar_excludes_demo/
/root/tar_demo/tar_excludes_demo/folder1/
/root/tar_demo/tar_excludes_demo/folder1/tmp.txt
/root/tar_demo/tar_excludes_demo/readme.txt
/root/tar_demo/tar_excludes_demo/folder2/
/root/tar_demo/tar_excludes_demo/folder2/tmp.txt

# 尝试相对路径压缩
tar --exclude-from tar_demo/excludes_absolute.txt -czvf tar_excludes_demo.tar.gz tar_demo/tar_excludes_demo
tar_demo/tar_excludes_demo/
tar_demo/tar_excludes_demo/exclude_folder/
tar_demo/tar_excludes_demo/exclude_folder/tmp.txt
tar_demo/tar_excludes_demo/exclude_file.txt
tar_demo/tar_excludes_demo/folder1/
tar_demo/tar_excludes_demo/folder1/tmp.txt
tar_demo/tar_excludes_demo/readme.txt
tar_demo/tar_excludes_demo/folder2/
tar_demo/tar_excludes_demo/folder2/tmp.txt


# 尝试绝对路径转移方式
tar --exclude-from /root/tar_demo/excludes_absolute.txt -czvf tar_excludes_demo.tar.gz -C /root/tar_demo tar_excludes_demo
tar_excludes_demo/
tar_excludes_demo/exclude_folder/
tar_excludes_demo/exclude_folder/tmp.txt
tar_excludes_demo/exclude_file.txt
tar_excludes_demo/folder1/
tar_excludes_demo/folder1/tmp.txt
tar_excludes_demo/readme.txt
tar_excludes_demo/folder2/
tar_excludes_demo/folder2/tmp.txt

观察压缩的执行过程,可清晰的看出使用绝对路径的方式,只能支持绝对路径这一种压缩方法。

通过对比相对路径绝对路径两种不同方式的压缩文件排除方法效果,在此更建议使用相对路径的方式,它可以灵活支持各种压缩方式的执行,同时也不用写那很长串的路径参数,另外要注意的点就是当排除的内容是文件夹时,排除的路径只要写到文件夹名称即可,万万不可在最后加上/结尾,切记!!!