As an operator, delete files on the server in order to facilitate the frequent use of the RM *.txt such wildcard characters, even for the sake of-rf parameters, if it is OK, if the deletion of an inattentive, it can be big.
As the saying goes often in the river standing where there are not wet shoes, no one can guarantee that they can not make a mistake, but every day fear of the whole into an obsessive-compulsive disorder is not a thing, so think of the Linux server is also the whole of a similar to win's Recycle Bin function.
The following is a script I implement the Recycle Bin, if you need to delete files after the Recycle Bin is enabled, simply execute "del file name/folder names"
1 [[email protected] tools]# tree trash_1. 0/2 trash_1. 0/3├──install_trash.sh4 └──trash_mkdir.sh
Very simple, two script files, trash_mkdir.sh is written to the crontab scheduled tasks, responsible for generating the corresponding date of the Recycle Bin folder every day, and regularly clean the Recycle Bin more than 5 days of files, that is, the Recycle Bin can keep you deleted files within five days, Install_ The trash.sh script is responsible for automatically configuring timed tasks, loading user personalization configurations, and so on.
Content of trash_mkdir.sh:
1[Email protected] trash_1.0]# Cat trash_mkdir.sh2#!/bin/Bash3Month= ' Date"+%m"' #获取当前月份4Day= ' Date"+%d"' #获取当前日期5 6Last_day= ' date-d"-1 Day" "+%d"' #获取前天月份7Last_day_m= ' date-d"-1 Day" "+%m"' #获取前天日期8 9Del_day= ' date-d"-5 Day" "+%d"' #获取五天前月份TenDel_day_m= ' date-d"-5 Day" "+%m"' #获取五天前日期 One ATrash_dir="/tmp/del_bak"#回收站总目录 -Tmp_dir="/tmp/del_bak/tmp"#每天删除文件存放目录 -#下面是创建相应目录, and give 777 permissions the if[ ! -d $Trash _dir] - Then -/bin/mkdir-P $Trash _dir -/bin/chmod777$Trash _dir + fi - + if[ ! -d $tmp _dir] A Then at/bin/mkdir-P $tmp _dir -/bin/chmod777$tmp _dir - fi - - if[!-d/tmp/del_bak/$month] - Then in/bin/mkdir/tmp/del_bak/$month -/bin/chmod777/tmp/del_bak/$month to fi + - if[!-d/tmp/del_bak/$month/$day] the Then */bin/mkdir/tmp/del_bak/$month/$day $/bin/chmod777/tmp/del_bak/$month/$dayPanax Notoginseng fi - theTrash_file= '/bin/ls-A $tmp _dir ' + A if["$Trash _file"!=""] #这是定时在每天凌晨将昨天删除的文件放到已月份和日期分类的目录下 to ensure that only files deleted on the same day are stored in the/tmp/del_bak/tmp directory the Then + CD $tmp _dir - if[!-d/tmp/del_bak/$last _day_m/$last _day/ ] $ Then $/bin/mkdir-p/tmp/del_bak/$last _day_m/$last _day/ - fi -/BIN/MV $tmp _dir/*/tmp/del_bak/$last _day_m/$last _day/ the fi - Wuyi if [-D $Trash _dir/$del _day_m/$del _day/] #清理五天前删除的文件 the Then - CD $Trash _dir/$del _day_m/$del _day/&& { Wu /bin/rm-rf $Trash _dir/$del _day_m/$del _day/ - } About fi
install_trash.sh Script content:
1[Email protected] trash_1.0]# Cat install_trash.sh2#!/bin/Bash3User= '/usr/bin/WhoAmI ' #获取当前用户4tools="/usr/local/toolsthe path of the #存放Trash_mkdir. Sh timed task script, which can be modified according to the individual's own5Home_dir= '/bin/grep"$USER"/etc/passwd|awk-f":" '{print $6}'' #获取当前用户家目录6conf= $HOME _dir"/.BASHRC"#拼接当前用户的配置文件路径7Trash= '/bin/grep"del"$CONF ' #判断是否已经配置了回收站机制8 9 if["$Trash"=""] #添加回收站别名delTen Then OneEcho"alias del= ' mv-t/tmp/del_bak/tmp/--backup=t '">>$CONF A fi - - if[ ! -D $TOOLS] the Then -/bin/mkdir-P $TOOLS - fi - +/bin/CP trash_mkdir.sh $TOOLS #拷贝定时任务脚本到指定目录 -/bin/chmod +x $TOOLS/trash_mkdir.sh + A if[-Z"' grep ' trash_mkdir.sh '/var/spool/cron/root '"] #判断定时任务crontab里是否已经加了回收站脚本 at Then -Echo"0 * * * $TOOLS/trash_mkdir.sh">>/var/spool/cron/Root - fi - -/bin/sh $TOOLS/trash_mkdir.sh #初始化回收站
Deployment notes:
1. Put two scripts in the same directory
2, use the user who need to enable Recycle Bin function to execute install_trash.sh script can install with one click
The above is my Centos6.5 server on the implementation of the Recycle Bin mechanism, interested friends can discuss the improvement together.
CentOS Implementation Recycle Bin mechanism