Ahh what a day! bash and aliases

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

Ahh what a day! bash and aliases

Postby shifty_ben » Sun Feb 26, 2006 12:52 am

Hi all,

having just spent the best part of three hours recovering deleted inodes after a very badly placed space, I think its time I set up an alias for rm. Although there are a lot of other users that remotely log into my webserver I think it is probably as much for me as anyone else. What I want to do is move any 'deleted# files to a folder in /var lets call it /var/deleted then I will set up a cron job to clear this every 7 days or so.
I was going to add this to my /etc/profile

alias rm="mv /var/deleted"

but then it occurred to me if I do that, then it will in effect be the user typing

mv /var/deleted foo.txt

which isn`t what I want, it may be because its late but I cant for the life of me think of how to enter it so it works in the way I want to, any help will be much appreciated

Thanks

Ben
Need a New Signature
User avatar
shifty_ben
LXF regular
 
Posts: 1292
Joined: Tue Oct 04, 2005 9:56 am
Location: Ipswich

RE: Ahh what a day! bash and aliases

Postby Nigel » Sun Feb 26, 2006 1:26 am

I guess the best way would be to set up a bash script that cycles through it's arguments moving them to /var/deleted, then alias that script to rm.
That would take care of the case where you type

rm foo.txt bar.doc old/*.junk

You would have to have something that removed parameters (eg -i), or better yet did something appropriate with them.
Hope this helps,

Nigel.
User avatar
Nigel
LXF regular
 
Posts: 1141
Joined: Fri Apr 08, 2005 8:03 pm
Location: Gloucestershire, UK

RE: Ahh what a day! bash and aliases

Postby shifty_ben » Sun Feb 26, 2006 11:36 am

Kool cheers, Im going to see if I can put something together tomorrow, 9 weeks of straight work and then the stress of that last night have left me rather knackered.
Damn my fat fingers!
Need a New Signature
User avatar
shifty_ben
LXF regular
 
Posts: 1292
Joined: Tue Oct 04, 2005 9:56 am
Location: Ipswich

RE: Ahh what a day! bash and aliases

Postby jhooks » Tue Mar 28, 2006 2:15 pm

If it were me, I would want so way of dealing with multiple files of the same name. Perhaps a simple way to do it would be to have get the full path for the file(s) to be deleted so that when you did
'rm ~/file.txt' in '/home/user'
it ra
'mkdir -p /var/deleted/home/user/; mv /home/user/somefile.txt /var/deleted/home/user/file.txt'

Perhaps you would want some way to keep multiple deleted copies of the same file. You could append a timestamp to the end of each file or even better move it to a folder which was timestamped. E.g. 'timestamp=$(date +%Y%m%d%H%M%S\-%N); mkdir -p /var/deleted/$timestamp/home/user/; mv /home/user/somefile.txt /var/deleted/$timestamp/home/user/file.txt'

Anyway have fun.
jhooks
 
Posts: 48
Joined: Sat Jan 07, 2006 5:39 pm

RE: Ahh what a day! bash and aliases

Postby jhooks » Tue Mar 28, 2006 2:26 pm

I forgot to mention. mv and cp can both overwrite files, perhaps you should alias them to scripts that can determine when files will be overwritten so that they also an go to a "recycle bin".
jhooks
 
Posts: 48
Joined: Sat Jan 07, 2006 5:39 pm

RE: Ahh what a day! bash and aliases

Postby shifty_ben » Tue Mar 28, 2006 2:31 pm

Cheers for the suggestions :D Ive taken a different approach and made and alias for rm which leads to rm -i that should be enough to prevent to many issues. I make nightly backups now so if anything does go its not the end of the world
Need a New Signature
User avatar
shifty_ben
LXF regular
 
Posts: 1292
Joined: Tue Oct 04, 2005 9:56 am
Location: Ipswich


Return to Programming

Who is online

Users browsing this forum: Yahoo [Bot] and 0 guests