LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-23-2006, 10:33 PM   #1
GATTACA
Member
 
Registered: Feb 2002
Location: USA
Distribution: Fedora, CENTOS
Posts: 209

Rep: Reputation: 32
safety script prevent accidental rm -rf


Hello.

I'm trying to prevent some students from accidentally deleting their important folders (they are new to the commandline).

What I would like would be something that warns you before executing a _specific_ command. For example, if the critical folder name is /home/student/homework. If the student were to type 'rm -rf /home/student/homework' the system would reply back with something like: "Are you sure you want to do this?(yes|no): "

BUT if the student types 'rm -rf /home/student/homework_old' the system won't object or ask questions.

Is this possible via a bash script?

Please let me know if this is unclear.
Thanks in advance.
 
Old 10-23-2006, 10:48 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Sure. Here's one alternative:
http://are.berkeley.edu/mason/comput...s/unix/rm.html

The simplest, easiest approach is here:
http://tldp.org/LDP/abs/html/aliases.html

Just add the following to a user's local ~/.bashrc (or, better, to the global /etc/bashrc) file:
Code:
alias rm="rm -i"
'Hope that helps .. PSM

Last edited by paulsm4; 10-23-2006 at 10:51 PM.
 
Old 10-23-2006, 10:56 PM   #3
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
"rm -rf" actually won't object because that is the meaning of -f.
"rm -r" would complain if you have alias rm="rm -i". You could write
Code:
function rm {
   if [ $1 = "-r" ]; then
      /usr/bin/rm -f "$@"
   else
      /usr/bin/rm -i "$@"
   fi
}
My initial testing seems to indicate that the solution doesn't work, but that is probably because I'm doing something bad for the conditional.

Last edited by tuxdev; 10-23-2006 at 11:03 PM.
 
Old 10-27-2007, 04:57 PM   #4
sKaar
Member
 
Registered: Jun 2006
Location: dartmouth, nova scotia
Distribution: slackware 12.1
Posts: 74

Rep: Reputation: 2
perhaps an option to list the files that will be deleted, with a default of don't proceed. i was reading the unix haters handbook and thought of all the times i had to make sure not to delete something important as root. i don't play with root much, so the likelihood i'll do something wrong is high.
the worst i did was formatting /home, but it was on reiserfs, so recovery was easy, everything at the root of /home was renamed, everything in the sub-directories was unmolested.
 
Old 10-29-2007, 02:36 AM   #5
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
Here is a sample shell front end to rm:
Code:
#!/bin/bash
###
### Shell front end for rm
###
case $# in
   0) echo "no file or directory passed to rm"
      exit 1;;

   1) ## 1 variable passed (should be a file)
      if [ -f $1 ]
      then
         /bin/rm $1
      else
         echo "File $1 not found"
         exit 2
      fi;;

   2) ## 2 variables check for -r -f -rf options
      l_force=`echo $1|sed 's/f//'`
      if [ "$l_force" != "$1" ]
      then
         # -f found!
         echo "Are you sure? Y/[N]"
         read l_ans
         case $l_ans in
            [Yy]|[Yy][Ee][Ss]) /bin/rm $1 $2;;
            *) exit 3;;
         esac
      else
         /bin/rm $1 $2
      fi;;
 
   *) echo "Syntax rm [options] [file(s) or directory]";;
esac
Save the file as new rm somewhere where everyone can access it eg: /usr/local/newrm

change permissions so everyone can execute it eg: chmod 755 /usr/local/newrm

add an alias for rm eg: alias rm='/usr/local/newrm'

Please note that I have not catered for the full option range supported by rm.

Feel free to modify the script in any way to better fit your needs.
 
  


Reply



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
prevent irc script accessing web site dtra Linux - Networking 3 06-09-2005 06:10 AM
Is there any way to temporarily prevent a script from loading in certain runlevel? davidas Debian 5 04-12-2004 09:12 AM
safety first with kernelupgrade, but how? deNiro Slackware 1 12-20-2003 09:02 AM
accidental mke2fs sarahan Linux - General 1 11-15-2003 04:58 AM
firewall is safety? hurtsman Linux - Networking 2 12-02-2001 04:14 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:12 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
Open Source Consulting | Domain Registration