Home » Questions » Computers [ Ask a new question ]

Rename a group of files with one command

Rename a group of files with one command

If I have a group of files with a .htm extention, how can I rename them all to .html?

Asked by: Guest | Views: 279
Total answers/comments: 5
Guest [Entry]

"Or, 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."
Guest [Entry]

"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"
Guest [Entry]

If you use Zsh you can use 'zmv'
Guest [Entry]

"The best tool is mmv.

mmv \*.htm #1.html

Other examples of use (and of other tools) in ""GNU/Linux Command-Line Tools Summary""."
Guest [Entry]

"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"