script to include filename with variations at the beginning of the file

Hi there,

I have a bunch of files that I want to modify.
As a beginner, but nevertheless enthusiast, in the use of the shell I want to create a script enabling me to modify those files quickly.
I had only some partial success with various peaces of scripts but I would like to create one script that do all the various actions.

So I have many files looking like these:

how-to.html
contact-me.html
what-is-new.html
etc...

There already have content in it but I want to add some content to them. I want to include their names with some variations in the beginning of each files, for example for how-to.html, I want those lines to be added at the beginning of the file:

how-to_mysite
how-to
How To
how to

I did a lot of searching and I basically found this script that seemed to have the bases of what I was looking for:

for file in *
do
    echo "$file" > ./tmpfile
    cat "$file" >> ./tmpfile
    mv ./tmpfile "$file"
done

Trying to get it step by step, I am already stuck at what I think could be logically the best choice for the first one:

  • first remove the .html extension (to put it back again at the end of the whole process)
  • second put the name of the file added with _mysite, a line break and again the name of the file

For the first part,I thought about using the following command, which I habe been using for a long time in the terminal without any problem

rename s"/*.html//g" *.html

For the second part, I just made a small modification of the model that I found:

for file in *
do    
    echo "$file"_mywebsite"\n$file" > ./tmpfile
    cat "$file" >> ./tmpfile
    mv ./tmpfile "$file"
done;

But I cannot figure out why this is not worlking:

`rename s"/*.html//g" *.html`;
for file in *
do    
    echo "$file"_mywebsite"\n$file" > ./tmpfile
    cat "$file" >> ./tmpfile
    mv ./tmpfile "$file"
done;
`rename s"/*/*.html/g" *`;

There is something wrong in the rename commands are they are not executed but I don�t have a clue.
I tried ' instead of " but it doesn�t change the output, I always get:

Quantifier follows nothing in regex; marked by <-- HERE in m/* <-- HERE / at (eval 1) line 1.

I also put the rename commands inside "for file in * do" and "done;" but no change

Forget the rename command, use mv

Not that using mv will make you loose the inode of the target file you can alternately do :

for i in *.html
do
     echo "${i%.*}" >tmpfile
     cat "$i" >>tmpfile
     cat tmpfile >>"$i"
     rm tmpfile
done

---------- Post updated at 04:01 PM ---------- Previous update was at 03:54 PM ----------

For education purpose :

# a=toto.AnyThing.html
# echo $a
toto.AnyThing.html
# echo "${a%.*}"
toto.AnyThing
# echo "${a%%.*}"
toto

also read this