How to use RM gracefully to prevent accidental deletion?

Source: Internet
Author: User

There was an old terrier in the IT community, a database manager at a forum complaining that his boss had been abusing him, and he was in a fury to delete the library.

So...

BEIJING, August 20 (Xinhua), a software engineer in Beijing, Xu after leaving the company due to failure to settle wages, then use its design in the Web site of the backdoor file to remove all the source code. 20th, Beijing Fengtai District People's court learned that Xu destroyed the computer information system crime was established, was sentenced to five years.

I accidentally executed the command while the server was being maintained ... rm -rf Now the whole server was deleted by me the swelling???

Well, let me introduce you now rm .

The power of RM-RF

rmis the Linux system to delete files under the command, representing the removal of everything under -r this, everything that kind of everything. findicates that no user acknowledgement is required and is executed directly.

Usually this command is used for a specified folder, such as

rm -rf /home/test/

is to delete everything under this folder/home/test/.

However, if the path to the following folder is not added,

rm -rf /On the server also means ...

As the saying goes: Often walk along the river, where not wet shoes. So how do you avoid this kind of tragedy?

How to avoid running again?

One solution is to redirect rm commands to be grafted as mv commands, which is equivalent to customizing a recycle Bin to the Linux system.

Here's how it's implemented:

### 重定义rm命令 #### 定义回收站目录trash_path=‘~/.trash‘# 判断 $trash_path 定义的文件是否存在,如果不存在,那么就创建 $trash_path.if [ ! -d $trash_path ]; then    mkdir -p $trash_pathfi# 定义别名:使用 rm 就调用 trashalias rm=trash# 使用 rl 就调用 ‘ls ~/.trash‘ # 如果更改上面的回收站目录这里的目录也需要修改alias rl=‘ls ~/.trash‘# 使用 unrm 就调用 restorefile,需要在删除目录的父目录下执行alias unrm=restorefile# 使用 rmtrash 就调用 claearteashalias rmtrash=cleartrash# 恢复文件的函数restorefile(){    mv -i ~/.trash/[email protected]  ./} # 删除文件的函数trash(){      mv [email protected]  ~/.trash/} # 清空回收站的函数cleartrash(){    read -p "确定要清空回收站吗?[y/n]" confirm    [ $confirm == ‘y‘ ] || [ $confirm == ‘Y‘ ]  && /bin/rm -rf  ~/.trash/*}

Finally, the above script is written /etc/bashrc and the immediate execution of the command source /etc/bashrc takes effect immediately.

Use

This script defines a few commands:

    • rl: View the files under the Recycle Bin
    • unrm 文件名或目录: Restores to the current path
    • rmtrash: Empties the Recycle Bin, but it will be a friendly reminder.

The Execute RM is not actually deleted, but instead uses the MV to move to the Recycle Bin that we specified.

Really want to delete can be /bin/rm deleted.

Also, note that rm some parameters of the previous instruction may no longer be used, since it rm is actually mv the case.

# touch hoxistest# touch 1# mkdir haha# ls1  haha  hoxistest# rm 1# lshaha  hoxistest# 查看回收站文件# rl1  myftp1# 恢复已删除文件# unrm 1[[email protected] test]# ls1  haha  hoxistest# rm haha/# rm hoxistest# rlhaha  hoxistest# 情况回收站# rmtrash确定要清空回收站吗?[y/n]y# rl

Look at the effect should also be OK.

Routine summary

It is also possible to look at, but there are some problems, such as the deletion of files can not duplicate, if the same name will prompt you whether to overwrite. Then you need to do special processing, such as the deletion of a time stamp or something, interested in the implementation of the next bar.

Via:https://www.cloudbility.com/club/6981.html

If feel useful, welcome to pay attention to my, study together, common progress, not regularly launch books activities ~

Recently gathered in the web video, video content covering Python, Java, PHP, front end, applets, algorithms, architecture, database and so on! Follow the public number, backstage reply "mu lesson net" can get.

How to use RM gracefully to prevent accidental deletion?

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.