Home > Bash, linux, Tip > Easy Copy/Rename with Bash

Easy Copy/Rename with Bash

Instead of
cp a_very_long_file_name.ext a_very_long_file_name.ext.bak
cp a_very_long_file_name.ext{,.bak}

Instead of
mv foldername/ foldername.old/
mv foldername{,.old}/

I forgot where I got this tip but it’ll definitely save you a lot of typing. You fingers will thank you for that. Happy scripting.

#!/usr/bin/env python
import telnetlib,time,random,smtplib
from subprocess import Popen
from email.MIMEText import MIMEText
PORT=110

def testhost(host):
result=”
count=0
while count < 3:
try:
tn=telnetlib.Telnet(host,PORT)
except Exception:
return “bad”
result=tn.read_until(‘Dovecot ready.’,5)
index=result.find(‘ready.’)
tn.close()
if index < 0:
count+=1
SLEEP=random.randint(5,10)
print( “count %d, sleeping for %d seconds now” % (count,SLEEP) )
time.sleep( SLEEP )
else:
return ‘good’
return ‘bad’

def rpr(sub,msg):
FROM=’noreply@25os.net’
TO=[‘ricoc@aweb.com’]
message=MIMEText(msg)
message[‘Subject’]=”%s” % sub
message[‘From’]=’Notification’
message[‘Reply-to’]=’noreply@25os.net’
message[‘To’]=TO[0]
server=smtplib.SMTP(‘localhost’)
server.sendmail(FROM, TO, message.as_string())
server.close()

def main():
#    IPS=[ ‘69.48.143.47’, ‘69.48.143.66’, ‘69.48.143.83’ ]
HOSTS={ ‘srv_1’: ‘69.48.143.47’,
‘srv_2’: ‘69.48.143.83’,
‘srv_5’: ‘69.48.143.66’ }
restarted_hosts=”
for hn,ip in HOSTS.items():
if testhost(ip) == ‘bad’:
Popen( [ “/usr/local/bin/killvm”, hn ] )
time.sleep(2)
Popen( [ “/usr/local/bin/startvms”, hn ] )
#simulate machine restart
print(“%s restarted\n” % ip)
restarted_hosts+=”%s\n” % ip
if restarted_hosts:
rpr(“Virtualbox guest restarted”, restarted_hosts)

if __name__==’__main__’:
main()

Advertisements
Categories: Bash, linux, Tip
  1. December 12, 2009 at 3:30 am

    Stunning, I did not heard about this topic until now. Thanks!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: