我必须递归地重命名一个完整的文件夹树,这样就不会出现大写字母(这是c++源代码,但这无关紧要)。

忽略CVS和Subversion版本控制文件/文件夹的加分项。首选的方法是shell脚本,因为shell应该在任何Linux机器上可用。

关于文件重命名的细节有一些有效的争论。

I think files with the same lowercase names should be overwritten; it's the user's problem. When checked out on a case-ignoring file system, it would overwrite the first one with the latter, too. I would consider A-Z characters and transform them to a-z, everything else is just calling for problems (at least with source code). The script would be needed to run a build on a Linux system, so I think changes to CVS or Subversion version control files should be omitted. After all, it's just a scratch checkout. Maybe an "export" is more appropriate.


当前回答

这在macOS上也能很好地工作:

ruby -e "Dir['*'].each { |p| File.rename(p, p.downcase) }"

其他回答

在OS X中,mv -f显示“same file”错误,所以我重命名两次:

for i in `find . -name "*" -type f |grep -e "[A-Z]"`; do j=`echo $i | tr '[A-Z]' '[a-z]' | sed s/\-1$//`; mv $i $i-1; mv $i-1 $j; done

使用bash,不重命名:

find . -exec bash -c 'mv $0 ${0,,}' {} \;

如果你已经设置了重命名命令(例如,在Mac中通过brew install),这是有效的:

rename --lower-case --force somedir/*

在这种情况下,我会使用Python,以避免乐观地假设没有空格或斜杠的路径。我还发现python2往往被安装在更多的地方,而不是重命名。

#!/usr/bin/env python2
import sys, os

def rename_dir(directory):
  print('DEBUG: rename('+directory+')')

  # Rename current directory if needed
  os.rename(directory, directory.lower())
  directory = directory.lower()

  # Rename children
  for fn in os.listdir(directory):
    path = os.path.join(directory, fn)
    os.rename(path, path.lower())
    path = path.lower()

    # Rename children within, if this child is a directory
    if os.path.isdir(path):
        rename_dir(path)

# Run program, using the first argument passed to this Python script as the name of the folder
rename_dir(sys.argv[1])

这在macOS上也能很好地工作:

ruby -e "Dir['*'].each { |p| File.rename(p, p.downcase) }"