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


当前回答

find . -depth -name '*[A-Z]*'|sed -n 's/\(.*\/\)\(.*\)/mv -n -v -T \1\2 \1\L\2/p'|sh

我没有尝试过这里提到的更详细的脚本,但是在我的Synology NAS上,没有一个命令行版本适合我。rename是不可用的,并且find的许多变体失败,因为它似乎坚持已经重命名的路径的旧名称(例如,如果它找到。/FOO后面跟着。/FOO/BAR,重命名。/FOO到。/FOO仍然会继续列出。/FOO/BAR,即使该路径不再有效)。以上命令对我来说没有任何问题。

下面是该命令每个部分的解释:


find . -depth -name '*[A-Z]*'

这将找到当前目录中的任何文件(更改。到您想要处理的任何目录),使用深度优先搜索(例如。,它会在./foo之前列出./foo/bar),但只针对包含大写字符的文件。name筛选器只应用于基本文件名,而不应用于完整路径。这里会列出。/FOO/BAR而不是。/FOO/BAR。这是可以的,因为我们不想重命名。/FOO/bar。虽然我们想重命名./FOO,但后面会列出这个(这就是-depth很重要的原因)。

这个命令本身对于首先查找要重命名的文件特别有用。在完全重命名命令之后使用此命令可以搜索由于文件名冲突或错误而仍然没有被替换的文件。


sed -n 's/\(.*\/\)\(.*\)/mv -n -v -T \1\2 \1\L\2/p'

该部分读取find命令输出的文件,并使用正则表达式在mv命令中格式化它们。-n选项停止sed打印输入,搜索和替换正则表达式中的p命令输出替换后的文本。

The regex itself consists of two captures: the part up until the last / (which is the directory of the file), and the filename itself. The directory is left intact, but the filename is transformed to lowercase. So, if find outputs ./FOO/BAR, it will become mv -n -v -T ./FOO/BAR ./FOO/bar. The -n option of mv makes sure existing lowercase files are not overwritten. The -v option makes mv output every change that it makes (or doesn't make - if ./FOO/bar already exists, it outputs something like ./FOO/BAR -> ./FOO/BAR, noting that no change has been made). The -T is very important here - it treats the target file as a directory. This will make sure that ./FOO/BAR isn't moved into ./FOO/bar if that directory happens to exist.

使用此命令和find命令一起生成将执行的命令列表(可以方便地验证将执行什么而无需实际执行)


sh

这是不言自明的。它将所有生成的mv命令路由到shell解释器。您可以将其替换为bash或任何您喜欢的shell。

其他回答

这是一个小的shell脚本,做你所要求的:

root_directory="${1?-please specify parent directory}"
do_it () {
    awk '{ lc= tolower($0); if (lc != $0) print "mv \""  $0 "\" \"" lc "\"" }' | sh
}
# first the folders
find "$root_directory" -depth -type d | do_it
find "$root_directory" ! -type d | do_it

注意第一个find中的-depth动作。

一行程序:

for F in K*; do NEWNAME=$(echo "$F" | tr '[:upper:]' '[:lower:]'); mv "$F" "$NEWNAME"; done

甚至:

for F in K*; do mv "$F" "${F,,}"; done

注意,这将只转换以字母K开头的文件/目录,因此要相应地进行调整。

我需要在Windows 7上的Cygwin设置上这样做,并发现我在上面尝试的建议中出现了语法错误(尽管我可能错过了一个工作选项)。然而,这个解决方案直接来自Ubuntu论坛:-)

ls | while read upName; do loName=`echo "${upName}" | tr '[:upper:]' '[:lower:]'`; mv "$upName" "$loName"; done

(注意:我之前用下划线替换了空格:

for f in *\ *; do mv "$f" "${f// /_}"; done

)

这适用于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,这是一个不同的、不兼容的工具。)

在这种情况下,我会使用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])