Author: Zhang Yanfeng, please indicate the source pseudonym: clouds dreams
51CTO course address: https://edu.51cto.com/lecturer/12750547.html Linux technology exchange group: 1127825548
We all know that the "rm -rf" command is very dangerous. If you type too many commands, you will inevitably make mistakes. Often walking by the river, you will inevitably get wet shoes!
In a production environment, if your files are not backed up, you make a hand mistake, type the wrong command, and drop the folder you originally wanted to leave to rm -rf. After a few days, all my hard work was gone, damn, I have all the desire to die! Of course the mentality has to be steady!
A few lessons:
1. Before rm, especially rm -rf, be careful, three or four, or just rewrite the command.
2. Make a backup and have a convenient backup script.
3. Make regular backups. There is a predecessor who makes regular scripts and executes them on a regular basis every day. Even if they are deleted by mistake, they will not be so sad.
The purpose of the experiment: We just want to make a recycle bin on the linux server the same as the windows system, so that even if we delete the wrong file, it can be retrieved through the recycle bin, which can greatly reduce our losses! (This is a small detail of operation and maintenance)
The experiment started:
rm command is removed
The rl command is to view the contents of the recycle bin, which is equivalent to the ls recycle bin.
The ur command is to restore the specified file.
The cleartrash command is to empty the recycle bin.
First, set up a recycle bin
[root@localhost ~]# mkdir -p ~/.trash
Add the following content under ~.bashrc or bash_profile
[root@localhost ~]# vi ~/.bash_profile
alias rm=trash
alias r=trash
alias rl='ls ~/.trash'
alias ur=undelfile
undelfile()
{
mv -i ~/.trash/$@ ./
}
trash()
{
mv $@ ~/.trash/
}
[root@localhost ~]# . ~/.bash_profile
Note: rm is equivalent to executing mv. At this time, there is no rm -rf. The deleted file will appear in .trash, so we can restore it back.
Test the main function of the recycle bin:
Create the /qq folder and delete it to see if the recycle bin exists.
[root@localhost ~]# mkdir /qq
[root@localhost ~]# rl
[root@localhost ~]# rm -f /qq
[root@localhost ~]# rl
qq <== exists
To restore files in the recycle bin, you need to specify the file name, or you can directly enter the folder mv out!
[root@localhost ~]# ll
total 4
-rw-------. 1 root root 1193 Nov 30 03:41 anaconda-ks.cfg
[root@localhost ~]# ur qq
[root@localhost ~]# ll
total 4
-rw-------. 1 root root 1193 Nov 30 03:41 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Feb 3 22:49 qq
Note: Using the ur command to restore files will only restore the files to the user's home directory.
The test is over!
There is a problem here. If you delete things in the recycle bin, the rm command is useless here.
Add functions in .bashrc
[root@localhost ~]# vi .bashrc
cleartrash()
{
read -p "clear sure?[n]" confirm
[ $confirm == "y" ] || [ $confirm == "Y" ] && /usr/bin/rm -rf ~/.trash/*
}
$..bashrc
[root@localhost ~]# . ~/.bashrc
Then, if you want to empty the recycle bin, use the "cleartrash" command.
Test the emptying function of the recycle bin:
[root@localhost ~]# rl
[root@localhost ~]# rm -f qq/
[root@localhost ~]# rl
[root@localhost ~]# cleartrash
clear sure?[n]y
[root@localhost ~]# rl
Empty!
The test is over!
So far the experiment is over!