如何更改 child 目录中的文件名

How can I change the name of file in the child's directory

有一个 child 目录,其名称存储在名为 temp 的变量中。 我想将该目录中的所有文件重命名为小写版本。

所以我写了这段代码:

mv ls $temp 'ls $temp | tr [:upper:][:lower:] [:lower:][:upper:]'

但它不起作用。我该如何更改它?

你需要一个循环。

您可以使用 Bash brace expansion 转换为小写,而不是 tr 每次都会创建一个额外的进程:

#!/bin/bash
cd "$temp"
for f in *; do
  mv "$f" "${f,,}"
done

如果你想反转文件名中每个字符的大小写(感谢@SLePort 的提示):

#!/bin/bash
cd "$temp"
for f in *; do
  mv "$f" "${f~~}"
done