How do I rename the extension for a batch of files

2019-01-04 15:29发布

In a directory, I have a bunch of *.html files.

I'd like to rename them all to *.txt

I use the bash shell.

18条回答
戒情不戒烟
2楼-- · 2019-01-04 15:45

Here is what i used to rename .edge files to .blade.php

for file in *.edge; do     mv "$file" "$(basename "$file" .edge).blade.php"; done

Works like charm.

查看更多
SAY GOODBYE
3楼-- · 2019-01-04 15:46

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
查看更多
闹够了就滚
4楼-- · 2019-01-04 15:48

If you prefer PERL, there is a short PERL script (orignally written by Larry Wall, the creator of PERL) that will do exactly what you want here: tips.webdesign10.com/files/rename.pl.txt. For your example the following should do the trick

rename.pl 's/html/txt/' *.html

= )

(Thanks @loretoparisi for the updated URL)

查看更多
地球回转人心会变
5楼-- · 2019-01-04 15:49

Try this

rename .html .txt *.html 

usage:

rename [find] [replace_with] [criteria]
查看更多
劫难
6楼-- · 2019-01-04 15:50

This question explicitly mentions Bash, but if you happen to have ZSH available it is pretty simple:

zmv '(*).*' '$1.txt'

If you get zsh: command not found: zmv then simply run:

autoload -U zmv

And then try again.

Thanks to this original article for the tip about zmv.

查看更多
叼着烟拽天下
7楼-- · 2019-01-04 15:53

For Ubuntu Users :

rename 's/\.html$/\.txt/' *.html
查看更多
登录 后发表回答