• Linux 遍历目录下面所有文件,将目录名、文件名转为小写


    当你从 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)
    Python

    修改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)
    python

     将当前目录文件名转化为大写
    for n in * ; do mv $n `echo $n | tr '[:lower:]' '[:upper:]'`; done

  • 相关阅读:
    HashTable、HashSet和Dictionary的区别
    CCF_ 201312-3_最大的矩形
    CCF_ 201312-2_ISBN号码
    CCF_201312-1_出现次数最多的数
    CCF_ 201509-2_日期计算
    CCF_ 201512-3_画图
    CCF_ 201512-2_消除类游戏
    CCF_ 201409-2_画图
    CCF_201409-1_相邻数对
    CCF_ 201412-1_门禁系统
  • 原文地址:https://www.cnblogs.com/Mrhuangrui/p/6039635.html
Copyright © 2020-2023  润新知