如何在 Linux 上将所有文件夹和文件重命名为小写? [英] How do I rename all folders and files to lowercase on Linux?

查看:30
本文介绍了如何在 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.

  1. 我认为应该覆盖具有相同小写名称的文件;这是用户的问题.在忽略大小写的文件系统上签出时,它也会用后者覆盖第一个.

  1. 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" 命令的简洁版本:

A concise version using the "rename" command:

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

附注

后者为移动命令提供了更大的灵活性(例如,"svn mv").

这篇关于如何在 Linux 上将所有文件夹和文件重命名为小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆