gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:07:50 25 4
gpt4 key购买 nike

有一个子目录,其名称存储在名为 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

关于linux - 如何更改 child 目录中的文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43025579/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com