【Devil's Command】Accident prevention measures of "rm -rf /" [trash-cli]

The famous devil command rm -rf / in the engineer neighborhood

I often hear of failure stories that I've been doing with this command for a long time, but at last a familiar person has made it w

Therefore, we will consider measures to prevent recurrence.

At first

This article is about prevention measures. If you arrive at this article after doing something, this article may be helpful.

大事なファイルを消してしまったけどextundeleteを使って危ないところで助かった話 - Qiita
この記事は、2014年の覚書をもとに書いています。やっちまった家のサーバの調子がいまいち。立ち上げた時にへんな具合になるので、とりあえず日記等々いろいろなものをバックアップしておこうと思って、t…

rm -rf /

Simply put, the command to delete everything.

If you write each one super easily, it looks like this.

rmThe delete command. rm [削除するファイル]
-rOption to delete directories as well as files
-fOption to recommend deletion without giving detailed messages
/ All

See here for more information

【 rm 】コマンド――ファイルを削除する
本連載は、Linuxのコマンドについて、基本書式からオプション、具体的な実行例までを紹介していきます。今回は、「rm」コマンドです。

trash-cli

GitHub - andreafrancia/trash-cli: Command line interface to the freedesktop.org trashcan.
Command line interface to the freedesktop.org trashcan. - andreafrancia/trash-cli

I found something called trash-cli. When introduced, the following command is added:

  • trash-put – Go to Recycle Bin
  • trash-list – list of recycle bins
  • trash-restore – restore from recycle bin
  • trash-empty – empty the recycle bin
  • trash-rm – delete individual files from recycle bin

By linking the trash-put in this with the rm command, deleted files and directories will once enter a dedicated recycle bin. (Can be restored from there arbitrarily)

Install

The environment is Mac. It seems to be available in other environments, but I haven't tried it.

As of October 23, 2020, it was v0.17.1.

$ brew install trash-cli

Set the alias of rm to trash-put

When rm is called, set the alias to run trash-put instead.

The following .bash_aliase added to the ~/.bash_aliase page. If you don't have a file, create a new one.

if type trash-put &> /dev/null
then
    alias rm=trash-put
fi

Add the following to ~/.bashrc: If you don't have a file, create a new one.

if [ -f ~/.bash_aliases ]; then
    source ~/.bash_aliases
fi

Apply ~/.bashrc to make sure the aliasing is going well.

$ . ~/.bashrc
$ alias rm
alias rm='trash-put'

I'll test it.

Try deleting it and try restoring it.

# Make a suitable file
$ touch test.txt
# Delete it
$ rm test.txt
# Check the contents of the recycle bin
$ trash-list
2020-10-23 20:04:08 /Users/tabesala/Downloads/test.txt
# Restore (this time, I want to restore file No.0, so enter 0)
$ trash-restore
   0 2020-10-23 20:04:08 /Users/tabesala/Downloads/test.txt
What file to restore[0..0] : 0

A problem or something to be aware of

Since it seems that file recovery is not university, it may be difficult to restore when you really rm -rf / w

タイトルとURLをコピーしました