bash - 存档超过 x 天的文件

标签 bash unix

这个问题在这里已经有了答案:





How to create tar for files older than 7 days using linux shell scripting

(3 个回答)


4年前关闭。




当所有文件(到一个 .tar.gz 文件)超过 X 天时,我想将它们归档到一个目录中。

我有一个类轮:

find /home/xml/ -maxdepth 1 -mtime +14 -type f -exec sh -c \ 'tar -czvPf /home/xml/archive/archive_$(date +%F).tar.gz $0' {} \;

当我运行此命令时,我看到在此目录中选择了正确的文件,但存档中只有最后一个文件。有没有办法将所有文件放入一个 tar.gz 存档中?

@Alex 回答后的另一个问题:仍然缺少许多文件,请查看屏幕截图。

enter image description here
也许冒号 (:) 导致了问题?

最佳答案

-exec为每个选定的文件运行命令,因此它会在其中写入一个文件的 tar,然后为每个源文件覆盖它,这解释了为什么您只获得最后一个文件。您可以使用 find生成你想要的文件列表,然后通过 xargs 管道它将列表作为参数传递给您的 tar命令:

find /home/xml/ -maxdepth 1 -mtime +14 -type f | xargs tar -czvPf /home/xml/archive/archive_$(date +%F).tar.gz

带冒号的文件名对我来说很好用:
% dd if=/dev/urandom of=one:1 count=1
% dd if=/dev/urandom of=two:2 count=1
% dd if=/dev/urandom of=three:3 count=1
% dd if=/dev/urandom of=four:4 count=1
% dd if=/dev/urandom of=five:5 count=1
% find . -type f | xargs tar cvf foo.tar
    ./five:5
    ./four:4
    ./two:2
    ./three:3
    ./one:1
% tar tvf foo.tar
    -rw------- alex/alex       512 2017-07-03 21:08 ./five:5
    -rw------- alex/alex       512 2017-07-03 21:08 ./four:4
    -rw------- alex/alex       512 2017-07-03 21:08 ./two:2
    -rw------- alex/alex       512 2017-07-03 21:08 ./three:3
    -rw------- alex/alex       512 2017-07-03 21:08 ./one:1

关于bash - 存档超过 x 天的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44891885/

相关文章:

linux - 在不使用unix中的排序的情况下对字符进行排序

python - 如何在 Linux 上管理基于 Python 的守护进程?

linux - 从 sudo-ed 环境启动 bash shell

java - 通过 shell 命令安装项目

linux - 如何在.sh文件中编写ssh连接代码以连接到远程计算机

php - 使用 PHP 获取 UNIX 服务器每月带宽使用情况

c - Getopt:与多个 optargs 一起使用的标志

bash - git push commit 显示错误 : unable to push to unqualified destination

python - 使用 sys.stdin.readline() 通过 "| bash "运行 python 脚本

python - rbenv 和 MacPorts 的路径位置