Prevent RM misoperation to build Recycle Bin under Linux

Source: Internet
Author: User

1. Add the following script to a directory (e.g./data/shell)

script Name:rm-remove.sh

Script content:

#!/usr/bin/env Bash

# func: Security: How to replace RM

# TIME:2018/2/1

# EDITOR:YW

# --------------------

para_cnt=$#

Trash_dir= "/data/backup/.zc_trash"

Date= ' Date +%f '

Trash_dir_d=${trash_dir}/${date}

if [!-D ${trash_dir_d}]; then

Mkdir-p ${trash_dir_d}

Fi

For I in $*

Do

stamp= ' Date +%s '

if [!-D ${trash_dir_d}/${stamp}]; then

Mkdir-p ${trash_dir_d}/${stamp}

Fi

Pwdname= ' dirname ${i} '

Filename= ' basename ${i} '

MV ${i} ${trash_dir_d}/${stamp}/

echo "rm ${pwdname}/${filename}" >> ${trash_dir_d}/${stamp}/${stamp}.log

Done

2. Create Recycle Bin directory

[Email protected] shell]# Mkdir/data/backup/.zc_trash

3. Add Environment variables

[Email protected] shell]# vim ~/.BASHRC

Add script execution in the last line of alias

Alias rm= "Sh/data/shell/rm-remove.sh"

4. Environment variable Effective

[Email protected] ~]# source ~/.BASHRC

5. Testing

Create a new file or directory in any directory to perform an RM delete operation will error

[[email protected] ~]# Touch cc

[Email protected] ~]# rm-f cc

DirName: Invalid option--F

Try executing "dirname--help" for more information.

BaseName: Invalid option--F

Try executing "basename--help" for more information.

MV: The target file to be manipulated is missing after "/data/backup/.zc_trash/2018-02-01/1517462127/"

Please try the "MV--help" to get more information.

[Email protected] ~]#

We performed the RM operation and found that the current directory does not exist in the cc file, but we just did the MV operation of the file, and did not really delete,

6. Switch to the Recycle Bin directory to view

[Email protected] shell]# cd/data/backup/.zc_trash/

[email protected]. zc_trash]# ls

2018-02-01

[email protected]. zc_trash]#

We can see that there is a time directory under the current directory , and you will see a Unix time name directory after entering

[email protected]. zc_trash]# CD 2018-02-01/

[[email protected] 2018-02-01]# ls

1517462127

[Email protected] 2018-02-01]#

We use the Unxi decoding tool

[Email protected] 2018-02-01]# date-d @1517462127 + "%y-%m-%d%h:%m:%s"

2018-02-01 13:15:27

[Email protected] 2018-02-01]#

after entering this directory, I see a log file and the cc file of our RM just now.

[[Email protected] 2018-02-01]# CD 1517462127/

[[email protected] 1517462127]# ls

1517462127.log cc

[Email protected] 1517462127]#

View the log contents for the RM Operation command we just executed

[email protected] 1517462127]# cat 1517462127.log

RM/

Rm./cc

[Email protected] 1517462127]#

7, comprehensive: After we have made the above restrictions, the deleted items will be mv to this Recycle Bin directory and will be the Unix time directory

Prevent RM misoperation to build Recycle Bin under Linux

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.