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?
views:
47answers:
3This 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)
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.