Saturday, 31 August 2013

How to prevent accidental delete rm -rf * in Linux

To prevent accidental delete rm -rf * in Linux by different ways

If you want to prevent any important directory deleted accidental

Create a file named -i in that directory. How can such a odd file be created? Using touch -- -i or touch ./-i

Now try rm -rf *:

[root@server]# touch {1..4}
[root@server]# touch -- -i
[root@server]# ls
1  2  3  4  -i file1 file2 directory1
[root@server]# rm -rf *
rm: remove regular empty file `1'? n
rm: remove regular empty file `2'?
Here the * will expand -i to the command line, so your command ultimately becomes rm -rf -i.
So command will prompt before removal. You can put these files in your /, /home/, /etc/, / etc.


Use --preserve-root as an option to rm. In the rm included in newer coreutils packages, this option is the default.
create alias to rm

alias rm=rm -i --preserve-root

--preserve-root means do not remove `/' (default)


Use safe-rm

Safe-rm is a safety tool intended to prevent the accidental deletion of important files by replacing /bin/rm with a wrapper, which checks the given arguments against a configurable blacklist of files and directories that should never be removed.

Users who attempt to delete one of these protected files or directories will not be able to do so and will be shown a warning message instead:

[root@server]# rm -rf /usr
Skipping /usr

Share This!

1 comment:

Here We Write The Problems we face at different situations and Solutions to those problems.