Embora isso possa ser feito no bash, eu sou mais um cara de python, então aqui vai minha proposta:
#!/usr/bin/env python
import glob
import os
import sys
def cpdir(source, target):
if target.endswith('/'):
if not os.path.isdir(target):
print "Target directory doesn't exist: %s" % target
sys.exit(1)
target += source
if '--backup=numbered' in sys.argv:
dirs = glob.glob(target + '.~*~')
if not dirs:
num = 1
else:
num = max([int(dir.split('~')[-2]) for dir in dirs]) + 1
target += '.~%s~' % num
cmd = 'cp -a %s %s' % (source, target)
#print cmd
rv = os.system(cmd)
sys.exit(rv)
def main():
if len(sys.argv) < 3:
print "Usage: %s [--backup=numbered] <source> <dest>" % sys.argv[0]
sys.exit(1)
source = sys.argv[1]
target = sys.argv[2]
return cpdir(source, target)
if __name__ == '__main__':
sys.exit(main())