shell - UNIX 将文件/目录重命名为大写

标签 shell

我正在尝试使用 shell 脚本将所有目录和文件重命名为大写。我有什么作品,但不适用于子目录。由于在脚本执行期间目录名称发生变化,我得到类似 mv: cannot stat './def/two/three': No such file or directory

我已尝试将 -depth 与 find 一起使用,以便它从下往上重命名。但仍然遇到同样的问题。我想使用 cut 来分解 / 上的路径并以这种方式重命名,但不知所措。

这是我所拥有的:

for i in `find . -name "*[a-z]*"`
    do new_name=`echo $i | tr '[a-z]' '[A-Z]'`
    mv $i $new_name
done

我会很感激任何方向,因为我觉得这应该是一项常见的任务,但未能从一些 Google 搜索中找到可行的解决方案。

请注意,我不能使用 rename,因为我的发行版不支持它。

最佳答案

试试这个方法:

find . -depth |while read LONG; do SHORT=$( basename "$LONG" | tr '[:lower:]' '[:upper:]' ); DIR=$( dirname "$LONG" ); if [ "${LONG}" != "${DIR}/${SHORT}"  ]; then mv "${LONG}" "${DIR}/${SHORT}" ; fi; done

或者,如果您想要可读版本(没有单行):

find . -depth | \
while read LONG; do
   SHORT=$( basename "$LONG" | tr '[:lower:]' '[:upper:]' )
   DIR=$( dirname "$LONG" )
   if [ "${LONG}" != "${DIR}/${SHORT}"  ]; then
     mv "${LONG}" "${DIR}/${SHORT}"
   fi
done

这将按照正确的顺序重命名文件,然后是它们所在的目录。

关于shell - UNIX 将文件/目录重命名为大写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4268591/

相关文章:

shell - 如何使用 shell 脚本的输出动态更新网页?

python - 无法在 python 中解码 AWS Session Manager websocket 输出

shell - 查找选项 -cmin 和 -amin 之间有什么区别?

linux - Linux脚本中两个$有什么区别

macos - -bash : : command not found when launching the shell in Mac OSX

linux - 如何将文件夹中的文件列表发送到Linux中的txt文件

linux - Bash 脚本 - 显示目录中文件和文件夹的数量

shell - 如何重定向 shell 脚本所有输出的副本?

shell - gnome 更新管理器配置文件存储在哪里?

linux - 如果使用相同 IP/端口的连接断开,则 Shell 脚本警报