
我需要刪除 2018 年創建的目錄中的所有檔案。
答案1
使用find
-newermt
選項:
find /path/to/directory -type f -newermt 2018-01-01 ! -newermt 2019-01-01 -print
或更少的思考:
y=2018
find /path/to/directory -type f -newermt $y-01-01 ! -newermt $((y+1))-01-01 -print
如果您對結果滿意,請更改-print
為。-delete
答案2
像這樣的腳本:
DIR="$1"
for f in "$DIR"/*
do
FileDate=$(stat -c %y "$f")
if [[ "${FileDate:0:4}" = "2018" ]] ; then
rm "$f"
fi
done
呼叫腳本並傳遞目錄名稱,例如kill2018 /home/me/Documents