Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I rename the extension for a bunch of files?

In a directory, I have a bunch of *.html files. I'd like to rename them all to *.txt

How can I do that? I use the bash shell.

like image 297
bmw0128 Avatar asked Aug 03 '09 21:08

bmw0128


People also ask

Is there a way to rename a bunch of files at once?

You can press and hold the Ctrl key and then click each file to rename. Or you can choose the first file, press and hold the Shift key, and then click the last file to select a group.


Video Answer


2 Answers

If using bash, there's no need for external commands like sed, basename, rename, expr, etc.

for file in *.html do   mv "$file" "${file%.html}.txt" done 
like image 142
ghostdog74 Avatar answered Oct 18 '22 05:10

ghostdog74


For an better solution (with only bash functionality, as opposed to external calls), see one of the other answers.


The following would do and does not require the system to have the rename program (although you would most often have this on a system):

for file in *.html; do     mv "$file" "$(basename "$file" .html).txt" done 

EDIT: As pointed out in the comments, this does not work for filenames with spaces in them without proper quoting (now added above). When working purely on your own files that you know do not have spaces in the filenames this will work but whenever you write something that may be reused at a later time, do not skip proper quoting.

like image 23
Mikael Auno Avatar answered Oct 18 '22 03:10

Mikael Auno