Wednesday, November 24, 2010

How to use sed to change file name?

How to use sed to change all files name from song1.song to son1.mp3 in the same folderHow to use sed to change file name?If your question contains a typo and you actually

meant moving %26quot;song1.song%26quot; to %26quot;song1.mp3%26quot;, then

you're just changing the suffix and you don't

even need %26quot;sed%26quot;. Instead do something like:



------------------------------

#!/bin/sh



for i in *.song ; do

name=`basename $i .song`

echo $i '==%26gt;' ${name}.mp3

mv $i ${name}.mp3

done

------------------------------



And to use it:



$ doit

song1.song ==%26gt; song1.mp3

song2.song ==%26gt; song2.mp3

song3.song ==%26gt; song3.mp3

song4.song ==%26gt; song4.mp3

song5.song ==%26gt; song5.mp3





If you actually want to remove the %26quot;g%26quot; from %26quot;song%26quot;,

then maybe you really do need to use %26quot;sed%26quot;. The

following script has **no** error checking,

but shows how you might change all files names

containing %26quot;pattern1%26quot; to use %26quot;pattern2%26quot; instead.



------------------------------

#!/bin/sh



src=$1

dst=$2



for i in *${src}* ; do

name=`echo $i | sed %26quot;s/$src/$dst/%26quot;`

echo $i '==%26gt;' $name

mv $i $name

done

------------------------------



$ mvpat song son

song1.mp3 ==%26gt; son1.mp3

song2.mp3 ==%26gt; son2.mp3

song3.mp3 ==%26gt; son3.mp3

song4.mp3 ==%26gt; son4.mp3

song5.mp3 ==%26gt; son5.mp3
  • music myspace
  • tvs
  • No comments:

    Post a Comment