7 ms·
Stop Using `rm` in Bash. Use `del`
We’ve all had our fair share of accidentally removing files. Instead of
rm my_file.txt
or
rm -rf my_folder
we do
del my_file.txt
or
del my_folder
where you can define your Bash alias for `del` in ~/.custom_aliases as
function del() {
mv $* ~/.Trash
}
which simply moves your files/folders to the trash such that you can recover them easily later.
What’s your alternative?
- molecule 8y agoOr “How I Filled Up /home By ‘Deleting’ A Larger Amount Of Data From A Larger Volume”
- fxfan 8y agoI wish something like this were built into kernel filesystem driver. All deletions automatically go to this specific place in the filesystem.
- majewsky 8y agoThis 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 8y agoIt could be fifo and you would still be able to undo damage for some time.
- devnonymous 8y ago...and then how do you delete it from that specific place when you are sure you want it gone? A new syscall? In seriousness tho, if you're ever wondering why such and such thing is not builtin into the kernel read up on separation of policy and mechanism eg: https://en.m.wikipedia.org/wiki/Separation_of_mechanism_and_policy https://en.m.wikipedia.org/wiki/Separation_of_mechanism_and_...
- JohnFen 8y agoI would hate that behavior, so if it's brought into Linux at the driver level (which I think is an inappropriate place for it -- the shell is the right place), it should be optional.
- WCSTombs 8y agoFor 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 8y agoUntil 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 8y agoWhich 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 8y agoI 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/1442649 https://bugs.launchpad.net/ubuntu/+source/nautilus/+bug/1442....
- floatingatoll 8y agoWhile 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 8y agoEven simpler: alias del="mv -t ~/.Trash/"
- chipironcin 8y agoNow let's delete them after roughtly 30 days: alias del="mv -t ~/.Trash/ ; find ~/.Trash/ -mtime +30 -exec rm {} \;"
- elcomet 8y agoThis is not fine, as deleting a small file can be very long if there's a large file to be deleted in the trash.
- ad_hominem 8y agoYeah running the delete in the user's crontab (i.e. in the background and on a schedule) would be a better option IMO.
- floatingatoll 8y agoThis fails on directories, as the -r switch was not included: mkdir foo; del foo and including it will spawn file not found errors for the contents of the foo directory, if any. EDIT: See also neighbor comment about appending filenames!
- majewsky 8y agoThis 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
- surfsvammel 8y agorm 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 8y agoWould 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.
- majewsky 8y agoIt usually is: http://extundelete.sourceforge.net http://extundelete.sourceforge.net
- tyfon 8y agoMost 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 8y agoOn 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.
- Volundr 8y agoCheck out nilfs, where this is explicit. Every filesystem action results in a checkpoint, and checkpoints are garbage collected only as-needed to maintain free space.
- everdrive 8y agoI'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 8y agoI 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. :)
- orev 8y agoThis fails in the GUI in cases such as if you accidentally hit navigation keys close to the Del key, like Home, End, etc. In those cases you may accidentally change the file selection in the same movement as the deletion, and then you whack the wrong file. If you’re the person in charge of making the delete operation the best and safest it can be, you do not have the luxury of yelling at users to just be more careful, because the data shows very plainly that doesn’t work (despite this being the approach most IT people seem to think is acceptable). In this role, with these constraints, the inevitable conclusion is the make a trash can.
- caf 8y agoThis is going to cause you pain if the tree you're "deleting" is on a different filesystem to ~ and too big...
- saagarjha 8y agoTime Machine.
- floatingatoll 8y agoFor 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.
- emondi 8y agoI move to /tmp and it gets deleted on next reboot.
- floatingatoll 8y ago/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 8y agoSure, 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 8y agoI 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.
- amelius 8y agoThis 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.
- mruts 8y agoLike what? Sh? Zsh?
- thjan 8y agoPlease elaborate :)
- avar 8y agoThere'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-Parameters.html https://www.gnu.org/software/bash/manual/html_node/Special-P...
- ashelmire 8y agoNothing 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.
- floatingatoll 8y agoWhat? No, come on. You can’t extrapolate “someone didn’t read the manual” to “don’t use shell scripting”.
- amelius 8y agoOr use a snapshotting filesystem, so you can simply go back in time if you mess up.
- zipperhead 8y agoI 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.
- clircle 8y agoIf 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.
- goshx 8y agoThis 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.
- devnonymous 8y ago> 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 ;-)
- skyriser 8y agoI like using the ‘trash’ command through Homebrew, on macOS Terminal: http://hasseg.org/trash/ http://hasseg.org/trash/
- pipogld 8y agoI have backups..
- floatingatoll 8y agoEither 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.
- Volundr 8y agonilfs2 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.
- deleted 8y ago[deleted]
- alexandernst 8y agoHow 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?
- sexyflanders 8y agoMy “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 8y ago> 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.
- towaway1138 8y agoMy 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.
- raibosome 8y agoAs pointed out, the alias should be function del() { mv “$@“ ~/.Trash } instead. Thanks @floatingatoll!
- banana_giraffe 8y agoAnd 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 8y agoI've used a tool called 'trash' on OSX for a few years: `brew install trash`
- JohnFen 8y agoI dislike the trash bin enough that I intentionally disable it, so personally, I'm perfectly happy with using rm.
- karmakaze 8y agoUse `git`, use `rm`.
- Upvoter33 8y agoDropbox
- phunehehe0 8y agoI 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
- ivan_ah 8y agoWhat would be the fish shell equivalent alias/function?
- mykowebhn 8y agoThis can be a pain if you're deleting many files, but alias rm='rm -i' has worked for me.
- raibosome 8y agoOkay I think this is more elegant than my workaround. But it doesn’t work for directories, does it?
- mykowebhn 8y agoWhen I execute "rm -r" with that alias, it does prompt me before deleting directories, at least on Mac OS.
- mykowebhn 8y agoBTW, 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.
- cleanyourroom 8y ago`alias rm='rm -rf /'`
- Daviey 8y agoI 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