If you've ever accidentally removed files or folders with "rm", you know that your options for recovering them are extremely limited. In order to avoid that, you can set an alias so that when you use the "rm" command, it sends the files or folders concerned into your Trash, where you can easily recover them if needed.
**Note** You don't have to create an alias. You can install Trash (see Step 1 below) and just run commands like so:
trash test.txt
At the end of this article, you can also read how to install Trasher if you have Rust installed.

Follow these steps to have "rm" send things to your trash instead:
To make the rm command move files to the Trash instead of permanently deleting them, you can create a custom alias in your shell configuration file (such as .bashrc, .zshrc, etc.). Here's how you can do it:
Step 1: Install trash CLI
You can use the trash CLI, which allows you to move files to the Trash instead of permanently deleting them. It can be installed using npm (Node.js package manager).
Install Node.js and npm if you don't already have them:
brew install node
Install the trash-cli package globally using npm:
npm install --global trash-cli
Step 2: Create an Alias for rm
Add an alias to your shell configuration file to use the trash command instead of rm.
a. Open your shell configuration file in a text editor.
For example, if you use zsh:
nano ~/.zshrc
If you use bash:
nano ~/.bashrc
b. Add the following line to create an alias for rm:
alias rm='trash'
c. Save the file and exit the editor (e.g., press Ctrl + X, then Y, and then Enter in nano).
d. Apply the changes by sourcing the configuration file.
For zsh:
source ~/.zshrc
For bash:
source ~/.bashrc
e. Add a mechanism so either ~/.bashrc or ~/.zshrc are sourced when a new IDE or Terminal instance is created.
For bash:
Ensure that the following lines are present at the top of your .bashrc to check for an interactive shell:
# If not running interactively, don't do anything
[[ $- != *i* ]] && return
Zsh typically sources .zshrc for interactive shells without needing any additional checks, but you can add a similar check if you like:
# If not running interactively, don't do anything
[[ -o interactive ]] || return
For IDEs, you may need to configure them to ensure they source the appropriate file when they start a terminal session. This can usually be done in the settings or preferences of the IDE. For example, in Visual Studio Code, you can specify the shell that the integrated terminal uses:
- Open the Command Palette (Ctrl+Shift+P on Windows/Linux, Cmd+Shift+P on macOS).
- Type "Select Default Shell."
- Choose either bash or zsh from the list.
If your IDE does not source .bashrc or .zshrc by default, you might need to add a command to the terminal settings within the IDE to source the file explicitly. For example:
source ~/.bashrc
or
source ~/.zshrc
Step 3: Using the Alias
Now, whenever you use the rm command, it will move the files to the Trash instead of permanently deleting them. For example:
rm filename.txt
This command will now move filename.txt to the Trash.
Step 4. Verification
You can verify that the alias is working correctly by using the type command:
type rm
This should output something like:
rm is aliased to `trash`
With this setup, you can safely use the rm command, knowing that files will be moved to the Trash and can be retrieved if needed.
Another option, if you have Rust installed, is...
trasher - A small command-line utility to replace rm and del by a trash system
Follow install instructions:
Trasher — command-line utility in Rust // Lib.rs
It works by moving items to delete to a trash directory instead of deleting them immediately. As moving a single item is nearly instant (even when it's a large directory), while deleting items recursively can take quite a long time, Trasher is faster than rm, especially for large directories.
Read about trasher and more tools like it in my two articles:
Extremely Cool Command Line Tools - Supercharged with Rust - {Part 1}
Extremely Cool Command Line Tools - Supercharged with Rust - {Part 2}