[英]How to Delete files in Linux with to the date in the file name?
我的 Linux VM 服務器中有一個名為 /var/test/logs 的文件夾。 它有如下文件夾(示例):
drwxr-xr-x 3 test test 26 Dec 01 00:00 Backuplogs_202212010000
drwxr-xr-x 3 test test 26 Dec 02 00:00 Backuplogs_202212020000
drwxr-xr-x 3 test test 26 Dec 12 00:00 Backuplogs_202212120000
drwxr-xr-x 3 test test 26 Dec 13 00:00 Backuplogs_202212130000
drwxr-xr-x 3 test test 26 Dec 14 00:00 Backuplogs_202212140000
drwxr-xr-x 3 test test 26 Dec 15 00:00 Backuplogs_202212150000
我需要刪除除每個月的第 1 天、第 15 天和最后一天以外的所有文件夾,方法是僅使用上面給出的文件夾名稱,如下所示,
Backuplogs_2022 1201 0000
計算每個月的日期以刪除除第 1 天、第 15 天和最后一天之外的文件夾
有人可以建議我這樣做的邏輯嗎?
Go 到您的路徑/var/test/logs
,並嘗試刪除當月:
#Return last date of the current month
LAST=$(date -d "-$(date +%d) days +1 month" +%d)
#Remove all folders except 01, 15, 31
find . -regextype egrep ! -regex "^./Backuplogs_202301(01|15|${LAST}).*" -delete
如果你想要幾個月,那么你可以根據你的范圍在上面添加for-loop
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.