如何在Linux上将所有文件夹和文件重命名为小写? [英] How do I rename all folders and files to lowercase on Linux?
问题描述
我必须递归地重命名完整的文件夹树,以使任何地方都没有大写字母(这是C ++源代码,但这没关系).
I have to rename a complete folder tree recursively so that no uppercase letter appears anywhere (it's C++ source code, but that shouldn't matter).
忽略CVS和Subversion版本控制文件/文件夹的奖励点.首选方法是shell脚本,因为shell应该在任何Linux机器上都可用.
Bonus points for ignoring CVS and Subversion version control files/folders. The preferred way would be a shell script, since a shell should be available on any Linux box.
关于文件重命名的细节,有一些有效的论据.
There were some valid arguments about details of the file renaming.
-
我认为具有相同小写字母名称的文件应被覆盖;这是用户的问题.在忽略大小写的文件系统上签出时,它也会用后者覆盖第一个文件.
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.
我会考虑使用A-Z字符并将其转换为a-z,其他所有内容都只是在引发问题(至少在源代码中如此).
I would consider A-Z characters and transform them to a-z, everything else is just calling for problems (at least with source code).
在Linux系统上运行构建将需要该脚本,因此我认为应省略对CVS或Subversion版本控制文件的更改.毕竟,这只是从头开始.也许出口"更合适.
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.
推荐答案
使用"rename"
命令的简洁版本:
find my_root_dir -depth -exec rename 's/(.*)\/([^\/]*)/$1\/\L$2/' {} \;
这避免了在文件之前重命名目录并尝试将文件移至不存在的目录(例如,将"A/A"
移至"a/a"
)的问题.
This avoids problems with directories being renamed before files and trying to move files into non-existing directories (e.g. "A/A"
into "a/a"
).
或者,更详细的版本,而不使用"rename"
.
Or, a more verbose version without using "rename"
.
for SRC in `find my_root_dir -depth`
do
DST=`dirname "${SRC}"`/`basename "${SRC}" | tr '[A-Z]' '[a-z]'`
if [ "${SRC}" != "${DST}" ]
then
[ ! -e "${DST}" ] && mv -T "${SRC}" "${DST}" || echo "${SRC} was not renamed"
fi
done
P.S.
后者使move命令(例如"svn mv"
)具有更大的灵活性.
P.S.
The latter allows more flexibility with the move command (for example, "svn mv"
).
这篇关于如何在Linux上将所有文件夹和文件重命名为小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!