LinuxQuestions.org
Review your favorite Linux distribution.
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 10-28-2003, 02:26 PM   #1
szf2
LQ Newbie
 
Registered: Aug 2003
Distribution: RedHat 9.0
Posts: 17

Rep: Reputation: 0
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 . 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
 
Old 10-28-2003, 03:08 PM   #2
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
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
 
Old 10-28-2003, 06:16 PM   #3
szf2
LQ Newbie
 
Registered: Aug 2003
Distribution: RedHat 9.0
Posts: 17

Original Poster
Rep: Reputation: 0
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") . 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

Last edited by szf2; 10-28-2003 at 06:19 PM.
 
Old 10-28-2003, 06:22 PM   #4
Mikhail_16
Member
 
Registered: Oct 2003
Distribution: Gentoo rules them all
Posts: 279

Rep: Reputation: 31
rename rmv function . duh!
name it something like 'trash' so you would not confuse it with anything.
 
Old 10-28-2003, 06:41 PM   #5
szf2
LQ Newbie
 
Registered: Aug 2003
Distribution: RedHat 9.0
Posts: 17

Original Poster
Rep: Reputation: 0
Maybe I wasn't clear (or maybe you're just more of a noob than I am ), 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
 
Old 10-28-2003, 07:15 PM   #6
Mikhail_16
Member
 
Registered: Oct 2003
Distribution: Gentoo rules them all
Posts: 279

Rep: Reputation: 31
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.
 
Old 10-28-2003, 07:21 PM   #7
Mikhail_16
Member
 
Registered: Oct 2003
Distribution: Gentoo rules them all
Posts: 279

Rep: Reputation: 31
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?
 
Old 10-28-2003, 08:09 PM   #8
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 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

Last edited by Tinkster; 10-28-2003 at 08:16 PM.
 
  


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
any recycle bin in mandrake 9.2 server?? starofflame Mandriva 1 05-01-2009 03:30 PM
Samba and Windows Recycle Bin scottpioso Linux - General 1 05-01-2009 03:29 PM
Emptying the recycle bin hongman Linux - Newbie 4 03-26-2005 05:05 PM
A Recycle Bin script. 95se Linux - Software 2 12-19-2003 12:12 PM
Recycle Bin? puzz_1 Linux - General 1 05-25-2003 04:08 PM


All times are GMT -5. The time now is 06:22 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