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 |
You could use something like this:
Code:
#!/bin/bash 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 |
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 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 |
rename rmv function :D. duh!
name it something like 'trash' so you would not confuse it with anything. |
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 |
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. |
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? |
Quote:
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 Quote:
Happy hacking! Cheers, Tink |
All times are GMT -5. The time now is 12:15 AM. |