回答#1:我最终从头开始,我能够拼凑出一些有效的东西!可能不是最有效率的,但它确实有效。
#!/bin/bash
for file in *.php
do
#Check to see if the filename contains any uppercase characters
iscap=`echo $file | awk '{if ($0 ~ /[[:upper:]]/) print }'`
if [[ -n $iscap ]]
then
#If the filename contains upper case characters convert them to lower case
newname=`echo $file | tr '[A-Z]' '[a-z]'` #make lower case
#Perform various search/replaces on the file name to clean things up
newname=$(echo "$newname" | sed 's/---/-/')
newname=$(echo "$newname" | sed 's/--/-/')
newname=$(echo "$newname" | sed 's/-\./\./')
newname=$(echo "$newname" | sed 's/there-s-/theres-/')
#Rename file
echo "Moving $file\n To $newname\n\n"
mv $file $newname
#Update all references to the new filename in all php files
`sed -i "s/$file/$newname/g" *.php`
fi
done
回答#2:虽然它没有检查大写字符,只是尝试转换所有内容(对已经小写的文件发出错误),如果你知道你只是要处理大写字符的文件,您可以使用 aqua 提交的版本。
#!/bin/bash
for file in *.php
do
newname=`echo $file | tr '[A-Z]' '[a-z]'`
newname=$(echo "$newname" | sed 's/---/-/')
newname=$(echo "$newname" | sed 's/--/-/')
newname=$(echo "$newname" | sed 's/-\./\./')
newname=$(echo "$newname" | sed 's/there-s-/theres-/')
mv "$file" "$newname"
for f in *.php
do
sed -i "s/$file/$newname/g" *.php
done
done
感谢大家的帮助!
原始问题&其他信息
我正在使用一些软件为开源软件项目创建文档,除了它输出带有大写字符的HTML文件,不推荐用于SEO。所以,我正在尝试编写一个bash脚本,将所有.html文件更改为小写,然后搜索/替换
所以,我想转此:
此-IS-THE-Output.html
进入这个:
此-是最output.html
然后搜索当前目录中的所有文件“This-Is-The-Output.html”并将其替换为“this-is-the-output.html”。
我一直在尝试根据我在网上发现的内容拼凑一些东西,但我无法让它发挥作用。我对于编写bash脚本缺乏经验,因为有人可以,所以我想我会转向SO看看是否有人可以提供帮助。
这就是我目前正在使用的......
#!/bin/sh
for i in *.php;
icleaned=`echo $i | sed s/./\\./g`;
inew="$(tr [A-Z] [a-z] <<< "$icleaned")";
sed -i 's/$i/$inew/g' *.php;
do mv $i $inew;
done
第3行:搜索并替换特殊字符,例如“。”
第4行:将干净的小写字符串分配给$ inew
第5行:搜索/替换旧文件名的所有引用到新文件名
第6行:将文件从旧文件名移动到新文件名
我得到的错误是:
line 3: syntax error near unexpected token `icleaned=`echo $i | sed s/./\\./g`'
line 3: ` icleaned=`echo $i | sed s/./\\./g`;'
我尝试回复icleaned以查看第一步是否正常工作,但我得到完全相同的错误。
我甚至尝试评论除了inew = line之外的所有内容:
#!/bin/sh
for i in *.php;
# icleaned=`echo $i | sed s/./\\./g`;
inew="$(tr [A-Z] [a-z] <<< "$i")";
echo $inew;
# sed -i 's/$i/$inew/g' *.php;
# do mv $i $inew;
done
我仍然收到“意外令牌附近的语法错误”错误。
line 4: syntax error near unexpected token `inew="$(tr [A-Z] [a-z] <<< "$i")"'
line 4: ` inew="$(tr [A-Z] [a-z] <<< "$i")";'
很明显,脚本在很多方面都搞砸了。
非常感谢任何人提供的帮助。
答案 0 :(得分:3)
根据@JS的评论更新为使用-exec:
find . -name '*[A-Z]*' -type f -exec bash -c 'echo "{}" | mv "{}" "$(tr A-Z a-z)"' \;
如果您安装了正确的版本,也可以尝试重命名。检查您的手册页以进行重命名:
rename 'y/A-Z/a-z/' *
带有mv的单行代码,仅使用find来搜索和重命名文件名为大写的文件。
for i in $(find . -name '*[A-Z]*' -type f); do mv "$i" "$(echo $i|tr A-Z a-z)"; done
答案 1 :(得分:1)
这样的事情应该有效:
#!/bin/bash
for file in *.html
do
lowercase=`echo $file | tr '[A-Z]' '[a-z]'`
mv "$file" "$lowercase"
for f in *.html
do
sed -i "s/$file/$lowercase/g" "$f"
done
done
如果您使用的是html文件以外的其他内容,请将*.html
更改为*.<extension>
。
答案 2 :(得分:0)
这确实是OP在做了一些工作后得出的答案,但我想我会把它分开来,所以它不会弄乱这个问题。
我最终从零开始,我能够拼凑出一些有效的东西!可能不是最有效率的,但它确实有效。
#!/bin/bash
for file in *.php
do
#Check to see if the filename contains any uppercase characters
iscap=`echo $file | awk '{if ($0 ~ /[[:upper:]]/) print }'`
if [[ -n $iscap ]]
then
#If the filename contains upper case characters convert them to lower case
newname=`echo $file | tr '[A-Z]' '[a-z]'` #make lower case
#Perform various search/replaces on the file name to clean things up
newname=$(echo "$newname" | sed 's/---/-/')
newname=$(echo "$newname" | sed 's/--/-/')
newname=$(echo "$newname" | sed 's/-\./\./')
newname=$(echo "$newname" | sed 's/there-s-/theres-/')
#Rename file
echo "Moving $file\n To $newname\n\n"
mv $file $newname
#Update all references to the new filename in all php files
`sed -i "s/$file/$newname/g" *.php`
fi
done
虽然它没有检查大写字符并且只是尝试转换所有内容(在已经小写的文件上给出错误),如果你知道你只是要处理带有大写字符的文件你可以使用 aqua 提交的版本。
#!/bin/bash
for file in *.php
do
newname=`echo $file | tr '[A-Z]' '[a-z]'`
newname=$(echo "$newname" | sed 's/---/-/')
newname=$(echo "$newname" | sed 's/--/-/')
newname=$(echo "$newname" | sed 's/-\./\./')
newname=$(echo "$newname" | sed 's/there-s-/theres-/')
mv "$file" "$newname"
for f in *.php
do
sed -i "s/$file/$newname/g" *.php
done
done
感谢大家的帮助!