如何将所有文件重命名为小写?

发布于 2024-12-10 09:18:54 字数 69 浏览 0 评论 0原文

例如,我有 TREE.wav、ONE.WAV。我想将其重命名为tree.wav、one.wav。如何将所有文件重命名为小写?

I have for example TREE.wav, ONE.WAV. I want to rename it to tree.wav, one.wav. How do I rename all files to lowercase?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

西瑶 2024-12-17 09:18:54

如果您对终端感到满意:

  1. 打开 Terminal.app,输入 cd,然后将包含要重命名的文件的文件夹拖放到窗口中。
  2. 要确认您位于正确的目录中,请输入 ls 并按 Enter 键。
  3. 粘贴此代码并按 Enter 键:

    for f in *;执行 mv "$f" "$f.tmp"; mv "$f.tmp" "`echo $f | tr "[:upper:]" "[:lower:]"`";完毕
    
  4. 要确认所有文件均为小写,请输入 ls 并再次按 Enter 键。

(感谢 Twitter 上的 @bavarious 进行了一些修复,并感谢下面的 John Whitley 使这在不区分大小写的文件系统上变得更安全。)

If you're comfortable with the terminal:

  1. Open Terminal.app, type cd and then drag and drop the Folder containing the files to be renamed into the window.
  2. To confirm you're in the correct directory, type ls and hit enter.
  3. Paste this code and hit enter:

    for f in *; do mv "$f" "$f.tmp"; mv "$f.tmp" "`echo $f | tr "[:upper:]" "[:lower:]"`"; done
    
  4. To confirm that all your files are lowercased, type ls and hit enter again.

(Thanks to @bavarious on twitter for a few fixes, and thanks to John Whitley below for making this safer on case-insensitive filesystems.)

会傲 2024-12-17 09:18:54

所提出的问题是一般性的,也很重要,所以我希望提供一个更一般性的答案:

最简单的情况(大多数时候是安全的,在 Mac OS X 上,但请继续阅读):

for i in * ; do j=$(tr '[:upper:]' '[:lower:]' <<< "$i") ; mv "$i" "$j" ; done

您还需要处理文件名中的空格(任何操作系统):

IFS=

您需要在区分大小写的文件系统中安全地处理仅大小写不同的文件名,而不是覆盖目标(例如Linux):

for i in * ; do j=$(tr '[:upper:]' '[:lower:]' <<< "$i") ; [ -e "$j" ] && continue ; mv "$i" "$j" ; done 

关于Mac OS X的注意事项:

Mac的文件系统不区分大小写,保留大小写。

但是,不需要创建临时文件,正如已接受的答案和评论中所建议的那样,因为两个仅大小写不同的文件名首先不能存在, 参考

为了展示这一点:

$ mkdir test
$ cd test
$ touch X x
$ ls -l 
total 0
-rw-r--r--  1 alexharvey  wheel  0 26 Sep 20:20 X
$ mv X x
$ ls -l 
total 0
-rw-r--r--  1 alexharvey  wheel  0 26 Sep 20:20 x
\n' ; for i in * ; do j=$(tr '[:upper:]' '[:lower:]' <<< "$i") ; mv "$i" "$j" ; done

您需要在区分大小写的文件系统中安全地处理仅大小写不同的文件名,而不是覆盖目标(例如Linux):

关于Mac OS X的注意事项:

Mac的文件系统不区分大小写,保留大小写。

但是,不需要创建临时文件,正如已接受的答案和评论中所建议的那样,因为两个仅大小写不同的文件名首先不能存在, 参考

为了展示这一点:

The question as-asked is general, and also important, so I wish to provide a more general answer:

Simplest case (safe most of the time, and on Mac OS X, but read on):

for i in * ; do j=$(tr '[:upper:]' '[:lower:]' <<< "$i") ; mv "$i" "$j" ; done

You need to also handle spaces in filenames (any OS):

IFS=

You need to safely handle filenames that differ only by case in a case-sensitive filesystem and not overwrite the target (e.g. Linux):

for i in * ; do j=$(tr '[:upper:]' '[:lower:]' <<< "$i") ; [ -e "$j" ] && continue ; mv "$i" "$j" ; done 

Note about Mac OS X:

Mac's filesystem is case-insensitive, case-preserving.

There is, however, no need to create temporary files, as suggested in the accepted answer and comments, because two filenames that differ only by case cannot exist in the first place, ref.

To show this:

$ mkdir test
$ cd test
$ touch X x
$ ls -l 
total 0
-rw-r--r--  1 alexharvey  wheel  0 26 Sep 20:20 X
$ mv X x
$ ls -l 
total 0
-rw-r--r--  1 alexharvey  wheel  0 26 Sep 20:20 x
\n' ; for i in * ; do j=$(tr '[:upper:]' '[:lower:]' <<< "$i") ; mv "$i" "$j" ; done

You need to safely handle filenames that differ only by case in a case-sensitive filesystem and not overwrite the target (e.g. Linux):

Note about Mac OS X:

Mac's filesystem is case-insensitive, case-preserving.

There is, however, no need to create temporary files, as suggested in the accepted answer and comments, because two filenames that differ only by case cannot exist in the first place, ref.

To show this:

昔梦 2024-12-17 09:18:54

鱼壳版本:

for old in *
    set new (echo $old | tr '[A-Z]' '[a-z]')
    mv $old $new
end

A fish shell version:

for old in *
    set new (echo $old | tr '[A-Z]' '[a-z]')
    mv $old $new
end
萌逼全场 2024-12-17 09:18:54

对于那些想要将当前目录和子目录中的所有文件小写的用户:

# lower case all files in current dir & subdirs
for d in ./**/ ; do (cd "$d" &&  for x in ./*/ ; do (cd "$x" && for f in *; do mv "$f" "$f.tmp"; mv "$f.tmp" "`echo $f | tr "[:upper:]" "[:lower:]"`"; done); done); done

#list all directories
for f in ./**/ ; do echo $f; done

# lower case all files in a directory
for x in ./*/ ; do (cd "$x" && for f in *; do mv "$f" "$f.tmp"; mv "$f.tmp" "`echo $f | tr "[:upper:]" "[:lower:]"`"; done); done

For those wanting to lowercase all files in the current directory and sub-directories:

# lower case all files in current dir & subdirs
for d in ./**/ ; do (cd "$d" &&  for x in ./*/ ; do (cd "$x" && for f in *; do mv "$f" "$f.tmp"; mv "$f.tmp" "`echo $f | tr "[:upper:]" "[:lower:]"`"; done); done); done

#list all directories
for f in ./**/ ; do echo $f; done

# lower case all files in a directory
for x in ./*/ ; do (cd "$x" && for f in *; do mv "$f" "$f.tmp"; mv "$f.tmp" "`echo $f | tr "[:upper:]" "[:lower:]"`"; done); done
土豪我们做朋友吧 2024-12-17 09:18:54

一个简单的解决方案是在 for 循环中使用 tr 命令。

for file in ./* ; do
    mv "$file" "$(tr '[:upper:]' '[:lower:]' <<<"$file")"
done

您可以在此处了解有关 tr 命令的更多信息tr 命令的说明

A simple solution is by using tr command within a for loop.

for file in ./* ; do
    mv "$file" "$(tr '[:upper:]' '[:lower:]' <<<"$file")"
done

You can learn more about tr command here description of tr command

南七夏 2024-12-17 09:18:54

我认为这是最好的解决方案。

<代码>for i in *;做 mv "$i" "${i,,}";完成

更容易、更短、更整洁

i think this is the best solution.

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

easier, shorter, neater

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文