You didn't specify that it had to be a bash solution so here is a python solution. It takes into account that you require a 3 character extension. However it doesn't do much in the way of checking you wont break anything. It does check for directories and that you are only going to rename real files. This solution will require an -E option to be passed in.
edit:
If filename starts with a . (hidden file) ignore. With regards to symlinks, that might take a little more thought.
#!/usr/bin/env python import sys import argparse import re import os def main(): path = '/var/dump/files' parser = argparse.ArgumentParser() parser.add_argument('-E', help='three character extension(.i.e log)',\ type=str) args = parser.parse_args() if args.E: if not re.search(r'^[a-zA-Z]{3}$', args.E): print 'Invalid Input. A three character extenstion required: %s' % str(args.E) sys.exit(19) ext = '.'+str(args.E) else: print 'Invalid Input. A three character extenstion required' sys.exit(19) files = [f for f in os.listdir(path) if os.path.isfile(os.path.join(path,f))] for f in files: # ignore hidden files if f[0] == '.': continue name = f.split('.')[0] os.rename(os.path.join(path,f),os.path.join(path,name+ext)) if __name__ == "__main__": main()
This solution the -E will be option, and use '.log' as a default. You could specify Required=True in the add_argument() to have it as a required option.
#!/usr/bin/env python import sys import argparse import re import os def main(): path = '/var/dump/files' parser = argparse.ArgumentParser() parser.add_argument('-E', help='three character extension(.i.e log)',\ type=str) args = parser.parse_args() if args.E: if not re.search(r'^[a-zA-Z]{3}$', args.E): print 'Invalid Input. A three character extenstion required: %s' % str(args.E) sys.exit(19) ext = '.'+str(args.E) else: ext = '.log' files = [f for f in os.listdir(path) if os.path.isfile(os.path.join(path,f))] for f in files: # ignore hidden files if f[0] == '.': continue name = f.split('.')[0] os.rename(os.path.join(path,f),os.path.join(path,name+ext)) if __name__ == "__main__": main()