我正在尝试编写一个bourne-shell脚本,该脚本以目录为参数,并查找名为ixxx.a的图像,并将它们重命名为ixxx_a.img,其中"xxx表示示例图像文件的扩展名为i001.a、i002.a、i003.a .)
mv $1/f[0-9][0-9][0-9].a $1/f[0-9][0-9][0-9]_a.img但上面写的不是目录。任何帮助都将不胜感激。谢谢。
发布于 2012-03-02 03:57:37
for i in $1/f[0-9][0-9][0-9].a; do
mv $i ${i%.a}_a.img
done但是,这不考虑文件/文件夹名称中的空格。在这种情况下,您必须使用while,这样每行就可以得到一个文件名(请参见下面的奖金)。可能还有很多其他的方法,包括rename。
find $1 -maxdepth 1 -type f -name "f[0-9][0-9][0-9].a"|while read i; do
mv "$i" "${i%.a}_a.img"
done编辑:,也许我应该解释一下我在那里做了什么。它称为字符串替换,主要用例是用于变量var的
# Get first two characters
${var:0:2}
# Remove shortest rear-anchored pattern - this one would give the directory name of a file, for example
${var%/*}
# Remove longest rear-anchored pattern
${var%%/*}
# Remove shortest front-anchored pattern - this in particular removes a leading slash
${var#/}
# Remove longest front-anchored pattern - this would remove all but the base name of a file given its path
# Replace a by b
${var//a/b}
${var##*/}有关更多信息,请参见man页面。
https://stackoverflow.com/questions/9527722
复制相似问题