TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Stop Using `rm` in Bash. Use `del`

35 pointsby raibosomeabout 6 years ago
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?

30 comments

floatingatollabout 6 years ago
While not an alternative, please bugfix your bash:<p>mv &quot;$@&quot; ~&#x2F;.Trash&#x2F;<p><a href="https:&#x2F;&#x2F;github.com&#x2F;koalaman&#x2F;shellcheck&#x2F;wiki&#x2F;SC2086" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;koalaman&#x2F;shellcheck&#x2F;wiki&#x2F;SC2086</a>
评论 #19413847 未加载
评论 #19414634 未加载
评论 #19414497 未加载
WCSTombsabout 6 years ago
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&#x27;s<p><pre><code> trash-put my_file.txt </code></pre> [1] <a href="https:&#x2F;&#x2F;github.com&#x2F;andreafrancia&#x2F;trash-cli" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;andreafrancia&#x2F;trash-cli</a>
评论 #19414176 未加载
everdriveabout 6 years ago
I&#x27;ve never liked the idea of a trashcan, and usually shift-delete when I&#x27;m in the gui. Deleting shouldn&#x27;t be done casually, but a ui shouldn&#x27;t second guess a user any more than it has to.
评论 #19413918 未加载
评论 #19415397 未加载
cafabout 6 years ago
This is going to cause you pain if the tree you&#x27;re &quot;deleting&quot; is on a different filesystem to ~ and too big...
surfsvammelabout 6 years ago
rm is for removing stuff. If you don’t want to remove the file, use mv.<p>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.<p>With great powers comes great responsibility.
评论 #19413858 未加载
ameliusabout 6 years ago
Or use a snapshotting filesystem, so you can simply go back in time if you mess up.
评论 #19414386 未加载
sexyflandersabout 6 years ago
My “alternative” to rm is to use mv directly. I just mv files aside whenever I am not 100% confident they are safe&#x2F;ready to delete permanently.<p>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.
评论 #19414851 未加载
skyriserabout 6 years ago
I like using the ‘trash’ command through Homebrew, on macOS Terminal:<p><a href="http:&#x2F;&#x2F;hasseg.org&#x2F;trash&#x2F;" rel="nofollow">http:&#x2F;&#x2F;hasseg.org&#x2F;trash&#x2F;</a>
devnonymousabout 6 years ago
&gt; What&#x27;s your alternative?<p>When I execute rm I mean to delete. When I&#x27;m not sure I expyre[1] the path: <a href="https:&#x2F;&#x2F;github.com&#x2F;lonetwin&#x2F;expyre" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;lonetwin&#x2F;expyre</a><p>[1] shameless plug -- but you did ask ;-)
phunehehe0about 6 years ago
I too have my share of reinventing the trash functionality :)<p>garbage-io has some features that scratch my own itches:<p>- Trash files with the same name without clashing - Delete things from trash directory base on deletion time and file size - Hide the deleted files&#x2F;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. ~&#x2F;Downloads<p>I do feel that a better approach would be something that works alongside the trash functionality instead of replacing it, though.<p><a href="https:&#x2F;&#x2F;gitlab.com&#x2F;phunehehe&#x2F;garbage-io" rel="nofollow">https:&#x2F;&#x2F;gitlab.com&#x2F;phunehehe&#x2F;garbage-io</a>
towaway1138about 6 years ago
My alternative is to think carefully before hitting return. As a bonus, this works for other commands, too.<p>Also, &#x27;\rm&#x27;, as many distributions insist on aliasing &#x27;rm&#x27; by default.
alexandernstabout 6 years ago
How do you manage your &quot;.Trash&quot; folder? Who is in charge of deleting it&#x27;s content? Do you do it periodically or based on some other event?
emondiabout 6 years ago
I move to &#x2F;tmp and it gets deleted on next reboot.
评论 #19414202 未加载
clircleabout 6 years ago
If you spend most of your time in emacs, and like to delete&#x2F;move files in dired, you can use<p>(setq delete-by-moving-to-trash t)<p>to achieve the same effect.
Volundrabout 6 years ago
nilfs2 is my alternative. It&#x27;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&#x27;t? Just find an earlier checkpoint and your good to go.<p>Backups too. Nothing replaces backups.
JohnFenabout 6 years ago
I dislike the trash bin enough that I intentionally disable it, so personally, I&#x27;m perfectly happy with using rm.
mykowebhnabout 6 years ago
This can be a pain if you&#x27;re deleting many files, but<p><pre><code> alias rm=&#x27;rm -i&#x27; </code></pre> has worked for me.
评论 #19418159 未加载
Davieyabout 6 years ago
I already have an alternative for Windows users. Doesn&#x27;t provide any data retention mechanism, but still helpful:<p><pre><code> $ ls -l &#x2F;usr&#x2F;local&#x2F;bin&#x2F;del lrwxrwxrwx 1 root root 13 Mar 18 12:46 &#x2F;usr&#x2F;local&#x2F;bin&#x2F;del -&gt; &#x2F;usr&#x2F;games&#x2F;sl</code></pre>
moleculeabout 6 years ago
Or “How I Filled Up &#x2F;home By ‘Deleting’ A Larger Amount Of Data From A Larger Volume”
goshxabout 6 years ago
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.<p>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.
saagarjhaabout 6 years ago
Time Machine.
评论 #19414227 未加载
raibosomeabout 6 years ago
As pointed out, the alias should be<p><pre><code> function del() { mv “$@“ ~&#x2F;.Trash } </code></pre> instead. Thanks @floatingatoll!
评论 #19414768 未加载
_nosajabout 6 years ago
I&#x27;ve used a tool called &#x27;trash&#x27; on OSX for a few years:<p>`brew install trash`
ivan_ahabout 6 years ago
What would be the fish shell equivalent alias&#x2F;function?
pipogldabout 6 years ago
I have backups..
评论 #19414238 未加载
cleanyourroomabout 6 years ago
`alias rm=&#x27;rm -rf &#x2F;&#x27;`
karmakazeabout 6 years ago
Use `git`, use `rm`.
Upvoter33about 6 years ago
Dropbox
fxfanabout 6 years ago
I wish something like this were built into kernel filesystem driver. All deletions automatically go to this specific place in the filesystem.
评论 #19413898 未加载
评论 #19414861 未加载
评论 #19413877 未加载
ameliusabout 6 years ago
This is another example that shows that Bash (and similar shells) should not be used for scripting.<p>It&#x27;s great for the command line, but for scripting there are much better, cleaner, faster and safer alternatives.
评论 #19414141 未加载
评论 #19414041 未加载
评论 #19414028 未加载
评论 #19414012 未加载