编辑:好吧,对不起,我应该指定我在Windows上,并使用基于bash 1.14.2的win-bash以及gnuwin32工具。这意味着所有发布的解决方案都不起作用。它不包含许多高级功能。然而,我终于明白了。这是一个丑陋的脚本,但它的作品。递归BASH重命名
#/bin/bash
function readdir
{
cd "$1"
for infile in *
do
if [ -d "$infile" ]; then
readdir "$infile"
else
renamer "$infile"
fi
done
cd ..
}
function renamer
{
#replace " - " with a single underscore.
NEWFILE1=`echo "$1" | sed 's/\s-\s/_/g'`
#replace spaces with underscores
NEWFILE2=`echo "$NEWFILE1" | sed 's/\s/_/g'`
#replace "-" dashes with underscores.
NEWFILE3=`echo "$NEWFILE2" | sed 's/-/_/g'`
#remove exclamation points
NEWFILE4=`echo "$NEWFILE3" | sed 's/!//g'`
#remove commas
NEWFILE5=`echo "$NEWFILE4" | sed 's/,//g'`
#remove single quotes
NEWFILE6=`echo "$NEWFILE5" | sed "s/'//g"`
#replace & with _and_
NEWFILE7=`echo "$NEWFILE6" | sed "s/&/_and_/g"`
#remove single quotes
NEWFILE8=`echo "$NEWFILE7" | sed "s/’//g"`
mv "$1" "$NEWFILE8"
}
for infile in *
do
if [ -d "$infile" ]; then
readdir "$infile"
else
renamer "$infile"
fi
done
ls
我想创建一个bash脚本来递归通过一个目录和重命名文件,以删除空格,破折号和其他字符。我已经得到了我所需要的脚本,除了它的递归部分。我仍然对此感到陌生,所以它不像应该那样高效,但它很有效。任何人都知道如何做这个递归?
#/bin/bash
for infile in *.*;
do
#replace " - " with a single underscore.
NEWFILE1=`echo $infile | sed 's/\s-\s/_/g'`;
#replace spaces with underscores
NEWFILE2=`echo $NEWFILE1 | sed 's/\s/_/g'`;
#replace "-" dashes with underscores.
NEWFILE3=`echo $NEWFILE2 | sed 's/-/_/g'`;
#remove exclamation points
NEWFILE4=`echo $NEWFILE3 | sed 's/!//g'`;
#remove commas
NEWFILE5=`echo $NEWFILE4 | sed 's/,//g'`;
mv "$infile" "$NEWFILE5";
done;
我觉得它比计算器更多的超级用户,但如果它只是为了解决一个特定的Linux操作系统中的东西我会使用查找和重命名。 – Fredrik 2010-11-22 20:49:49
如果您使用`*。*`,则不匹配不包含“`。”“的文件。在Bash中,仅仅使用`*` – 2010-11-22 21:27:56