Centos implements the recycle bin mechanism and centos recycle bin Mechanism

Source: Internet
Author: User

Centos implements the recycle bin mechanism and centos recycle bin Mechanism

As an O & M personnel, rm * is often directly used to delete files on the server for convenience *. txt and other Wildcards are even added-rf parameters to save trouble. If it is OK, it would be nice. If it is left blank when it is deleted, it would be too big.

As the saying goes, there are always wet shoes on the Riverside station, and no one can guarantee that they will be able to make a mistake, but it is not a problem to become an obsessive-compulsive disorder every day, so I thought of a Windows-like recycle bin function on the linux server.

The following is my script for implementing the recycle bin. to delete a file after enabling the recycle bin, you only need to execute "del file name/folder name ".

1 [root@localhost tools]# tree Trash_1.0/2 Trash_1.0/3 ├── install_Trash.sh4 └── Trash_mkdir.sh

 

Very simple: there are two script files. Trash_mkdir.sh is the content written to the crontab scheduled task. It is responsible for generating the recycle bin folder of the corresponding date every day and regularly clearing the files in the recycle bin for more than five days, that is to say, the recycle bin can retain the files you deleted within five days. The install_Trash.sh script is responsible for automatically configuring scheduled tasks and loading user personalized configurations.

Trash_mkdir.sh content:

1 [root @ localhost Trash_1.0] # cat Trash_mkdir.sh 2 #! /Bin/bash 3 month = 'date "+ % m" '# obtain the current month 4 day = 'date "+ % d"' # obtain the current date 5 6 last_day = 'date -d "-1 day" "+ % d" '# obtain the day before yesterday 7 last_day_m = 'date-d "-1 day" "+ % m"' # obtain the day before yesterday 8 9 del_day = 'date-d "-5 day" "+ % d" '# obtain the last month 10 del_day_m = 'date-d "-5 day" "+ % m "'# Get The date 11 12 Trash_dir ="/tmp/del_bak "# recycle bin directory 13 tmp_dir ="/tmp/del_bak/tmp "# delete the file storage directory 14 every day # create a directory, and grant 777 permissions 15 if [! -D $ Trash_dir] 16 then17/bin/mkdir-p $ Trash_dir18/bin/chmod 777 $ Trash_dir19 fi20 21 if [! -D $ tmp_dir] 22 then23/bin/mkdir-p $ tmp_dir24/bin/chmod 777 $ tmp_dir25 fi26 27 if [! -D/tmp/del_bak/$ month] 28 then29/bin/mkdir/tmp/del_bak/$ month30/bin/chmod 777/tmp/del_bak/$ month31 fi32 33 if [! -D/tmp/del_bak/$ month/$ day] 34 then35/bin/mkdir/tmp/del_bak/$ month/$ day36/bin/chmod 777/tmp/del_bak/$ month/$ day37 fi38 39 Trash_file = '/bin/ls-A $ tmp_dir' 40 41 if ["$ Trash_file "! = ""] # This is scheduled to put the files deleted yesterday in the directory of the month and date categories in the early morning of every day, ensure that only the files deleted on the current day are stored in the/tmp/del_bak/tmp directory. 42 then43 cd $ tmp_dir44 if [! -D/tmp/del_bak/$ last_day_m/$ last_day/] 45 then46/bin/mkdir-p/tmp/del_bak/$ last_day_m/$ last_day/47 fi48/bin/mv $ tmp_dir/*/tmp/del_bak/$ last_day_m/$ last_day/49 fi50 51 if [-d $ Trash_dir/$ del_day_m/$ del_day/] # Clear files deleted five days ago 52 then53 cd $ Trash_dir/$ del_day_m/$ del_day/& {54/bin/rm-rf $ Trash_dir/$ del_day_m/$ del_day/55} 56 fi

 

Install_Trash.sh script content:

1 [root @ localhost Trash_1.0] # cat install_Trash.sh 2 #! /Bin/bash 3 USER = '/usr/bin/whoam' # obtain the path where the current USER 4 TOOLS = "/usr/local/tools" # stores the Trash_mkdir.sh scheduled task script, you can modify 5 HOME_DIR = '/bin/grep "$ USER"/etc/passwd | awk-F "by yourself ": "'{print $6}'' # obtain the current user's home directory 6 CONF = $ HOME_DIR "/. bashrc "# splice the current user's configuration file path 7 Trash = '/bin/grep" del "$ conf' # determine whether the recycle bin mechanism is configured 8 9 if [" $ Trash" = ""] # Add the recycle bin alias del10 then11 echo "alias del = 'mv-t/tmp/del_bak/tmp/-- backup = t'"> $ CONF12 fi13 14 if [! -D $ TOOLS] 15 then16/bin/mkdir-p $ TOOLS17 fi18 19/bin/cp Trash_mkdir.sh $ TOOLS # copy the scheduled task script to the specified directory 20/bin/chmod + x $ TOOLS/Trash_mkdir.sh21 22 if [-z "'grep' Trash _ mkdir. sh '/var/spool/cron/root' "] # Check whether the recycle bin script 23 then24 echo" 10 0 **** $ TOOLS/Trash_mkdir.sh "is added to the crontab of the scheduled task.> >/var/spool/cron/root25 fi26 27/bin/sh $ TOOLS/Trash_mkdir.sh # initialize the recycle bin

 

Deployment description:

1. Put the two scripts in the same directory.

2. You can run the install_Trash.sh script to enable the recycle bin function.

The above is the recycle bin mechanism implemented on the Centos6.5 server. If you are interested, you can discuss the improvement.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.