tar 命令请教

2021-04-22 10:05:40 +08:00
 dazkarieh

写了个 crontab 任务运行备份脚本,想把 hdd 文件夹中大于 100M 的文件及某些格式筛除后,再用 tar 打包放在 dropbox 目录下备份。试运行了下,发现遇到文件名中有空格的就报错,而且压缩出来的包明显比原文件夹大

$ find ./hdd -type f -size -100M -print0 | xargs tar -zcpf ~/Dropbox/backupfile/hdd-"$(date '+%Y-%m-%d').tar.gz"  --null -T - --exclude='*psd' --exclude='*.key' ./hdd
tar: 2.jpg: Cannot stat: No such file or directory
tar: Error exit delayed from previous errors.

所以我先把所有的空格替换为_,没报错了

find ./hdd -type f -name "* *" -exec rename "s/\s/_/g" {} \;
find ./hdd -type f -size -100M -print0 | xargs tar -zcpf ~/Dropbox/backupfile/hdd-"$(date '+%Y-%m-%d').tar.gz"  --null -T - --exclude='*psd' --exclude='*.key' ./hdd

之后试了另一种写法,也不会报错

find ./hhd -type f -size -100M -print0 | xargs tar -czf ~/Dropbox/backupfile/hhd-"$(date '+%Y-%m-%d').tar.gz"  --null -T - --exclude='*psd' --exclude='*.key' ./hhd

不明白问题出在哪里,还有另一个问题:压缩包里有个 PDF 文件大小为 187M,find 筛选无效怎么破?

我是 macOS 下的 tar,纯小白请勿见笑。

1963 次点击
所在节点    Linux
5 条回复
WishMeLz
2021-04-22 10:09:36 +08:00
我只用过 tar -xvf nodejs.xz
owwlo
2021-04-22 10:28:11 +08:00
xargs -I{} tar ... "{}"
owwlo
2021-04-22 10:29:24 +08:00
栗子:
```
owwlo@owwlo-MS-7A72:~$ find . -maxdepth 1 -type f -name "wu*"
./wulala wakaka.doc
owwlo@owwlo-MS-7A72:~$ find . -maxdepth 1 -type f -name "wu*" | xargs -I{} tar -zcpf wulala.tar.gz "{}"
owwlo@owwlo-MS-7A72:~$ find . -maxdepth 1 -type f -name "wu*"
./wulala.tar.gz
./wulala wakaka.doc
```
owwlo
2021-04-22 10:37:46 +08:00
另,tar 用法我没记错的话,最后的 positional 参数是“需要被压缩的文件”。

```
... tar -czf {path} --null -T - --exclude='*psd' --exclude='*.key' ./hhd
```
这样写的话除了你传给 tar 经过 find 的文件 tar 会压缩,tar 每次似乎还会压缩参数最后“./hhd”里面所有的文件
dazkarieh
2021-04-22 11:07:58 +08:00
owwlo #4 确实是这样,反复压缩……谢谢!!自己搞定了,好像按下面写没问题。
```
find ./hdd -type f -size -100M -print0 | tar -zcpf ~/Movies/hdd-"$(date '+%Y-%m-%d').tar.gz" --exclude='*psd' --exclude='*.key' --null -T -
```

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/772373

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX