
Stop Using `rm` in Bash. Use `del` - raibosome
We’ve all had our fair share of accidentally removing files. Instead of<p><pre><code>  rm my_file.txt
</code></pre>
or<p><pre><code>  rm -rf my_folder
</code></pre>
we do<p><pre><code>  del my_file.txt
</code></pre>
or<p><pre><code>  del my_folder
</code></pre>
where you can define your Bash alias for `del` in ~&#x2F;.custom_aliases as<p><pre><code>  function del() {
      mv $* ~&#x2F;.Trash
  }
</code></pre>
which simply moves your files&#x2F;folders to the trash such that you can recover them easily later.<p>What’s your alternative?
======
floatingatoll
While not an alternative, please bugfix your bash:

mv "$@" ~/.Trash/

[https://github.com/koalaman/shellcheck/wiki/SC2086](https://github.com/koalaman/shellcheck/wiki/SC2086)

~~~
dan-robertson
Even simpler:

    
    
      alias del="mv -t ~/.Trash/"

~~~
chipironcin
Now let's delete them after roughtly 30 days:

    
    
      alias del="mv -t ~/.Trash/ ; find ~/.Trash/ -mtime +30 -exec rm {} \;"

~~~
majewsky
This does not work. The arguments to `del` get appended at the end, after
`find`. You want this:

    
    
      del() { mv "$@" ~/.Trash/ && find ~/.Trash -mtime +30 -delete }
    

I also replaced the unnecessary exec by find's builtin -delete

~~~
futureastronaut
Thank you, it's painful to see people providing replacements for critical
commands like `rm` without understanding basic shell features like aliases.

And just a reminder everybody please don't run random scripts you find on the
internet.

P.S. you need a semicolon before the close brace :)

------
WCSTombs
For Linux users: also look into the trash-cli [1] package, which complies with
the FreeDesktop.org trash specification and remembers the original path, etc.
so that trash items can be restored even with filename collisions. Then it's

    
    
        trash-put my_file.txt
    

[1] [https://github.com/andreafrancia/trash-
cli](https://github.com/andreafrancia/trash-cli)

~~~
pizza234
Until at least an year ago, the Ubuntu version (and likely, the upstream on),
wouldn't work on certain filesystems.

I'm not sure about the current state, but one needs to test is very carefully
before using it.

~~~
floatingatoll
Which filesystem were you using when you encountered trouble with it? What
happened when you tried to use it? Did it warn you about the filesystem issue?
Did it delete rather than trash?

~~~
pizza234
I think the problem was with Btrfs subvolumes. There are old bugs filed on
Ubuntu, not sure if they're current, e.g.
[https://bugs.launchpad.net/ubuntu/+source/nautilus/+bug/1442...](https://bugs.launchpad.net/ubuntu/+source/nautilus/+bug/1442649).

------
everdrive
I've never liked the idea of a trashcan, and usually shift-delete when I'm in
the gui. Deleting shouldn't be done casually, but a ui shouldn't second guess
a user any more than it has to.

~~~
taneq
I habitually shift+delete too, but I won't say I've never made a mistake.

It's much less hazardous doing this when everything is in source control,
though. :)

------
caf
This is going to cause you pain if the tree you're "deleting" is on a
different filesystem to ~ and too big...

------
surfsvammel
rm is for removing stuff. If you don’t want to remove the file, use mv.

Take care when entering commands, if it’s important that you don’t mess up,
spend an extra second. Don’t run around shooting rm -rf as super user.

With great powers comes great responsibility.

~~~
joelthelion
Would be nice if filesystems were garbage collected, though. I don't see a
fundamental reason why rm couldn't be undoable the second after you issue it.

~~~
tyfon
Most have SSD drives now that trims the space on delete. Not sure how easy it
is to mark regions on the SSD for "undelete" when it has been trimmed. In some
cases the deleted region of the drive may even be mapped to another place on
the memory chip itself to cycle it.

~~~
ac29
On Linux at least, it's not recommend to run continuous TRIM (aka "discard").
Instead, you can run fstrim on a timer to clean things up periodically. Most
SSD's can actually do either, but discard is recommend against for NVMe
drives, and many SSD's have bugs, so overall periodic trim is the way to go.

------
amelius
Or use a snapshotting filesystem, so you can simply go back in time if you
mess up.

~~~
zipperhead
I use borg backup running via cron multiple passes per day. When needed you
can mount any backup as a file system for easy restoration. Yes, short-lived
files can't be recovered, but that's something I am willing to live with.

------
sexyflanders
My “alternative” to rm is to use mv directly. I just mv files aside whenever I
am not 100% confident they are safe/ready to delete permanently.

Personally I’m not a fan of shell environment customizations like this because
they are often fragile and may vary between systems. I like to know exactly
what commands I’m executing.

~~~
JohnFen
> Personally I’m not a fan of shell environment customizations like this
> because they are often fragile and may vary between systems.

True. I gave up the practice entirely many years ago for a slightly different
reason -- I need to use many different systems on a regular basis, and don't
want to get used to using something custom that won't exist on random system X
without customizing it, too.

Often, I prefer predictability and stability over convenience.

------
skyriser
I like using the ‘trash’ command through Homebrew, on macOS Terminal:

[http://hasseg.org/trash/](http://hasseg.org/trash/)

------
fxfan
I wish something like this were built into kernel filesystem driver. All
deletions automatically go to this specific place in the filesystem.

~~~
majewsky
This would be fun as an experiment to see how quickly it fills up the drive,
but probably utterly useless in production. A lot of stuff is creating and
deleting files all the time, e.g. logrotate.

~~~
lixtra
It could be fifo and you would still be able to undo damage for some time.

------
devnonymous
> What's your alternative?

When I execute rm I mean to delete. When I'm not sure I expyre[1] the path:
[https://github.com/lonetwin/expyre](https://github.com/lonetwin/expyre)

[1] shameless plug -- but you did ask ;-)

------
phunehehe0
I too have my share of reinventing the trash functionality :)

garbage-io has some features that scratch my own itches:

\- Trash files with the same name without clashing \- Delete things from trash
directory base on deletion time and file size \- Hide the deleted
files/directories before moving them, in case moving them takes a long time (I
later think this was a bit over the top...) \- The smart deletion can also be
used for e.g. ~/Downloads

I do feel that a better approach would be something that works alongside the
trash functionality instead of replacing it, though.

[https://gitlab.com/phunehehe/garbage-
io](https://gitlab.com/phunehehe/garbage-io)

------
towaway1138
My alternative is to think carefully before hitting return. As a bonus, this
works for other commands, too.

Also, '\rm', as many distributions insist on aliasing 'rm' by default.

------
alexandernst
How do you manage your ".Trash" folder? Who is in charge of deleting it's
content? Do you do it periodically or based on some other event?

------
emondi
I move to /tmp and it gets deleted on next reboot.

~~~
floatingatoll
/tmp is a ramdisk on some systems, which makes this particular approach a very
risky endeavor to use as a global replacement for ‘rm’ as the OP suggests.

~~~
emondi
Sure, also it works for me because it is on a system that is frequently turned
off. If it would have to stay on for a long time it would fill /tmp with
garbage.

~~~
floatingatoll
I meant to warn others that, for example, “del foo.iso” could fail due to OOM.
As long as they understand that, your use case will work fine for them.

------
clircle
If you spend most of your time in emacs, and like to delete/move files in
dired, you can use

(setq delete-by-moving-to-trash t)

to achieve the same effect.

------
JohnFen
I dislike the trash bin enough that I intentionally disable it, so personally,
I'm perfectly happy with using rm.

------
Volundr
nilfs2 is my alternative. It's a log-based filesystem that results in
continuous checkpoints that can be upgraded to mountable snapshots at any
time. Realized you blew away a file you needed? Made large modifications you
wish you hadn't? Just find an earlier checkpoint and your good to go.

Backups too. Nothing replaces backups.

------
Daviey
I already have an alternative for Windows users. Doesn't provide any data
retention mechanism, but still helpful:

    
    
      $ ls -l /usr/local/bin/del
      lrwxrwxrwx 1 root root 13 Mar 18 12:46 /usr/local/bin/del -> /usr/games/sl

------
mykowebhn
This can be a pain if you're deleting many files, but

    
    
      alias rm='rm -i'
    

has worked for me.

~~~
raibosome
Okay I think this is more elegant than my workaround. But it doesn’t work for
directories, does it?

~~~
mykowebhn
BTW, if I'm absolutely sure I want to delete all the files in a directory and
all subdirectories, and I don't want to bother with a gazillion prompts, I can
override the alias with a backslash, as in "\rm -r *". Use with extreme
caution, though. Otherwise, your solution of putting into Trash is probably
better and safer.

------
molecule
Or “How I Filled Up /home By ‘Deleting’ A Larger Amount Of Data From A Larger
Volume”

------
goshx
This was one of the first things I realized I had to do back when I installed
a Red Hat 3.0 for the first time.

My alias was for rm itself. I replaced it completely, as the habit of using rm
is hard to forget. Today I don’t use an alias anymore.

------
saagarjha
Time Machine.

~~~
floatingatoll
For the unaware, Time Machine is the macOS built-in backup system. Once
configured, it takes frequent filesystem snapshots and syncs them to a
connected storage device. If the file being removed is included by those
backups, and existed long enough to be in a snapshot, then you can restore it
from the TM backup. Neither of those criteria are guaranteed, as when working
with files in a Terminal or bash script you can easily create and remove files
with names and/or locations that exempt them.

------
raibosome
As pointed out, the alias should be

    
    
      function del() {
        mv “$@“ ~/.Trash
      }
    

instead. Thanks @floatingatoll!

~~~
banana_giraffe
And if you copy and paste that version, it won't work either.

Also, be aware this will silently delete older files with name collisions.

------
_nosaj
I've used a tool called 'trash' on OSX for a few years:

`brew install trash`

------
ivan_ah
What would be the fish shell equivalent alias/function?

------
pipogld
I have backups..

~~~
floatingatoll
Either your backups run continuously on every inode change (see nilfs
elsethread), or there’s a window of risk where you create a file and then
remove it before your backups run.

------
cleanyourroom
`alias rm='rm -rf /'`

------
karmakaze
Use `git`, use `rm`.

------
Upvoter33
Dropbox

------
amelius
This is another example that shows that Bash (and similar shells) should not
be used for scripting.

It's great for the command line, but for scripting there are much better,
cleaner, faster and safer alternatives.

~~~
avar
There's plenty of good reasons not to use shellscript, but someone not knowing
the difference between "$@" and $* isn't one of them. This is prominently
documented in Bash's documentation:
[https://www.gnu.org/software/bash/manual/html_node/Special-P...](https://www.gnu.org/software/bash/manual/html_node/Special-
Parameters.html)

~~~
ashelmire
Nothing about that is especially prominent (its just regular documentation),
and then you still need to consider how any given utility will interact with
one word vs multiple words.

