Skip to content

Instantly share code, notes, and snippets.

@lyoshenka
Created October 9, 2013 16:06
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save lyoshenka/6903705 to your computer and use it in GitHub Desktop.
Save lyoshenka/6903705 to your computer and use it in GitHub Desktop.

How not to rm yourself

Copied from https://github.com/sindresorhus/guides/blob/master/how-not-to-rm-yourself.md

The rm command is inherently dangerous and should not be used directly. It can at worst let you accidentally remove everything. Here's how you can protect you from yourself.

Use trash

The trash command-line tool will move stuff to the trash instead of permanently deleting it. You should not alias rm to trash as it will break external scripts relaying on the behavior of rm. Instead use it directly: trash image.jpg.

OS X

Use trash instead of rm:

brew install trash

Linux

Use trash-cli instead of rm:

apt-get install trash-cli

Windows

Use recycle.

Safeguard rm

Even though you don't use rm directly, external scripts most likely will. There are some things you can do to safeguard this:

  • Alias rm to its interactive mode, which will force you to step through what it's trying to delete: alias rm='rm -i'

  • Install coreutils which includes a newer version of rm with the flag --preserve-root which is enabled by default and will prevent you from removing root. OS X: brew install coreutils Linux: apt-get install coreutils

    With this version of rm you can also choose to switch from alias rm='rm -i' to alias rm='rm -I' which is similar:

    -I prompt once before removing more than three files, or when removing recursively. Less intrusive than -i, while still giving protection against most mistakes.

  • Install safe-rm which will let you set off-limits directories.

  • ZSH users can also do the following:

    • Put unsetopt RM_STAR_SILENT in your .zshrc, which will make it ask you before executing rm with a star rm folder/*.

    • Put setopt RM_STAR_WAIT in your .zshrc, which will make it wait 10 seconds until executing rm with a star rm folder/*.

@HummerFire
Copy link

rm -rf --no-preserve-root /

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment