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


当前回答

for f in `find -depth`; do mv ${f} ${f,,} ; done

Find -depth打印每个文件和目录,目录的内容在目录本身之前打印。${f,,}将文件名小写。

其他回答

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。

这里的解决方案都不适合我,因为我所在的系统无法访问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上看到类似问题“将整个目录树更改为小写名称”的答案。

最初的问题要求忽略SVN和CVS目录,这可以通过在find命令中添加-prune来实现。例如忽略CVS:

find . -name CVS -prune -o -exec mv '{}' `echo {} | tr '[A-Z]' '[a-z]'` \; -print

[编辑]我尝试了一下,在find中嵌入小写翻译并没有起作用,原因我实际上不明白。因此,将其修改为:

$> cat > tolower
#!/bin/bash
mv $1 `echo $1 | tr '[:upper:]' '[:lower:]'`
^D
$> chmod u+x tolower 
$> find . -name CVS -prune -o -exec tolower '{}'  \;

Ian

这是一个小的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动作。

冗长但“没有惊喜和装置的作品”

This script handles filenames with spaces, quotes, other unusual characters and Unicode, works on case insensitive filesystems and most Unix-y environments that have bash and awk installed (i.e. almost all). It also reports collisions if any (leaving the filename in uppercase) and of course renames both files & directories and works recursively. Finally it's highly adaptable: you can tweak the find command to target the files/dirs you wish and you can tweak awk to do other name manipulations. Note that by "handles Unicode" I mean that it will indeed convert their case (not ignore them like answers that use tr).

# adapt the following command _IF_ you want to deal with specific files/dirs
find . -depth -mindepth 1 -exec bash -c '
  for file do
    # adapt the awk command if you wish to rename to something other than lowercase
    newname=$(dirname "$file")/$(basename "$file" | awk "{print tolower(\$0)}")
    if [ "$file" != "$newname" ] ; then
        # the extra step with the temp filename is for case-insensitive filesystems
        if [ ! -e "$newname" ] && [ ! -e "$newname.lcrnm.tmp" ] ; then
           mv -T "$file" "$newname.lcrnm.tmp" && mv -T "$newname.lcrnm.tmp" "$newname" 
        else
           echo "ERROR: Name already exists: $newname"
        fi
    fi    
  done
' sh {} +

参考文献

我的剧本是基于以下这些精彩的回答:

https://unix.stackexchange.com/questions/9496/looping-through-files-with-spaces-in-the-names

如何在Bash中转换字符串为小写?