DATA RECOVERY UNION > Articles > Unix – Rename a group of files with one command
Posted in

Unix – 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?

mv *.htm *.html

does not work.

Solution:

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.

๐Ÿ’ 

๐Ÿ”ต Best-selling hard drives, USB flash drives & SSDs everyone's buying.

Fast, reliable, and on sale now. Thousands pick these weekly โ€” don't miss Amazon's lowest storage prices.

โšก Top 10 Bestsellers
๐Ÿ† 4.7โ˜…+ Reviews
๐Ÿ“ฆ Prime Shipping
๐Ÿ‘‰ See today's best-selling Data storage on Amazon.com HDD ยท USB Flash Drives ยท SSD ยท External Drives
๐Ÿ›’
โœ… Updated hourly โ€” Amazon real-time ranking ๐Ÿ”ฅ Limited stock deals ๐Ÿ”— Affiliate
โญ Click to see complete best-selling list โญ