CentOS implements the recycle bin Mechanism

Source: Internet
Author: User

CentOS implements the 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.sh
4 ── 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:

[Root @ localhost Trash_1.0] # cat Trash_mkdir.sh
#! /Bin/bash
Month = 'date "+ % m" '# obtain the current month
Day = 'date "+ % d" '# obtain the current date

Last_day = 'date-d "-1 day" "+ % d" '# obtain the month before yesterday
Last_day_m = 'date-d "-1 day" "+ % m" '# obtain the date of the day before yesterday

Del_day = 'date-d "-5 day" "+ % d" '# obtain the month before the last five days
Del_day_m = 'date-d "-5 day" "+ % m" '# obtain the date five days ago

Trash_dir = "/tmp/del_bak" # recycle bin directory
Tmp_dir = "/tmp/del_bak/tmp" # delete the file storage directory every day
# The following describes how to create a directory and grant the 777 permission. 15 if [! -D $ Trash_dir]
Then
/Bin/mkdir-p $ Trash_dir
/Bin/chmod 777 $ Trash_dir
Fi

If [! -D $ tmp_dir]
Then
/Bin/mkdir-p $ tmp_dir
/Bin/chmod 777 $ tmp_dir
Fi

If [! -D/tmp/del_bak/$ month]
Then
/Bin/mkdir/tmp/del_bak/$ month
/Bin/chmod 777/tmp/del_bak/$ month
Fi

If [! -D/tmp/del_bak/$ month/$ day]
Then
/Bin/mkdir/tmp/del_bak/$ month/$ day
/Bin/chmod 777/tmp/del_bak/$ month/$ day
Fi

Trash_file = '/bin/ls-A $ tmp_dir'

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. Make sure that only files deleted on the same day are stored in the/tmp/del_bak/tmp directory.
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/
Fi

If [-d $ Trash_dir/$ del_day_m/$ del_day/] # Clear files deleted five days ago
Then
Cd $ Trash_dir/$ del_day_m/$ del_day /&&{
/Bin/rm-rf $ Trash_dir/$ del_day_m/$ del_day/
}
Fi

Install_Trash.sh script content:

[Root @ localhost Trash_1.0] # cat install_Trash.sh
#! /Bin/bash
USER = '/usr/bin/whoam' # obtain the current USER
TOOLS = "/usr/local/tools" # path for storing the Trash_mkdir.sh scheduled task script, which can be modified by yourself
HOME_DIR = '/bin/grep "$ USER"/etc/passwd | awk-F ":"' {print $6} ''# obtain the current USER's home directory
CONF = $ HOME_DIR "/. bashrc" # concatenate the current user's configuration file path
Trash = '/bin/grep "del" $ conf' # checks whether the recycle bin mechanism is configured.

If ["$ Trash" = ""] # Add the recycle bin alias del
Then
Echo "alias del = 'mv-t/tmp/del_bak/tmp/-- backup = t'"> $ CONF
Fi

If [! -D $ TOOLS]
Then
/Bin/mkdir-p $ TOOLS
Fi

/Bin/cp Trash_mkdir.sh $ TOOLS # copy the scheduled task script to the specified directory
/Bin/chmod + x $ TOOLS/Trash_mkdir.sh

If [-z "'grep' Trash _ mkdir. Sh'/var/spool/cron/root'"] # Check whether the recycle bin script has been added to the crontab of the scheduled task.
Then
Echo "10 0 **** $ TOOLS/Trash_mkdir.sh">/var/spool/cron/root
Fi

/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.

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.