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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7787029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 21:07:13  来源:igfitidea点击:

How do I rename all files to lowercase?

macosfilebash

提问by Voloda2

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?

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

回答by wjl

If you're comfortable with the terminal:

如果您对终端感到满意:

  1. Open Terminal.app, type cdand 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 lsand 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 lsand hit enter again.
  1. 打开 Terminal.app,输入cd包含要重命名的文件的文件夹,然后将其拖放到窗口中。
  2. 要确认您位于正确的目录中,请键入ls并按 Enter。
  3. 粘贴此代码并按回车键:

    for f in *; do mv "$f" "$f.tmp"; mv "$f.tmp" "`echo $f | tr "[:upper:]" "[:lower:]"`"; done
    
  4. 要确认您的所有文件都是小写的,请ls再次键入并按 Enter。

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

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

回答by Alex Harvey

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):

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

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

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

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

IFS=$'\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):

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

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

Note about Mac OS X:

关于 Mac OS X 的注意事项:

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

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

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.

但是,正如接受的答案和评论中所建议的那样,不需要创建临时文件,因为首先不能存在两个仅因大小写不同的文件名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

回答by arsenius

A fish shell version:

一个鱼壳版本:

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