我正在尝试使用 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/