Bash 重命名文件但将其保留在当前子目录中

Bash 重命名文件但将其保留在当前子目录中,bash,awk,mv,renaming,pwd,Bash,Awk,Mv,Renaming,Pwd,我有一个脚本,可以根据html文件标记中的信息重命名html文件。此脚本遍历当前目录和所有子目录,并递归执行此重命名。但是,在重命名它们之后,它会将它们移动到当前的工作目录中,我正在从中执行shell脚本。如何确保文件保留在其子目录中,并且不移动到工作目录 以下是我的工作内容: #!/usr/bin/env bash for f in `find . -type f | grep \.htm` do title=$( awk 'BEGIN{IGNORECASE=1;FS="<t

我有一个脚本,可以根据html文件标记中的信息重命名html文件。此脚本遍历当前目录和所有子目录,并递归执行此重命名。但是,在重命名它们之后,它会将它们移动到当前的工作目录中,我正在从中执行shell脚本。如何确保文件保留在其子目录中,并且不移动到工作目录

以下是我的工作内容:

#!/usr/bin/env bash
for f in `find . -type f | grep \.htm`
   do
   title=$( awk 'BEGIN{IGNORECASE=1;FS="<title>|</title>";RS=EOF} {print $2}' "$f" )
   mv  ./"$f" "${title//[ ]/-}".htm
done
#/usr/bin/env bash
对于f in`find-类型f | grep\.htm`
做
title=$(awk'BEGIN{IGNORECASE=1;FS=“|”;RS=EOF}{print$2}'”$f)
mv./“$f”“${title/[]/-}”.htm
完成
试试:

请注意,您在\find…“中对f的
将在任何文件名中包含空格或CR时失败。您可以通过以下方式避免这种情况:

find . -type f -name '*.htm' -type f -exec myrename.sh {} \;
如果重命名代码位于名为
myrename.sh的脚本中,请尝试:

shopt -s globstar nullglob
for f in **/*.htm
do
   title=$( awk 'BEGIN{IGNORECASE=1;FS="<title>|</title>";RS=EOF} {print $2}' "$f" )
   mv  "$f" "$(dirname "$f")/${title//[ ]/-}".htm
done
请注意,您在\
find…“中对f的
将在任何文件名中包含空格或CR时失败。您可以通过以下方式避免这种情况:

find . -type f -name '*.htm' -type f -exec myrename.sh {} \;
其中重命名代码位于名为
myrename.sh

shopt-s globstar nullglob的脚本中
shopt -s globstar nullglob
for f in **/*.htm
do
   title=$( awk 'BEGIN{IGNORECASE=1;FS="<title>|</title>";RS=EOF} {print $2}' "$f" )
   mv  "$f" "$(dirname "$f")/${title//[ ]/-}".htm
done
对于**/*.htm中的f 做 title=$(awk'BEGIN{IGNORECASE=1;FS=“|”;RS=EOF}{print$2}'”$f) mv“$f”“$(dirname“$f”)/${title/[]/-}.htm 完成
shopt-s globstar nullglob
对于**/*.htm中的f
做
title=$(awk'BEGIN{IGNORECASE=1;FS=“|”;RS=EOF}{print$2}'”$f)
mv“$f”“$(dirname“$f”)/${title/[]/-}.htm
完成

切勿使用此结构:

for f in `find . -type f | grep \.htm`
因为循环对于包含空格的文件名失败,并且grep不必要,因为find有一个-name选项。改用这个:

find . -type f -name '*\.htm.*' -print |
while IFS= read -r f
这:


切勿使用此构造:

for f in `find . -type f | grep \.htm`
因为循环对于包含空格的文件名失败,并且grep不必要,因为find有一个-name选项。改用这个:

find . -type f -name '*\.htm.*' -print |
while IFS= read -r f
这: