LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How do I make a RH recycle bin? (https://www.linuxquestions.org/questions/linux-newbie-8/how-do-i-make-a-rh-recycle-bin-109566/)

szf2 10-28-2003 01:26 PM

How do I make a RH recycle bin?
 
After "rm *"ing some files I didn't want to remove, I decided I needed some more protection against myself :p. A friend suggested placing

alias rm="rm -i"

in my .bashrc file which I did. However, what I really would like is if all the files I sent through rm really got sent to some trash folder. That is "rm somefilename" mapped to "mv somefilename /trash" where "/trash" is whatever the path is to my trash folder. I don't know how to put this in my .bashrc b/c I need "somefilename" to be inserted between the constants "mv" and "/trash".

Any sugestions?

Thanks,
Steve

Tinkster 10-28-2003 02:08 PM

You could use something like this:
Code:

#!/bin/bash
for test in "$@"
do
if [ -d "$test" ] ; then
echo "Moving directory:" "$test"
tar --remove-files -czPf /tmp/trash/"$test".tar.gz "$PWD/$test"
rm -rf $test
else
echo "Moving file:" "$test"
tar --remove-files -czPf /tmp/trash/"$test".tar.gz "$PWD/$test"
fi
done

Save it to /bin/rmv and "alias rm=/bin/rmv".
It's not thoroughly tested, I just made it as
a quick hack. (Worked with my test-cases
and directories, but there may be problems
on your machine ... so, please, don't just
use it and blame me if something fails ;})

I'm sure there's a REAL shell guru around
that will be able to make this thing safe or
syntactically correct (the fact that it works
for me may mean nothing :}).

Cheers,
Tink

szf2 10-28-2003 05:16 PM

Tinkster,

Thanks for the idea. I've never written a bash script before, but based on your code I came up with the following:

Code:

#!/bin/bash
for a in "$@"
do
cp -r $a /home/myusername/trash
rm -rf $a
done

I saved this code to /home/myusername/bin/rmv and edited my .bashrc so it contains the line

alias rm=rmv

Now everything gets sent to the trash when I type "rm stuff", plus I don't have to type "rm -r stuff" to remove directories (just "rm stuff") :D. Also, I can empty the trash using "/bin/rm -r /home/myusername/trash/*".

The only problem is that now I can't type things like "rm -v stuff" since the "-v" trips up the rmv code. Any suggestions (anyone)? How does one typically test for a "-flag" argument being passed to a bash shell? If I knew this maybe I could switch over different flags for the "rm" command and have my "rmv" script respond appropriately.

Thanks,
Steve

Mikhail_16 10-28-2003 05:22 PM

rename rmv function :D. duh!
name it something like 'trash' so you would not confuse it with anything.

szf2 10-28-2003 05:41 PM

Maybe I wasn't clear (or maybe you're just more of a noob than I am :D), but the problem isn't the function name. I ran "slocate rmv" before doing all this to make sure I wasn't overriding some built in funciton (and I'm not). If I type

rm stuff

there is no problem since this maps to

rmv stuff

which does a recursive removal of whatever "stuff" is. However, if I type

rm -v stuff

(which should do an rm with verbosity), this maps to

rmv -v stuff

and the rmv code cannot handle the "-v" flag (i.e. verbosity flag). However, I would like my rmv code to respond appropriately to a "-v" flag. For example, it could give verbose output from both the "cp" and "rm" commands, plus other useful information. Further, I would like the code to be able to handle other common flags and throw an error if it gets and unexpected flag.

Thus, what I really need to know here is how to handle flags in a bash script (since, again, this is my first bash script).

Thanks,
STeve

Mikhail_16 10-28-2003 06:15 PM

oh, i misunderstood the question i thought 'trips up' meant something else. Sorry for snide remark.

You have to google 'bash shell scripting' to answer your question i do not have a reference handy and cannout remember the answer off the top of my head. What you have to do is to mess around with rmv code. Basically rmv is a function and you have to give it ability to take parameters and act accordingly. Familiarity with C helps.

Mikhail_16 10-28-2003 06:21 PM

Actually nevermind; Tinkster has already provided you the answer all you have to do is use his code and change the behaviour to whatever you want it to do. -d in the code is an allowed flag for the command. basically like this (your code original:)
#!/bin/bash
for a in "$@"
do
if [-v $a] ; then
cp -r $a /home/myusername/trash
rm -rvf $a
else
cp -r $a /home/myusername/trash
rm -rf $a
fi
done

Get it?

Tinkster 10-28-2003 07:09 PM

Quote:

Originally posted by szf2
Tinkster,

Thanks for the idea. I've never written a bash script before, but based on your code I came up with the following:

Code:

#!/bin/bash
for a in "$@"
do
cp -r $a /home/myusername/trash
rm -rf $a
done


Glad I could help (in a way :}) ... I see one disadvantage
in your method, though. If you happened to delete two
files from different subdirectories, but with identical names,
the first one deleted would be lost :} My technique preserves
the paths :}

DOH... :} it doesn't, not in the extent I intended :}
here's a modified version
Code:

#!/bin/bash
for test in "$@"
do
if [ -d "$test" ] ; then
  echo "Moving directory:" "$test"
  if [ -x /tmp/trash/"$test".tar.gz ]; then
    tar --remove-files -rzPf /tmp/trash/"$test".tar.gz "$PWD/$test"
    rm -rf $test
  else
    tar --remove-files -czPf /tmp/trash/"$test".tar.gz "$PWD/$test"
    rm -rf $test
  fi
else
  echo "Moving file:" "$test"
  if [ -x /tmp/trash/"$test".tar.gz ]; then
    tar --remove-files -rzPf /tmp/trash/"$test".tar.gz "$PWD/$test"
  else
    tar --remove-files -czPf /tmp/trash/"$test".tar.gz "$PWD/$test"
fi
done




Quote:

The only problem is that now I can't type things like "rm -v stuff" since the "-v" trips up the rmv code. Any suggestions (anyone)? How does one typically test for a "-flag" argument being passed to a bash shell?
Mikhail provided a solution to that :}
Happy hacking!

Cheers,
Tink


All times are GMT -5. The time now is 12:15 AM.