Thanks to visit codestin.com
Credit goes to github.com

Skip to content
/ rm-safely Public

'rm' safely – a safety net for rm

License

zdk/rm-safely

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

49 Commits
 
 
 
 
 
 
 
 
 
 

rm-safely

rm safely.

rm-safely is a safety shell alias to prevent the accidental deletion of important files, especially, default MacOS rm

This is just a handy shell wrapping function along with option to directly invoke /bin/rm with --rm.

If you always use /bin/rm -i or alias it already, you will probably be fine.

But, with this rm-safely alias, it should prevent you in case of autocomplete from shell history that left of with unintended rm -rf.

Keys,

  • Written in shell script, no dependencies.
  • Save files in ~/.local/share/Trash and in /.Trash-$(id-u) for mounted volumes.
  • Tested on:
    • Shell
      • zsh
    • OS:
      • macOS 15.5+
      • Linux arch-linux 6.17.1-2-cachyos

Demo

demo.gif

Install

Via curl:

curl -fsSL https://raw.githubusercontent.com/zdk/rm-safely/main/rm-safely | bash -s install

Via homebrew:

brew install zdk/tools/rm-safely

Usage

Use rm command to delete things as usual, but safer \(◎o◎)/

In other words when you run rm -rf file directory/

You will have files/dir the trash first, then you can decide to delete or clean them later on.

Additional features

rm -rm                      Skip trash!, really execute 'rm'
rm -list-trash, -l          Show trash contents from all filesystems
rm -restore <hash>, -s      Restore a file from trash using its hash
rm -undo, -u                Restore the last deleted files
rm -empty-trash             Empty all trash directories
rm -show-trash-path, -p     Display all trash directory paths
rm -version                 Show version information
rm -help                    Show this help

Really Remove

If you don't really care to move it to Trash first.

rm-safely provide bypass to your OS rm via --rm option.

That means, use rm --rm

For examples:

  • remove files and directy with OS rm.

rm --rm file directory/

  • see the OS rm --help
rm --rm --help
/bin/rm: illegal option -- -
usage: rm [-f | -i] [-dIPRrvWx] file ...
       unlink [--] file

--rm is nothing special other than execute /bin/rm from current shell.

Or, you could just /bin/rm directly.

Uninstall

curl -fsSL https://raw.githubusercontent.com/zdk/rm-safely/main/rm-safely | bash -s uninstall

Notes

[Important Reminder],

Regarding the normal bahaviour of unix alias,

  • Please keep in mind, the rm-safely alias is available in current user only.

And, you have a few options to make the alias available in another user like so:

  1. Make sure you install in each user you need to have alias available. (Either manually or dotfiles should work)

  2. Always use sudo -s to switch to root or another, then run rm in the next step as a good habit anyway.