I use the terminal on a daily basis, I will often use rm DirectoryName. Sometimes I get a little impatient and do rm * without deliberating if it’s safe. As you can imagine, these shenanigans have sometimes come back and hit me in the face. Does anyone know how I create a mapping so rm whatever moves everything to the trashcan which I then delete manually.
8 Answers
Bad Idea
Using rm to move files to the trash is a dopamine hit. It is common and pleasuring but can be bad for you taken out of context.
You really need control yourself when using rm. Especially if your backups are not current or you don’t have the time to do an erase / install / restore.
Don't use rm
Imagine, you get used to rm moving to trash and make a habit of it. Sure, your system is safe but when you log into a friend's (or your wife's or your boss') notebook and have to delete something? You'll be actually using the real rm - deleting those files forever. Without lots of prep and qualifiers, it's a bad habit, and you need to know that.
So instead, install trash and make a habit of using it:
# Install `trash`, e.g. using `brew` $ brew install trash # Example usage $ touch foo.txt $ trash foo.txt # `trash -l` prints the contents of the Trash $ trash -l | grep foo.txt /Users/yourname/.Trash/foo.txt Correcting bad habits
Here’s where the personal advice starts - changing one’s behavior is hard. To help yourself re-train your habit so that you use trash instead of rm is alias it to a custom message in your .profile.
alias rm="echo Use 'del', or the full path i.e. '/bin/rm'" So, whenever you use rm, you'll be prompted to either use trash or /bin/rm. Remember, this is only temporary, after a while you should remove the alias.
Never ever, do something like this:
$ pwd / $ cd /tnp; rm -rf * sh: cd: /tnp: No such file or directory Pop Quiz: In what directory will the rmcommand run? :)
- 26
&&is your lifeline.;is the devil. P.S. The answer is that pretty much everything on your system disappears, in the above example.Jason Salaz– Jason Salaz2011-07-20 08:06:28 +00:00Commented Jul 20, 2011 at 8:06 - 2Debian/Ubuntu users install
trash-cliinstead, ex.aptitude install trash-cli- see github.com/andreafrancia/trash-cliDaniel Sokolowski– Daniel Sokolowski2016-02-18 01:44:31 +00:00Commented Feb 18, 2016 at 1:44 - 6@jm666 Also check out trash (blog post & source). It asks Finder for trashing files. Moreover it can also use standard Objective C filesystem APIs (faster) and fallback to Finder if insufficient rights.
brew install trashParth– Parth2017-07-05 05:29:32 +00:00Commented Jul 5, 2017 at 5:29 - 4
rmtrashis really old, having been last updated in 2003, and it shows. It has many fewer options thanrm, and if you sayrmtrashon a secondary volume it moves those files to your user's trash folder on the system drive, which can take a long time for large files. Thetrashoption recommended by parth is better, solving the keep-on-same-volume problem, but still missing manyrmoptions.Warren Young– Warren Young2018-01-28 01:58:11 +00:00Commented Jan 28, 2018 at 1:58 - 12For those on macOS:
rmtrashis no longer to be found on homebrew, so I recommend installingtrashinstead:brew install trash.Niek– Niek2020-11-23 10:38:12 +00:00Commented Nov 23, 2020 at 10:38
I can think of a dozen ways to code this, but categorically refuse to type any of them up in an answer. I strongly recommend you curb your habit of getting a little crazy. You shouldn't be in the habit of using rm at all if you can't use it properly.
The fundamental problem here is that even if you install a safety net on your own system, that will only help you keep your bad habits and when you happen to be on another machine some day that doesn't have the same custom protections, you will do something epicly bad.
The proper solution is to use rm as it was meant to be used including manually adding the -i argument whenever you are in doubt about how a glob will expand, and have good system backups that you can restore in the event of serious user error. Trying to add "trash" to commands that in the rest of the universe don't use it is a half-way step that is the worst of both worlds.
Either use the trash or don't. Using rm doesn't go to trash, it removes.
If you want to use the trash, there is nothing wrong with that. Just get in the habit of using the rmtrash command instead of rm. This will help your brain understand what's happening and not get you into a bad habit that will cause grief later.
- 1Your point doesn't compute in this instance. People delete things accidentally on a daily basis, system backups do not work in the case of a home pc (which mine is) as the resources are limited, a move to trash can is a far more efficient way of handling these deletions for my particular use case.steve– steve2011-07-21 11:11:04 +00:00Commented Jul 21, 2011 at 11:11
- 7@steve you missed the point entirely.
rmby definition removes a file from the disk. It doesn't send it to the trash. Teaching yourself to usermwhen you mean send to trash sets you up for disaster. If you want to send to trash, usermtrashor a program that was designed for the job, then you won't run into nightmares later. I wasn't saying using the trash is inherently wrong, but if you want to use it, don't wire it up to something that was not intended to use it!Caleb– Caleb2011-07-21 11:13:26 +00:00Commented Jul 21, 2011 at 11:13 - 2
brew install rmtrashand stop usingrmGabLeRoux– GabLeRoux2014-02-05 20:16:40 +00:00Commented Feb 5, 2014 at 20:16 - This is an awesome answer, cleared up a few things for me. I'll try to form a habit of using
rmtrashinstead ofrm.Sheharyar– Sheharyar2015-02-16 17:56:11 +00:00Commented Feb 16, 2015 at 17:56 - 1@Brian You are welcome to
alias rm=ulif you prefer to be pedantic. I understand what happens at the file system level and at least some of the physics of spinning rust (not so much some of the newer mediums), and know thatrmdoesn't scrub the bits. But for an end user with a habit of getting crazy withrm(remove) I think the correct answer is to tell them to stop using the remove command if they want a trash bucket to easily recover stuff out of.Caleb– Caleb2022-11-17 20:21:33 +00:00Commented Nov 17, 2022 at 20:21
As of macOS 14 Sonoma a native trash command exists
The trash command line tool allows moving items to the MacOS trash natively.
The man trash documentation from macOS 15 Sequoia for reference:
TRASH(8) System Manager's Manual TRASH(8) NAME trash – Moves files and directories to the user trash folder SYNOPSIS trash [-h] [--help] [-s] [-stopOnError] [-v] [--verbose] FILE [FILE...] DESCRIPTION The trash moves files and directories into the user trash folder. The options are as follows: -h | --help display usage information for the tool and exit -v | --verbose display more verbose status -s | --stopOnError exit with an error if any move to a trash folder fails EXAMPLES In a directory with a file named "Foo.txt" and a directory "Bar", trash Foo.txt Bar moves the file and directory into the user's trash folder, if it exists and permissions allow the items to be moved into the user's trash folder. HISTORY First appeared in macOS 14.0 macOS May 25, 2022 macOS Use a command line tool like rmtrash (which usually just moves the file(s) to ~/.Trash making sure that the file has a unique name) or a script that uses appscript to call the Finder to do the delete. The latter is slower but adds the ability to use the Put Back option fromn Finder to restore the file easily.
I used trash as it is in in MacPorts and Homebrew and rmtrash is now very old and unmaintained. However on reading the code changes when editing this note I noticed that it changed in 2017 from using the Finder by default to move by default (which is not what I wanted and is a major change). So I will look for another tool.
As noted in the comments it is not a good idea to make rm an alias best to remember to use the trash program you installed directly
But if you wanted to you could do
After installing rmtrash to say /usr/local/bin/rmtrash then you can create an alias for bash
alias rm='rmtrash' You can use \rm to call rm directly and ignore the alias
- 17If you do use a tool like
rmtrash, please do yourself a favor and use it by it's own name. Never alias things that are notrmtorm, it will some day bite you.Caleb– Caleb2011-07-18 09:45:30 +00:00Commented Jul 18, 2011 at 9:45 - 1
rmtrashisn't compatible withrm(doesn't support the same command line parameters like-r) so an alias really isn't useful in any casePerry– Perry2017-07-21 16:16:27 +00:00Commented Jul 21, 2017 at 16:16 - 2It's a bad idea to alias
rmtrashtorm. Not only they are not compatible, but also it'll enforce the bad habit and you'll accidentally usermin another system thinking you are only usingrmtrash.Fayaz– Fayaz2019-08-20 04:27:47 +00:00Commented Aug 20, 2019 at 4:27
Just want to add a helpful habit to get into for those that don't do this already. Before using rm for anything, do 'ls' on it first. That way you can see all the files that you intend to delete. Then instead of retyping, just pull previous 'ls' command up and swap out ls for rm. This habit has reduced a lot of anxiety when invoking rm.
There are ways to protect yourself from horrible outcomes from rm. However, I agree with the other commenters here: very few are any good.
Do not for a minute believe that changing the command environment will help you. For example, you will find people online everywhere recommend doing this sort of thing:
alias rm="rm -i" or
function rm() { echo >&2 'rm forbidden; use "/bin/rm"' } export -f rm This will not work. Period. There are loads of use cases where these little parlor tricks will fail you miserably.
The only robust way I have used to spare myself from the danger of rm is by setting up a regular backup process through cron over the directories where I work. Raw UNIX does it for you nicely, with great simplicity, using find in conjunction with -mtime to check for files that have been updated within or since a certain time.
Introduction (you can skip it)
For me rm -i would be enough, because it prompts you before deleting any file. However, some extra security provided by safe-rm isn't a bad idea at all.
A simple alias like rm=safe-rm -i could be enough, however rm -rf would not work, because -i is overriden by -f.
So, we have to make sure that -i always come after -f!
My solution (3 steps):
Install safe-rm:
brew install safe-rmYou can still work with
rm, but you have modify theRM_BINin the next stepEdit your
~/.profile(or bash_profile or whatever), and put the following code snippet, (which contain analiasand afunction):alias rm="rm_i" function rm_i(){ RM_BIN=safe-rm # you can replace it with regular rm if you like args="" files="" argsDone=0 # to make sure arguments are before the files for var in "$@" do if [[ $var == \-* ]] ; then if [ $argsDone -eq 1 ] ; then $RM_BIN # just to show the usage of rm return fi args+=" $var" else argsDone=1 files+=" $var" fi done args+=" -i" # Put -i at the end (so rm -rf will not ignore it) $RM_BIN $args $files }Finally,
logoutandlogin, so the updates in the .profile are applied. DON'T use it before a logout/login!
Notes:
- It can be applied to any Linux distro, with slight modifications.
- It does NOT move your staff to thrash. It just asks you before deleting each file!
- It works with
-rfand with regexes. - If you really want to use
rm -rfWITHOUT being asked for every single file/folder you can still use/bin/rm -rf(orsafe-rm -rfif you had installed it). - No more unintentional
rm -rforrm *;)
- 8Aliasing rm to anything larger than itself (e.g.
alias rm='sudo rm') is like strapping a Uzi to a kitchen knife: someday you will get more than you bargained for when you go to cut that pineapple. Aliasing rm to anything smaller than itself (e.g.alias rm='rmtrash') is like letting your children play with disarmed hand grenades and not teaching them what they actually are: someday when they encounter a real one they won't treat it with the proper caution. Either way—aliasing rm is bad for sys admins as lack of hygiene is to personal safety and public health!Caleb– Caleb2014-06-27 05:22:48 +00:00Commented Jun 27, 2014 at 5:22 - 2Using something like safe-rm is reasonable advice. Aliasing it to rm is not. Use it under its own name or a unique alias so you don't take for granted a safety net that will not always be there.Caleb– Caleb2014-06-27 05:28:51 +00:00Commented Jun 27, 2014 at 5:28
- I see your point. BUT, the reason I did this, is that I work with many terminals.. some remote terminals, and some locals. And I want to throw lot of grenades on the remote terminals, but when I accidentally pull the safety seal on the local terminal, I want to see a warning before the explosion occurs.Paschalis– Paschalis2014-06-27 13:04:40 +00:00Commented Jun 27, 2014 at 13:04
- @Paschalis and what happens if a script or installed program needs to use
rm? Also, Caleb's point is even more valid -- if you get used to a system with safe-rm aliased, then you might get reckless with it and eventually use it on a system with an unaliased rm.Manishearth– Manishearth2014-07-03 14:03:23 +00:00Commented Jul 3, 2014 at 14:03 - Also be aware that there are several different incarnations of safe-rm. A ZSH plugin to move files to trash, a Perl script to blacklist important folders etc and a Rust version of the same.AndrewC– AndrewC2021-07-02 10:53:54 +00:00Commented Jul 2, 2021 at 10:53
Mac bigSur
brew install trash alias rm='trash' which rm touch a.md rm a.md ls ~/.Trash - Why does this only work on Big Sur?2021-08-26 06:19:11 +00:00Commented Aug 26, 2021 at 6:19
mv /file/to/delete ~./Trashalias rm=/bin/rm -ishould get you back to safety. Also - which Trash directory do you want rm to use? It gets clunky since Mac OS X has a trash directory for each volume as well as a user level trash can.rm *orrm /path/*