
以下腳本適用於單一資料夾,但我想迭代所有子目錄。
ls /home/user/Desktop/cron_database_hourly/*/*_[012][0-9]*.zip | head -n -1 | \
while read -r f; do rm "$f"; done
答案1
你可能會逃脫類似(未經測試)的事情 -
for each in `find -type d /home/usr/Desktop/cron_database/hourly`
do
ls -t $each/_[012][0-9]*.zip | head -n -1 | \ while read -r f;
do
rm "$f"
done
done
「foreach」將循環查找所有目錄的列表,並且向 ls 添加“-t”將導致它按日期順序排序 - 最近的在前。