gpt4 book ai didi

linux - 扩展特定限制时从 unix 服务器中删除文件

转载 作者:太空宇宙 更新时间:2023-11-04 12:04:57 25 4
gpt4 key购买 nike

我编写了一个脚本来从 unix 服务器检索数据使用情况,并发送电子邮件通知我的同事。现在我想在 if 语句中插入一个 block ,以删除一些两天前的文件。

我已经尝试过删除命令,但我对路径有点迷路(顺便说一句,文件存储在 unix 服务器中)。请在下面找到我的代码

    SPACE=$(df -P /data |tail -1 |awk '{print $5}' |cut -d '%' -f1)
echo $SPACE
if [ "$SPACE" -gt 75 ]; then
echo "Warning you run out of space,Please delete from server" | mailx -r sender@mail.com -s "server_Data_Usage" receiver@mail.com
else
echo "Enough space " | mailx -r sender@mail.com -s "server_Data_Usage" receiver@mail.com
fi

提前致谢,帕诺斯

最佳答案

您好,您可以使用 find 命令轻松完成此操作,如下所示:

 find /data/ -mindepth 1 -type f -mtime +2 | xargs rm

在上面的命令中,find 将搜索/data 下的所有文件并列出 2 天前的文件,并通过管道将其重定向到 rm 命令将永久删除文件。

现在您需要像下面这样修改您的脚本:-

SPACE=$(df -P /data |tail -1 |awk '{print $5}' |cut -d '%' -f1)
echo $SPACE
if [ "$SPACE" -gt 75 ]; then
echo "Warning you run out of space,Please delete from server" | mailx -r sender@mail.com -s "server_Data_Usage" receiver@mail.com
find /data/ -mindepth 1 -type f -mtime +2 | xargs rm
else
echo "Enough space " | mailx -r sender@mail.com -s "server_Data_Usage" receiver@mail.com
fi

希望对你有帮助

关于linux - 扩展特定限制时从 unix 服务器中删除文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51057312/

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