rm -rf another tragedy, it's time to give Linux a whole recycle bin!

Posted by shaneH on Wed, 02 Mar 2022 02:47:26 +0100

you 're right! Those who understand the basics of Linux understand this command! rm -rf . If this command is not used properly, its lethality is extremely high!

The famous rm -rf tragedy!

On May 28, 2015, Ctrip suspected that the service was suspended due to the wrong operation of employees

On February 2, 2017, GitLab administrator mistakenly deleted the production database

On April 5, 2017, DigitalOcean had an accident of deleting the production database

On April 24, 2018, Kuriko announced that all data on the host was lost due to the technical personnel rm -rf / *

......

Just a while ago, bloggers even stepped on the pit!!!!

The online environment found some similar named in a directory png file! So I almost want to delete it manually.

Click and enter the following command RM - RF * > png Enter ~????

???? Last 0.5 seconds

???? Last 1 second

???? Last 1.5 seconds

???? Last 2 seconds

???? , Ctrl + C

Look at the directory quickly ls

???? Nearly half of the documents were deleted

what the hell! What about this? The directory file is basically code. The backup was 6 months ago. I was lazy and changed the directory code directly online!!!!

Run away!!!

????, Joking, running doesn't exist. Even if you run, you have to fix this problem! It was finally solved after two days.

To prevent this from happening! Especially online servers! It's time to add a recycle bin function to him!

{card-list-item}

Open source project

There are many ways to introduce online. This recommends an open source project!

https://github.com/andreafrancia/trash-cli/

{/card-list-item} {card-list-item}

Deployment installation

requirement:

Python 7.3 is also OK

PIP (use apt get install Python pip on Debian to install pip)

Installation command:

pip install trash-cli

{/card-list-item}

{card-list-item}

Source installation

Install for all users:

git clone https://github.com/andreafrancia/trash-cli.git
cd trash-cli
sudo pip install .

Install for current user:

git clone https://github.com/andreafrancia/trash-cli.git
cd trash-cli
pip install .

After installing for the current user, you may need to add the following code to bashrc:

export PATH=~/.local/bin:"$PATH"

Uninstall command:

pip uninstall trash-cli

{/card-list-item}

{card-list-item}

usage method

trash-put           Move files or directories to the recycle bin
trash-empty         Empty Trash 
trash-list          List recycle bin files
trash-restore       Recover recycle bin files
trash-rm            Delete recycle bin files

To move files to the Recycle Bin:

$ trash-put

List recycle bin files:

$ trash-list
2008-06-01 10:30:48 /home/andrea/bar
2008-06-02 21:50:41 /home/andrea/bar
2008-06-23 21:50:49 /home/andrea/foo

Search for recycle bin files:

$ trash-list | grep foo
2007-08-30 12:36:00 /home/andrea/foo
2007-08-30 12:39:41 /home/andrea/foo

Recover recycle bin files:

$ trash-restore
0 2007-08-30 12:36:00 /home/andrea/foo
1 2007-08-30 12:39:41 /home/andrea/bar
2 2007-08-30 12:39:41 /home/andrea/bar2
3 2007-08-30 12:39:41 /home/andrea/foo2
4 2007-08-30 12:39:41 /home/andrea/foo
What file to restore [0..4]: 4
$ ls foo
foo

Delete all recycle bin files:

$ trash-empty

Delete the files recycled n days ago in the Recycle Bin:

$ trash-empty <days>

Example:

$ date
Tue Feb 19 20:26:52 CET 2008
$ trash-list
2008-02-19 20:11:34 /home/einar/today
2008-02-18 20:11:34 /home/einar/yesterday
2008-02-10 20:11:34 /home/einar/last_week
$ trash-empty 7
$ trash-list
2008-02-19 20:11:34 /home/einar/today
2008-02-18 20:11:34 /home/einar/yesterday
$ trash-empty 1
$ trash-list
2008-02-19 20:11:34 /home/einar/today

Delete only files that match a pattern:

$ trash-rm \*.o

Note: use double quotation marks around the pattern to avoid shell expansion.

Regularly empty the recycle bin, which can be realized by crontab

The following command empties the contents of the recycle bin 3 days ago at 0 o'clock every day

0 0 * * * trash-empty 3

{/card-list-item}

{card-list-item}

common problem

{collapse item label = "how do I create a top-level. Trash directory?" open} Steps:

sudo mkdir --parent /.Trash
sudo chmod a+rw /.Trash
sudo chmod +t /.Trash

{/collapse-item} {collapse item label = "can I set the alias of rm to trash put?"} Yes, but it shouldn't. I thought it was a good idea before, but now I don't think so. Although the trash put interface looks compatible with rm, they have different syntax, which can lead to some problems. For example, when rm is used to delete a directory, - R is required, but trash put is not required. {/collapse-item} {collapse item label = "but sometimes I forget to use trash put. Can't I really set an alias for rm?" open} You can set an alias for rm to remind you not to use it:

alias rm='echo "This is not the command you are looking for."; false'

If you really want to use rm, add a slash before rm to cancel the alias:

\rm file-without-hope

Note that Bash alias is only effective in the interactive interface, so using this alias will not affect the script using rm. {/collapse-item} {collapse item label = "where are the files moved to the recycle bin?" open} Files moved from the home partition to the recycle bin are here:

~/.local/share/Trash/

{/collapse-item}

{/card-list-item}

{card-list-item}

An error occurred

An error occurred while installing CentOS7 in vest

Processing /root/trash-cli
Collecting psutil (from trash-cli==0.21.7.24)
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/e1/b0/7276de53321c12981717490516b7e612364f2cb372ee8901bd4a66a000d7/psutil-5.8.0.tar.gz
    Complete output from command python setup.py egg_info:
    /usr/lib64/python2.7/distutils/dist.py:267: UserWarning: Unknown distribution option: 'python_requires'
      warnings.warn(msg)
    /usr/lib64/python2.7/distutils/dist.py:267: UserWarning: Unknown distribution option: 'long_description_content_type'
      warnings.warn(msg)
    error in psutil setup command: 'extras_require' must be a dictionary whose values are strings or lists of strings containing valid project/version requirement specifiers.
    
    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-n6gexX/psutil/
You are using pip version 8.1.2, however version 21.2.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

Later, it was found that there was a problem with the psutil version. Just install the old version!

pip install psutil==5.7.0

{/card-list-item} {card-list-item}

Final description

Of course, there are many other solutions. For example, use alias!

alias rm='rm -i'

The following is the alias I currently use. I hope it will be helpful to you!

alias rm='echo "This is not the command you are looking for."; false'
alias ls='ls --show-control-chars -F --color=auto'
alias ll='ls -hl'
alias la='ls -a'
alias cp='cp -i'
alias mv='mv -i'
alias less='less -R'
alias cat='ccat'
alias diff='colordiff'
alias grep='grep --color=auto'
alias egrep='egrep --color=auto'

{/card-list-item}

This article is not original, mainly because it is too helpful to me. I share it with you. The original address is: If there is a happy enjoyment - rm -rf another tragedy, it's time to give Linux a whole recycle bin! Infringement contact the webmaster to delete, thank you????