LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   about "rm -rf *" command ! (https://www.linuxquestions.org/questions/linux-newbie-8/about-rm-rf-%2A-command-807103/)

shipon_97 05-11-2010 01:43 AM

about "rm -rf *" command !
 
Dear Friends ,

Sometimes we linux person make a command "rm -rf * " which is very dangerous for the Llnux server . By mistake , using the above command I have deleted more importent files .

So that I need a suggestion , Is there any way to block the above command or there are any way to make a warning when I give the command :

"rm -rf * "

Plz suggest me ...

asifbasha 05-11-2010 01:45 AM

don't give -f option

smeezekitty 05-11-2010 01:47 AM

Write a wrapper script that confirms if using -rf.

druuna 05-11-2010 01:49 AM

Hi,

You could alias rm to include the -i (interactive) option: alias rm='rm -i' (to be added to for example your /etc/profile).

This will ask you to delete a file (interactive => rm will ask permission before removing anything).

I'm not sure if this is workable (you need to say y for every file that needs to be deleted). Being careful and checking what you typed before hitting enter is probably the best way.

Anyway, hope this helps.

bsat 05-12-2010 05:09 AM

to be extra safe how about aliasing "rm -rf" itself to "rm -ri".
but as druuna said the to be prompted every time is surely no fun.

bsat 05-12-2010 05:10 AM

to be extra safe how about aliasing "rm -rf" itself to "rm -ri".
but as druuna said the to be prompted every time is surely no fun.

shipon_97 05-12-2010 11:34 PM

How to protect form "rm -rf * " command
 
Thx bsat and u r correct that (alias 'rm -rf'='rm -ri' ) is not working . Is there any way to solve it ?

shipon_97 05-12-2010 11:36 PM

about rm -rf * command !
 
Thx smeezekitty ..

Would u plz help me to write a wrapper script about the above problem ?

Tinkster 05-12-2010 11:40 PM

Another alternative is to place a file '-i' in the critical directories.


Code:

touch ./-i


Cheers,
Tink

Disillusionist 05-13-2010 02:53 AM

I thought the -f overrides the -i flag?

I would suggest writing a wrapper just as smeezekitty suggested.

This doesn't need to be too complex, but you need to work out what you want to cater for.

Do you just want to check for -rf or do you want to check anything that includes the -f option?

Code:

#!/bin/bash

### Sample wrapper for rm
### just checks that $1 is set to -rf
###

run_command()
{
  /bin/rm $*
}

if [ "$1" == '-rf' -o "$1" == '-fr' ]
then
  echo "Current directory: $(pwd)"
  echo "About to run rm $*"
  read -p "Are you sure?" ans
  case $ans in
      [Yy]|[Yy][Ee][Ss]) run_command;;
      *) exit;;
  esac
else
  run_command
fi

The code would need to be executable (chmod 755) and either aliased or found in the PATH environment variable before /bin/rm

EDIT: aliased would be safer than modifying the PATH!

Obviously you can put a lot more testing in the script until you are completely satisfied, for example the -rf option could be reversed to -fr but would have the same impact...

Tinkster 05-13-2010 03:28 PM

Quote:

Originally Posted by Disillusionist (Post 3966704)
I thought the -f overrides the -i flag?

Nope. Not on my system(s), anyway. Seems that the last thing
on the command-line wins (which would be -i) The greatest caveat
with the file method is that it only protects files that
start with character that's lower in the sort order than
the '-' ....



Cheers,
Tink

raj77_in 05-28-2010 10:44 AM

rm -rf solution is to use -I
 
if you want to protect yourself, I guess the best bet is to alias rm to use -I like below:

alias rm="rm -I"

This would ensure that if there are more than three files to be deleted then only rm will confirm.

druuna 05-28-2010 10:46 AM

@raj77_in: That should be i instead of I. Which was already contributed by several people.....


All times are GMT -5. The time now is 03:50 PM.