我必须递归地重命名一个完整的文件夹树,这样就不会出现大写字母(这是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.


当前回答

这里的解决方案都不适合我,因为我所在的系统无法访问perl重命名脚本,而且有些文件包含空格。然而,我发现了一个有效的变体:

find . -depth -exec sh -c '
    t=${0%/*}/$(printf %s "${0##*/}" | tr "[:upper:]" "[:lower:]");
    [ "$t" = "$0" ] || mv -i "$0" "$t"
' {} \;

归功于“Gilles 'SO-停止作恶”,在Unix和Linux StackExchange上看到类似问题“将整个目录树更改为小写名称”的答案。

其他回答

上面的大多数答案都很危险,因为它们不处理包含奇数字符的名称。对于这种情况,最安全的方法是使用find的-print0选项,该选项将以ASCII NUL而不是\n终止文件名。

下面是一个脚本,它只改变文件而不改变目录名,以免混淆find:

find .  -type f -print0 | xargs -0n 1 bash -c \
's=$(dirname "$0")/$(basename "$0");
d=$(dirname "$0")/$(basename "$0"|tr "[A-Z]" "[a-z]"); mv -f "$s" "$d"'

我对它进行了测试,它适用于包含空格、各种引号等的文件名。这很重要,因为如果您以根用户身份运行树中的其他脚本,其中包括由

touch \;\ echo\ hacker::0:0:hacker:\$\'\057\'root:\$\'\057\'bin\$\'\057\'bash

... 你猜怎么着……

使用排版:

typeset -l new        # Always lowercase
find $topPoint |      # Not using xargs to make this more readable
  while read old
  do new="$old"       # $new is a lowercase version of $old
     mv "$old" "$new" # Quotes for those annoying embedded spaces
  done

在Windows上,像Git Bash这样的模拟可能会失败,因为Windows在本质上不区分大小写。对于这些,添加一个步骤,mv的文件首先到另一个名称,如“$old.tmp”,然后到$new。

我在Mac OS X上发现的最简单的方法是使用http://plasmasturm.org/code/rename/:上的重命名包

brew install rename
rename --force --lower-case --nows *

——force即使目标文件名已经存在,也要重命名。 ——lower-case将文件名全部转换为小写。 替换文件名中所有空格序列为单个下划线字符。

这适用于CentOS/Red Hat Linux或其他发行版,无需重命名Perl脚本:

for i in $( ls | grep [A-Z] ); do mv -i "$i" "`echo $i | tr 'A-Z' 'a-z'`"; done

源:重命名所有文件名称从大写到小写字符

(在某些发行版中,默认的重命名命令来自util-linux,这是一个不同的、不兼容的工具。)

这里的解决方案都不适合我,因为我所在的系统无法访问perl重命名脚本,而且有些文件包含空格。然而,我发现了一个有效的变体:

find . -depth -exec sh -c '
    t=${0%/*}/$(printf %s "${0##*/}" | tr "[:upper:]" "[:lower:]");
    [ "$t" = "$0" ] || mv -i "$0" "$t"
' {} \;

归功于“Gilles 'SO-停止作恶”,在Unix和Linux StackExchange上看到类似问题“将整个目录树更改为小写名称”的答案。