Thursday, May 24, 2018

Stop using the built-in rm command

Using *nix command line interface can be dangerous, especially if you aren't careful. For example the command rm -rf * can wipe everything. Today I was careless and accidentally deleted some of my files in the Desktop directory. "With great power comes great responsibility" - Lesson learned.

My first train of thought was how do I prevent this mistake in the future? Override the default rm command to move the files in the trashcan instead of permanently removing the files.

Here is the code I came up with:

How to use:

  1. Put the code in your ~/.bash_profile or ~/.bash_rc` (if those files do not exist create one)
  2. Type source ~/.bash_profile or source ~/.bash_rc to reload the file
Now whenever you run rm it will move to the trash application

Backstory: I needed to erase all the files in the ~/Library/Developer/Xcode/DerivedData directory because Xcode was caching old provisioning profiles. As soon as I changed the directory, the virtual memory was running out so space so the terminal application restarted. On restart, the application launched into the ~/Desktop directory. Without much thought I typed rm -rf * then BAM!