V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
dazkarieh
V2EX  ›  Linux

tar 命令请教

  •  
  •   dazkarieh · 2021-04-22 10:05:40 +08:00 · 1935 次点击
    这是一个创建于 1072 天前的主题,其中的信息可能已经有所发展或是发生改变。

    写了个 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,纯小白请勿见笑。

    5 条回复    2021-04-22 11:07:58 +08:00
    WishMeLz
        1
    WishMeLz  
       2021-04-22 10:09:36 +08:00
    我只用过 tar -xvf nodejs.xz
    owwlo
        2
    owwlo  
       2021-04-22 10:28:11 +08:00
    xargs -I{} tar ... "{}"
    owwlo
        3
    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
        4
    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
        5
    dazkarieh  
    OP
       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 -
    ```
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5256 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 07:23 · PVG 15:23 · LAX 00:23 · JFK 03:23
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.