DEV Community

Discussion on: Linux - I am Love with Terminal

Collapse
 
habereder profile image
Raphael Habereder • Edited

Pro Tip: never use rm -r directory/
You will get used to it, and one day you will use env-vars and use rm -r $someDir

That is when bad things can happen. Especially if $someDir ends up being / for some reason.

If you delete something recursively, be precise. If you want to prevent deleting any important directory, there is a neat trick.

Get used to rm -r directory/* && rmdir directory/ and
create a file named -i in all directories you want to protect. This can be done using
touch -- -i
or
touch ./-i

The * in rm -r directory/* will expand the -i File to the command line, so your command ultimately becomes
rm -rf -i
Thus rm will prompt you before deleting anything. You can put this file in your /, /home/, /etc/, and so on.

Collapse
 
omar1024 profile image
omar

thank you pal..that was helpful :)

Collapse
 
habereder profile image
Raphael Habereder

Gladly! I hope you will continue to enjoy linux and it's powerful terminal even more in the future :D

Collapse
 
190245 profile image
Dave

rm doesn't let you delete root anymore.

They fixed that, check the manpage.

Collapse
 
habereder profile image
Raphael Habereder • Edited

That doesn't mean that a cleaned out /etc/ or any other system-critical directory won't hurt you a lot. Sure, you can't "kill" a linux box with RM anymore, but you can still make it pretty damn unusable

Thread Thread
 
190245 profile image
Dave

Isn't that what good permissions, not running as root by default, and backups, are all for?

Thread Thread
 
habereder profile image
Raphael Habereder

Considering we are discussing this in a beginners post about Linux, the foundation of careful usage should still apply.

Other than that, yes you are certainly right. A beginner probably doesn't have these precautions in place though.

Collapse
 
rhymes profile image
rhymes

what if you simply alias rm with a "trash" tool that puts stuff in your desktop Trash can?
Or in alternative, alias rm with rm -i ?

Collapse
 
habereder profile image
Raphael Habereder

That could be a solution. I never really used "~/.local/share/Trash/" before.
rm in linux means "it's gone for good now". I'm not sure if rm has a "soft delete" or anything of the likes. The only thing to revert a rm is to restore a backup afaik.

I'm not a fan of aliasing rm, since aliases are user-only and if you don't keep your alias file portable on you for every system, through git or whatever, you rely on it on the wrong machine, and your stuff is gone.
rm should always be used with caution.

Thread Thread
 
rhymes profile image
rhymes

I don't use Linux but in macOS terminal I have the following alias in my .zshrc:

rm='trash -i'

where trash is hasseg.org/trash/

Never been happier :D

Thread Thread
 
habereder profile image
Raphael Habereder

That's a cool little utility for my mac! I'll be sure to try that one, thanks! :)

Some comments have been hidden by the post's author - find out more