LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-11-2010, 02:43 AM   #1
shipon_97
Member
 
Registered: Oct 2005
Location: Bangladesh
Posts: 484

Rep: Reputation: 30
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 ...
 
Old 05-11-2010, 02:45 AM   #2
asifbasha
Member
 
Registered: Feb 2009
Posts: 98

Rep: Reputation: 15
don't give -f option
 
Old 05-11-2010, 02:47 AM   #3
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,330

Rep: Reputation: 227Reputation: 227Reputation: 227
Write a wrapper script that confirms if using -rf.
 
Old 05-11-2010, 02:49 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
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.
 
Old 05-12-2010, 06:09 AM   #5
bsat
Member
 
Registered: Feb 2009
Posts: 347

Rep: Reputation: 72
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.
 
Old 05-12-2010, 06:10 AM   #6
bsat
Member
 
Registered: Feb 2009
Posts: 347

Rep: Reputation: 72
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.
 
Old 05-13-2010, 12:34 AM   #7
shipon_97
Member
 
Registered: Oct 2005
Location: Bangladesh
Posts: 484

Original Poster
Rep: Reputation: 30
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 ?
 
Old 05-13-2010, 12:36 AM   #8
shipon_97
Member
 
Registered: Oct 2005
Location: Bangladesh
Posts: 484

Original Poster
Rep: Reputation: 30
about rm -rf * command !

Thx smeezekitty ..

Would u plz help me to write a wrapper script about the above problem ?
 
Old 05-13-2010, 12:40 AM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Another alternative is to place a file '-i' in the critical directories.


Code:
touch ./-i


Cheers,
Tink
 
1 members found this post helpful.
Old 05-13-2010, 03:53 AM   #10
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,036

Rep: Reputation: 96
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...

Last edited by Disillusionist; 05-13-2010 at 03:57 AM.
 
1 members found this post helpful.
Old 05-13-2010, 04:28 PM   #11
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by Disillusionist View Post
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
 
Old 05-28-2010, 11:44 AM   #12
raj77_in
LQ Newbie
 
Registered: Oct 2008
Posts: 26

Rep: Reputation: 15
Wink 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.
 
0 members found this post helpful.
Old 05-28-2010, 11:46 AM   #13
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] When I type "sudo grub" it says "command not found" in Ubuntu 9.10 Live CD. msbstar Linux - Newbie 7 01-30-2010 12:05 PM
ns:"error when calling class OldSim"&tclsh:"invalid command+child process exits abn." shojaru Linux - Newbie 0 03-05-2009 05:23 AM
Feeding the output of "diff" or "cat" command to dpkg --purge kushalkoolwal Debian 9 06-19-2008 08:27 AM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 11:18 PM
Tiny Sofa 2.0 - I thought "halt", "reboot" were only root command ?? sorcerer Linux - Distributions 1 08-21-2004 04:28 PM


All times are GMT -5. The time now is 11:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration