重命名所有二级子目录中具有特定名称的所有文件
Rename all files of a certain name within all second-level sub-directories
我的目标是自动执行以下过程:在所有二级子目录中搜索,并为所述子目录中名为“Test.pptx”的所有文件重命名为“Test - Appended.pptx”。根据我在 Whosebug 上看到的其他问题的回复,我尝试了以下代码:
for D in *; do
if [ -d "${D}" ]; then
echo "${D}"
for E in "${D}"; do
if [ -d "${E} ]; then
echo "${E}"
for f in "Test.pptx"; do mv "$f" "Test - Appended.pptx"; done
fi
done
fi
done
我设置了脚本可执行文件(使用 chmod +x
)并 运行 它,但出现以下错误:
line 7: unexpected EOF while looking for matching `"'
line 12: syntax error: unexpected end of file
我是 Bash 脚本的新手,所以如果能帮助我诊断错误并实现最初的目标,我将不胜感激。谢谢!
使用find
:
while read -r pptx
do
mv -n "${pptx}" "${pptx%.pptx} - Appended.pptx"
done < <( find . -mindepth 3 -maxdepth 3 -type f -name "*.pptx" )
请注意,我没有对其进行测试,它可能需要根据您的特殊情况进行调整。
只要在 mv
中设置了 -n
选项,它就不会覆盖任何内容。
sub-loops 不需要。
for f in */*/Test.pptx; do mv "$f" "${f%/*}/Test - Appended.pptx"; done
${f%/*}
是当前文件的完整路径,从最后一个斜杠 (/
) 向前剥离,所以如果文件是 a/b/Test.pptx
那么 ${f%/*}
是a/b
.
我的目标是自动执行以下过程:在所有二级子目录中搜索,并为所述子目录中名为“Test.pptx”的所有文件重命名为“Test - Appended.pptx”。根据我在 Whosebug 上看到的其他问题的回复,我尝试了以下代码:
for D in *; do
if [ -d "${D}" ]; then
echo "${D}"
for E in "${D}"; do
if [ -d "${E} ]; then
echo "${E}"
for f in "Test.pptx"; do mv "$f" "Test - Appended.pptx"; done
fi
done
fi
done
我设置了脚本可执行文件(使用 chmod +x
)并 运行 它,但出现以下错误:
line 7: unexpected EOF while looking for matching `"'
line 12: syntax error: unexpected end of file
我是 Bash 脚本的新手,所以如果能帮助我诊断错误并实现最初的目标,我将不胜感激。谢谢!
使用find
:
while read -r pptx
do
mv -n "${pptx}" "${pptx%.pptx} - Appended.pptx"
done < <( find . -mindepth 3 -maxdepth 3 -type f -name "*.pptx" )
请注意,我没有对其进行测试,它可能需要根据您的特殊情况进行调整。
只要在 mv
中设置了 -n
选项,它就不会覆盖任何内容。
sub-loops 不需要。
for f in */*/Test.pptx; do mv "$f" "${f%/*}/Test - Appended.pptx"; done
${f%/*}
是当前文件的完整路径,从最后一个斜杠 (/
) 向前剥离,所以如果文件是 a/b/Test.pptx
那么 ${f%/*}
是a/b
.