tags:

views:

47

answers:

3

I have many html files named 12345.html, 12346.html, etc. I need to change "style.css" to "style-12345.css" or the appropriate file name. I'm not sure what tool to use, recommendations?

+2  A: 

This is pretty easily done with a for loop and sed:

for i in *.html; do
    sed -i "s/style\\.css/style-`basename $i .html`.css/g" $i
done

The loop runs the inner command with $i set to each .html filename. sed -i modifies the file in place. basename $i .html gets $i without the .html suffix (i.e. just the number)

Michael Mrozek
You should probably escape the '.' in the match section ("style.css" -> "style\.css") "just in case" ...
opello
@opello Ah yes, fixed
Michael Mrozek
You should probably evaluate `\`basename $i .html\`` outside of the sed expression, in case you need it more then once. I'd also recommend using `$()` instead of `\`\`` -- I find it easier to read.
Craig Trader
+1  A: 

Look for a command named rename. It comes in two varieties, depending upon the implementation.

The perl package provides /usr/bin/prename which uses perl-style regular expressions to rename files. As an example, this command

$ prename 's/foo/bar/ *foo* 

would change 'foo' to 'bar' in every filename that contains 'foo'.

The util-linux package provides /usr/bin/rename which uses simple string substitution to rename files. As an example, this command

$ rename foo bar *foo*

would have the same effect as the first one.

prename is much more powerful than regular rename, but that power means it's trickier to use.

Craig Trader
One of us is misunderstanding him; it sounded to me like he needs to change occurrences of "style.css" within the HTML files, not rename them. @wmil Some clarification might be in order
Michael Mrozek
Or, he needs both tools, because he needs to rename a file, plus every reference to that file...
Craig Trader
A: 
perl -pi -e 's/style.css/style-12345.css/g' *.html
depesz