当你从 Windows 服务器换到 Linux 服务器的时候,以前的上传目录的目录名、文件名会遇到大小写的问题。在 Windows 环境下面没有文件区分大小写的概念,而 Linux 却有严格的文件名大小写区分。 这样一来但来自 Windows 服务器上面那些文件就有可能遇到因为文件名中带有大写字母而无法被找到。需要将这些文件的文件名从大写转换为小写字母…
我需要遍历目录、子目录下的所有文件,当然也包括目录名称,于是自己写了一个
#!/usr/bin/python import os, sys,re print "Auto convert Upper filename to Lower filename" dir = '/image' print "find files..." for root,dirs,files in os.walk(dir): print root print str(len(files)) + " found." os.rename(root,root.lower()) for f in files: filename = root.lower() + "/" + f if re.search('[A-Z]',filename) != None: print filename newfilename = filename.lower() print "Renaming", f, "to", f.lower(), "..." os.rename(filename, newfilename)
修改dir目录即可
优化版本(yip修改):
#!/usr/bin/python import os, sys,re print "Auto convert Upper filename to Lower filename" dir = '/data/dandantang/Web/Resource/image/' print "find files..." i=0 def func WalkHere(dir): for root,dirs,files in os.walk(dir): for f in files: filename = root + "/" + f if re.search('[A-Z]',f) != None: print filename + " found." newfilename = root + "/" + f.lower() print "Renaming", f, "to", f.lower(), "..." os.rename(filename, newfilename) for one in dirs: filename = root + "/" + one if re.search('[A-Z]',one) != None: print filename + " found." newfilename = root + "/" + one.lower() print "Renaming Dir", one, "to", one.lower(), "..." os.rename(filename, newfilename)#this change dirs so,we have to walk this Route Again WalkHere(newfilename)
将当前目录文件名转化为大写
for n in * ; do mv $n `echo $n | tr '[:lower:]' '[:upper:]'`; done