gpt4 book ai didi

linux - 用于循环和删除的 Shell 脚本

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:56:22 26 4
gpt4 key购买 nike

有人可以帮我解决这个问题吗?我有如下所示的文件夹结构。我想遍历备份测试中的每个文件夹并删除除今天日期文件夹之外的所有文件夹。我希望它作为 cron 作业运行

enter image description here

最佳答案

为此使用find:

today="$(date +%Y-%m-%d)"
find /path/to/backuptest/Server* -mindepth 1 -maxdepth 1 -type d -not -name "$today" -exec rm -R {} \;

编辑

要不删除包含日期结构的目录以外的目录,请使用类似

find /path/to/backuptest/Server* -mindepth 1 -maxdepth 1 -type d  -regex ".*2016-[0-1]*[0-9]-[0-3][0-9]$" -not -name "$today"

关于linux - 用于循环和删除的 Shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39278389/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com