in a directory, i have a bunch of *.html files. I'd like to rename them all to *.txt I use the bash shell. Is this easy enough to say how to do?
You want to use rename
:
rename .html .txt *.html
This does exactly what you want - it will change the extension from .html
to .txt
for all files matching *.html
.
Note: Greg Hewgill correctly points out this is not a bash builtin; and is a seperate Linux command. If you just need something on Linux this should work fine; if you need something more cross-platform then take a look at one of the other answers.
Unfortunately it's not trivial to do portably. You probably need a bit of expr magic.
for file in *.html; do echo mv -- "$file" "$(expr "$file" : '\(.*\)\.html').txt"; done
Remove the echo once you're happy it does what you want.
Edit: basename
is probably a little more readable for this particular case, although expr
is more flexible in general.
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.
For an better solution (with only bash functionality, as opposed to external calls), see one of the other answers.
if using bash, no need for external commands like sed, basename, rename, expr...etc
for files in *.html
do
mv "$files" "${files%.html}.txt"
done