zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    SCAU 9504 面试
    SCAU 9503 懒人选座位
    SCAU 8628 相亲
    SCAU 10691 ACM 光环
    SCAU 8626 原子量计数
    SCAU 10674 等差对
    HDU ACM 1048 The Hardest Problem Ever (水题)
    SCAU 9502 ARDF
    SCAU 10686 DeathGod不知道的事情
    SCAU 8629 热身游戏(高精度)
  • 原文地址:https://www.cnblogs.com/Mrhuangrui/p/6039635.html
Copyright © 2011-2022 走看看