Preventing accidental deletion of important files using safe-rmFeeding the Cloud
<a href="https://creativecommons.org/licenses/by-sa/4.0/">Creative Commons Attribution-ShareAlike 4.0 International License</a>
https://feeding.cloud.geek.nz/posts/preventing-accidental-deletion-of/Feeding the Cloudikiwiki2012-11-04T04:30:26Zhttps://feeding.cloud.geek.nz/posts/preventing-accidental-deletion-of/comment_1_ebb3a5fccd9c9a9b0d41e5141f910e3f/debian.org2012-11-04T04:30:26Z2008-06-20T11:45:00Z
Nice app, thanks!
https://feeding.cloud.geek.nz/posts/preventing-accidental-deletion-of/comment_2_0fd03b2ab8691276699a8696ed01a956/Badzil2012-11-04T04:30:26Z2008-06-20T12:36:00Z
<p>I did this with an alias:<br />
"alias rm='rm -i'"</p>
<p>I use "\rm" when I'm sure about what I'm deleting but most of the time, I prefer to go through the confirmation. Too many files disappeared because of a rushed removal action.</p>
https://feeding.cloud.geek.nz/posts/preventing-accidental-deletion-of/comment_3_bd8bb6b58be76980e33d406b9e761b10/Anonymous2012-11-04T04:30:26Z2008-06-20T18:46:00Z
<p>Very very dangerous command</p>
<p>Do in a folder</p>
<p>rm -rf .*</p>
<p>for me, it's a 'rm' bug because</p>
<p>rm -rf *</p>
<p>Do not remove dot file.</p>
<p>But rm -rf .* remove .. folder !</p>
<p>rm -rf .*</p>
<p>And everything is remove on your disk ;-(</p>
<p>I think you can a this to your tool.</p>
<p>Sytoka</p>
https://feeding.cloud.geek.nz/posts/preventing-accidental-deletion-of/comment_4_ab1bf5e08b85f73d8d389e89706e2750/Matt2012-11-04T04:30:26Z2008-06-20T20:44:00Z
<p>This is an unfortunate error to make, but no, you're definitely not alone. One time I accidently deleted half of the CEO's home directory on the file server.</p>
<p>(Bonus question: If you're trying to get rid of all the hidden files in a directory, is "rm -rf .*" a bad idea?)</p>
<p>Anyway, it wouldn't be too difficult* to write a shell-replacement for 'rm' which makes an inode-based copy into a .trash directory before deleting, which the user can then empty out at their leisure. I suppose moving the file to the user's .trash directory would suffice too, although speed and disk space become a problem when more than one partitions are involved. As an aside, is there a non-trivial way to determine the current mountpoint's root from pwd? Oh well. Glad you caught this before it got out of hand, and take solace in the fact that you're definitely not the only person to do it.</p>
<p>* - implementation is left as an exercise to the reader</p>
https://feeding.cloud.geek.nz/posts/preventing-accidental-deletion-of/comment_5_336123be51ba55778a8e0c131dfbeafe/fil2012-11-04T04:30:26Z2008-06-20T20:46:00Z
<p>The replacement of /bin/rm is a bad idea IMO.</p>
<p>It's not as evil at aliasing rm to rm -i (which you mention as one of the alternative approaches) but it's going down a similar road.</p>
<p>The first problem is that if you come to rely on this functionality, and so feel slightly safer when using rm, what happens when you're on a system where safe-rm is not installed? (this is the thing that makes the rm -i alias <strong>totally evil</strong> -- I'll admit that this is much less of an issue with safe-rm, as it's not like you're going to get into that habit of typing <strong>rm </strong>*, assuming there's a -i to save you)</p>
<p>Secondly, what happens when a pre/postinst comes along that really needs to delete something that you thought should never be deleted?</p>
<p>The correct thing to do IMO, if you want to do this is not to replace rm, but to add, say "srm" and get into the habit of using that. That way, when it's not installed you instantly get alerted to the fact that you have no safety net by the:</p>
<p>phil@perdix:~$ srm<br />
bash: srm: command not found</p>
<p>Then, if people want to train themselves to be shot in the foot, they're at liberty to alias rm to srm -i if they feel the need without damaging the system for other admins <img alt=";-)" src="https://feeding.cloud.geek.nz/smileys/smile4.png" /></p>
https://feeding.cloud.geek.nz/posts/preventing-accidental-deletion-of/comment_6_6fd1d678430b592c4b9aab1093fc6bea/François2012-11-04T04:30:26Z2008-06-20T22:13:00Z
<p>Fil: you're right about the pre and postinst packages being unable to delete certain directories if safe-rm is protecting too many directories.</p>
<p>I thought about adding apt-get hooks, but then I figured that I really didn't want a buggy maintainer script to accidentally delete some of my "protected paths" anyways <img alt=":)" src="https://feeding.cloud.geek.nz/smileys/smile.png" /></p>
<p>Regarding the srm binary, I think it's a much better name than having to type safe-rm. Thanks for that suggestion.</p>
<p>I think that the whole point of the package is to be an opt-out one as opposed to opt-in however. I was logged in as a different user (and didn't have an alias set there) where I deleted half of my important files.</p>
https://feeding.cloud.geek.nz/posts/preventing-accidental-deletion-of/comment_7_449ecd3b5d15f83650fb2ed30d4b19bc/Anonymous2012-11-04T04:30:26Z2008-06-21T00:15:00Z
I actually would like the opt-in approach, in part because I only want this protection for interactive calls to rm, rather than those done by scripts.
https://feeding.cloud.geek.nz/posts/preventing-accidental-deletion-of/comment_8_b8519c0c163ad6ce73077cf7c35c58b5/Luca Bruno aka Lethalman2012-11-04T04:30:26Z2008-06-21T10:14:00Z
<p>Good app really. I'll wait it for being more improved or even contribute someway.<br />
I've seen already a deb package. What about adding it to mentors.debian.org?</p>
https://feeding.cloud.geek.nz/posts/preventing-accidental-deletion-of/comment_9_7f3e95cbbca19b101f0ca511fe1c299d/François2012-11-04T04:30:26Z2008-06-21T10:29:00Z
<p>lethalman: I'm not sure exactly what you mean by suggesting that this package be uploaded to mentors.debian.org because it's in unstable already and it's part of the collab-maint project.</p>
<p>So if you want to contribute anything, feel free to clone the git repo at http://git.debian.org/?p=collab-maint/safe-rm.git and send patches.</p>
https://feeding.cloud.geek.nz/posts/preventing-accidental-deletion-of/comment_10_4a50890bf4b1d291c2b44407579bcf53/Asheesh Laroia2012-11-04T04:30:26Z2008-06-22T21:25:00Z
<p>Everyone has his own safe-rm.</p>
<p>For me, this is enough to do it:</p>
<p>alias rm='echo -n "YOU ARE ON " && hostname && echo Sleeping for 2 seconds... && sleep 2 && rm -v'</p>
<p>It's more useful than the blacklist, actually; and if I know I really want to rm something, I \rm it.</p>
<p>http://svn.asheesh.org/svn/public/conf/bashrc.safe_rm is this snippet of my bashrc.</p>
https://feeding.cloud.geek.nz/posts/preventing-accidental-deletion-of/comment_11_96f1d604e6dec54b2faff52724c0da9e/CAPSLOCK20002012-11-04T04:30:26Z2008-07-02T14:49:00Z
<p>Another solution is to mount /usr read only. During normal operation you should never need write access anyway.</p>
<p>You can configure dpkg to remount /usr with write permissions before installing software, so you should never have to do it manually.</p>
https://feeding.cloud.geek.nz/posts/preventing-accidental-deletion-of/comment_12_f11fc247b9d56d406cb37b380a580d98/Anonymous2012-11-04T04:30:26Z2008-09-15T16:06:00Z
<p>While you're at it, maybe (optionally) support the XDG Trash spec too - deleting would turn it into a move to Trash, compatible with Gnome/KDE, for easy undeletion.</p>
<p>Some people (not me) have been thinking about this too, see:<br />
http://code.google.com/p/trash-cli/</p>
<p>and this thread:</p>
<p>http://lists.freedesktop.org/archives/xdg/2008-August/009792.html</p>
https://feeding.cloud.geek.nz/posts/preventing-accidental-deletion-of/comment_13_40212965caf4db4ed3c732124a617edf/Tim2012-11-04T04:30:26Z2008-09-16T05:30:00Z
<p>I know it's non-standard but I tend to type:</p>
<p>rm /usr/lib/blah -rf</p>
<p>By putting the -rf at the end I ensure that I can't accidentally enter the command before I've typed the full path. I've also picked up the habit of always pausing before typing the final -rf to check that it is what I plan to do.</p>
https://feeding.cloud.geek.nz/posts/preventing-accidental-deletion-of/comment_14_aac2e0009e42cdef63cf03aba5417170/Miriam Ruiz2012-11-04T04:30:26Z2008-11-01T04:36:00Z
Are regexp expressions allowed in the configuration file containing the list of protected files and directories?
https://feeding.cloud.geek.nz/posts/preventing-accidental-deletion-of/comment_15_6ed37840fdd7d8c4908c9f2d71d4855e/François2012-11-04T04:30:26Z2008-11-01T07:19:00Z
<p>Regular expressions are not yet supported, but that's a great idea. I've added it to my issue tracker.</p>
<p>Since safe-rm is written in Perl, it shouldn't be too hard to implement either.</p>