shell 删除指定日期间的脚本

脚本描述

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!

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值