开发者

How to concatenate replace filenames

I need to write a script that will automatically rename a number of files and include a user defined string. As an example, I want the file *001.jpg renamed to "user defined string" + 001.jpg (ex: MyVacation20110725_001.jpg).

This is what I've tried so far:

#!/bin/sh

echo "Type t开发者_StackOverflowhe user-defined string, followed by [Enter]: "
read str
for file in *.jpg; do 
`sed s/$str_$file/$file/`;
done

The problem is that nothing is happening.


You can try this on your loop:

for file in *.jpg
do
mv ${file} $file$str
done


Some changes in your script.see below:

#!/bin/sh

echo "Type the user-defined string,followed byt[Enter]: "
read str
for file in *.jpg; do 
new_name=`echo ${str}_${file}`;
mv ${file} ${new_name}
done


Something like

cnt=1
for file in *.jpg; do
   cname=$(printf "%03d" $cnt)
   mv "${file%.jpg}" "${str}_$cname.jpg"
   ((cnt++))
done

(after the reading str from the user, as you did) would rename from "whatever.jpg" to "myString_NNN.jpg", e.g. the first would be "myString_001.jpg", the second "myString_002.jpg" and so on.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜