腳本描述
1. 刪除指定日期間的文件,日期格式YYYY-MM-DD;
2. 用戶輸入需要刪除的目錄的路徑,默認當前路徑;
3. 提供文件列表讓用戶確認
4. 刪除前需要用戶多次確認,如輸入當輸入爲Y,yes等條件時執行刪除,當爲N,no 等條件時退出,當輸入爲其他值時重新確認;
#!/bin/bash
## delete specified path files
echo -n "Input your dictionary(Default is your current path): ";
read path;
if [ $path ]
then
apath=$path
else
apath=`pwd`
fi
echo "Current dictionary is: $apath";
echo -n "In put start date,format'YYYY-MM-DD':"
read start;
if [ $start ]
then
astart=$start
else
astart=`date +%Y-%m-%d`
fi
echo "From data : $astart"
echo -n "In put end date,format'YYYY-MM-DD'(not include):"
read end;
if [ $end ]
then
aend=$end
else
aend=`date +%Y-%m-%d`
fi
echo "To data : $aend"
echo "Confirm the list you want to delete."
list="`find $apath -newermt $astart ! -newermt $aend -exec ls -l {} \; |wc -l`"
echo $list
if [ $list -eq 0 ]
then
echo "There is no file between $astart and $aend"
else
echo "There are $list files will be deleted.List as below: "
echo -n "Do you want delete above $list files?[Y/N]: "
while true
do
read confirm;
echo $confirm;
case $confirm in
Y|y|Yes|yes|YES) echo "Your input is Yes! List files will be deleted."
find $apath -newermt $astart ! -newermt $aend -exec rm -f {} \;
break
;;
N|n|No|no|NO) echo "Your input is No! Quit normal."
break
;;
*) echo "Your input is not correct,please try again."
;;
esac
done
#find $apath -newermt $astart ! -newermt $aend -exec rm -f {} \;
echo "Delete successful!"
fi
運行效果如下:
[oracle@100001 ~]$ sh del.sh
Input your dictionary(Default is your current path):
Current dictionary is: /home/oracle
In put start date,format'YYYY-MM-DD':2019-04-10
From data : 2019-04-10
In put end date,format'YYYY-MM-DD'(not include):2019-04-11
To data : 2019-04-11
Confirm the list you want to delete.
2
There are 2 files will be deleted.List as below:
-rw-r--r-- 1 oracle oinstall 11917 Apr 10 04:10 /home/oracle/S5.CDP_MERGE_DATA.sql
-rw-r--r-- 1 oracle oinstall 16197 Apr 10 07:10 /home/oracle/S4.CDP_TRAN_COMP_DATA.sql
Do you want delete above files?[Y/N]: k
k
Your input is not correct,please try again.
Y
Y
Your input is Yes! List files will be deleted.
Delete successful!