If I have a group of files with a .htm extention, how can I rename them all to .html?
mv *.htm *.html
does not work.
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.
Sign up to join this communityOr, you could use pure bash... (except for mv, that is..)
for file in *.htm; do mv "$file" "${file%.htm}.html"; done
and avoid the nasty basename stuff. ;)
Bash has an extensive set of variable expansion options. The one used here, '%', removes the smallest matching suffix from the value of the variable. The pattern is a glob pattern, so ${file%.*}
would also work. The '%%' operator removes the largest matching suffix, and is interchangeable in the example above, as the pattern is fixed, ${file%%.*}.html
would turn a.b.htm into a.html though.
See the variable substition section of the bash manpage for more neat tricks. There's a lot that can be done within bash directly.
There shouldn't be spaces, newlines or other whitespace in the filenames, but this version of freiheit's answer handles those. It also uses $()
instead of backticks for readability along with other benefits.
for file in *.htm
do
mv "$file" "$(basename "$file" .htm).html"
done
Even better - for the special case of just adding on to the end:
for file in *.htm
do
mv "$file" "${file}l"
done
rename(1)
is a Perl utility that does exactly what you want. In this case:
rename 's/\.htm$/.html/' *htm
or if you are using sub directories as well
(requires Bash 4.0 and the globstar setting: shopt -s globstar
)
rename 's/\.htm$/.html/' **/*htm
rename
comes with perl, and basically every unix comes with perl these days.
shopt -s globstar
(off by default). Also, Linux and Unix have directories rather than folders.
Aug 11, 2012 at 23:59
for file in *.htm; do
mv $file `basename "$file" .htm`.html
done
Try it with an echo
in front of the mv
first time around.
The problem with your original is that "mv *.htm *.html
" has the *
s handled by the shell, so the mv
command simply sees a list of all the .htm and .html files in the current directory. In other words, something like "mv foo.htm bar.htm stuff.htm six.htm file.htm
". mv only knows how to handle more than 2 arguments if the last one is a directory.
mv
won't work.
Jul 19, 2009 at 21:57
Yet another pure bash example using string replace.
for file in *.htm; do mv $file ${file/htm/html}; done
Extra - this replaces all the occurrences of a string
for file in *.htm; do mv $file ${file//htm/html}; done
The best tool is mmv.
mmv \*.htm #1.html
Other examples of use (and of other tools) in "GNU/Linux Command-Line Tools Summary".
The util-linux-ng package (on Fedora) has a rename
command similar to the one mentioned by TRS-80. You can use it like this:
rename .htm .html *.html
ren *.htm *.html
will work in Windows.