DEV Community

Discussion on: Mastering rm command

Collapse
 
habereder profile image
Raphael Habereder

I just commented this on another post, I thought it might fit here too.
As you perfectly said:

using -rf flag, verify you're in the right directory by using $ pwd command, then proceed to delete files.

If you want to prevent deleting any important directory via rm -rf <directory/>*, there is a neat trick.

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 * 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.
Example:

rha@vm:/tmp/rmtest$ touch foobar
rha@vm:/tmp/rmtest$ ls
foobar
rha@vm:/tmp/rmtest$ touch -- -i
rha@vm:/tmp/rmtest$ ls
-i  foobar
rha@vm:/tmp/rmtest$ rm -r *
rm: remove regular empty file 'foobar'? ^C
Collapse
 
hasone profile image
HasOne

Thanks, Habereder! you're the best this is something awesome I'll definitely do this in future like server host and local ... and will save our life. I love this hack!

Collapse
 
vlasales profile image
Vlastimil Pospichal

I use rm -rf directory often, but never use rm -rf "$directory"