批量修改后缀为.bak的文件为原文件名
例如AuthLogs.log.bak执行后变为AuthLogs.log
python写法
# -*- coding: utf-8 -*-
import os,sys
path=sys.argv[1]
def main():
for file in os.listdir(path):
#print file
#以.bak后缀结尾
if file.endswith('.bak'):
new_filename='.'.join(file.split('.')[:-1])
print 'rename %s to: %s\n'%(file,new_filename)
os.rename(os.path.join(path,file),os.path.join(path,new_filename))
if __name__ == '__main__':
main()
shell写法
#!/bin/bash
sfile=log.bak
dfile=log
for i in "ls *.$sfile"
do
mv $sfile ${i%.*}.$dfile
done