rename (linux only, not ubuntu)

重命名文件

语法

  rename from to file...

rename将通过将第一个出现的from替换为to来重命名指定的文件

例子

将文件 foo1, …, foo9, foo10, …, foo278, 变成 foo001, …, foo009, foo010, …, foo278.

$ rename foo foo0 foo?
$ rename foo foo0 foo??

修复.htm文件的扩展名,使它们成为.html:

$ rename .htm .html *.htm

将所有文件(*)重命名为小写:

$ rename 'y/A-Z/a-z/' *

重命名不是bash内置的,它可以在大多数发行版上使用,如果你没有它,那么另一种方法是使用for循环。例如,重命名一个basename.txt文件的文件夹,以扩展名为.html

$ for i in *.txt; do mv "$i" "`basename $i .txt`.html"; done 
or
$ for files in *.txt; do mv "$files" "${files%.txt}.html"; done

rename 命令实例:

linux命令:rename 's/\d+/sprintf("%04d",$&)/e' * * 's/\d+/sprintf("%04d",$&)/e' rename rename 's/\d+/sprintf("%04d",$&)/e' *rootopen.com
linux命令:rename 's/\d+/sprintf("%04d",$&)/e' * * 's/\d+/sprintf("%04d",$&)/e' rename rename 's/\d+/sprintf("%04d",$&)/e' *rootopen.com
2019-06-04 22:29:09

linux命令:rename 'y/A-Z/a-z/' * * 'y/A-Z/a-z/' rename rename 'y/A-Z/a-z/' *rootopen.com
linux命令:rename 'y/A-Z/a-z/' * * 'y/A-Z/a-z/' rename rename 'y/A-Z/a-z/' *rootopen.com
2018-11-22 22:55:55

linux命令:rename 's/\.html$/\.php/' *.html *.html 's/\.html$/\.php/' rename rename 's/\.html$/\.php/' *.htmlrootopen.com
linux命令:rename 's/\.html$/\.php/' *.html *.html 's/\.html$/\.php/' rename rename 's/\.html$/\.php/' *.htmlrootopen.com
2018-09-11 09:55:30

linux命令:rename .oldextension .newextension *.oldextension *.oldextension .newextension .oldextension rename rename .oldextension .newextension *.oldextensionrootopen.com
linux命令:rename .oldextension .newextension *.oldextension *.oldextension .newextension .oldextension rename rename .oldextension .newextension *.oldextensionrootopen.com
2017-12-25 07:43:48

linux命令:rename 's/foo/bar/g' * * 's/foo/bar/g' rename rename 's/foo/bar/g' *rootopen.com
linux命令:rename 's/foo/bar/g' * * 's/foo/bar/g' rename rename 's/foo/bar/g' *rootopen.com
2018-05-30 08:14:48

linux命令:rename 'y/A-Z/a-z/' * * 'y/A-Z/a-z/' rename rename 'y/A-Z/a-z/' *rootopen.com
linux命令:rename 'y/A-Z/a-z/' * * 'y/A-Z/a-z/' rename rename 'y/A-Z/a-z/' *rootopen.com
2018-02-15 17:08:48

linux命令:rename .JPG .jpg *.JPG *.JPG .jpg .JPG rename rename .JPG .jpg *.JPGrootopen.com
linux命令:rename .JPG .jpg *.JPG *.JPG .jpg .JPG rename rename .JPG .jpg *.JPGrootopen.com
2018-04-18 04:35:48

linux命令:rename 's/.*/[it]$&/' *.pdf *.pdf 's/.*/[it]$&/' rename rename 's/.*/[it]$&/' *.pdfrootopen.com
linux命令:rename 's/.*/[it]$&/' *.pdf *.pdf 's/.*/[it]$&/' rename rename 's/.*/[it]$&/' *.pdfrootopen.com
2017-12-25 18:22:48

linux命令:rename 'y/ /_/' * * /_/' 'y/ rename rename 'y/ /_/' *rootopen.com
linux命令:rename 'y/ /_/' * * /_/' 'y/ rename rename 'y/ /_/' *rootopen.com
2018-02-12 02:54:48

linux命令:rename 's/^/prefix/' * * 's/^/prefix/' rename rename 's/^/prefix/' *rootopen.com
linux命令:rename 's/^/prefix/' * * 's/^/prefix/' rename rename 's/^/prefix/' *rootopen.com
2018-05-02 20:23:48

linux命令:rename -v 's/ /_/g' * * /_/g' 's/ -v rename rename -v 's/ /_/g' *rootopen.com
linux命令:rename -v 's/ /_/g' * * /_/g' 's/ -v rename rename -v 's/ /_/g' *rootopen.com
2018-03-08 21:16:48

linux命令:rename -v 's/.*[s,S](\d{2}).*[e,E](\d{2}).*\.avi/SHOWNAME\ S$1E$2.avi/' poorly.named.file.s01e01.avi poorly.named.file.s01e01.avi S$1E$2.avi/' 's/.*[s,S](\d{2}).*[e,E](\d{2}).*\.avi/SHOWNAME\ -v rename rename -v 's/.*[s,S](\d{2}).*[e,E](\d{2}).*\.avi/SHOWNAME\ S$1E$2.avi/' poorly.named.file.s01e01.avirootopen.com
linux命令:rename -v 's/.*[s,S](\d{2}).*[e,E](\d{2}).*\.avi/SHOWNAME\ S$1E$2.avi/' poorly.named.file.s01e01.avi poorly.named.file.s01e01.avi S$1E$2.avi/' 's/.*[s,S](\d{2}).*[e,E](\d{2}).*\.avi/SHOWNAME\ -v rename rename -v 's/.*[s,S](\d{2}).*[e,E](\d{2}).*\.avi/SHOWNAME\ S$1E$2.avi/' poorly.named.file.s01e01.avirootopen.com
2018-03-20 05:27:48

linux命令:rename 'y/A-Z/a-z/' * * 'y/A-Z/a-z/' rename rename 'y/A-Z/a-z/' *rootopen.com
linux命令:rename 'y/A-Z/a-z/' * * 'y/A-Z/a-z/' rename rename 'y/A-Z/a-z/' *rootopen.com
2018-01-26 07:57:48

linux命令:rename "s/ *//g" *.jpg *.jpg *//g" "s/ rename rename "s/ *//g" *.jpgrootopen.com
linux命令:rename "s/ *//g" *.jpg *.jpg *//g" "s/ rename rename "s/ *//g" *.jpgrootopen.com
2018-02-20 07:06:48

linux命令:rename 's/ /_/g' * * /_/g' 's/ rename rename 's/ /_/g' *rootopen.com
linux命令:rename 's/ /_/g' * * /_/g' 's/ rename rename 's/ /_/g' *rootopen.com
2018-04-09 08:47:48

linux命令:rename 's/foo/bar/g' foobar foobar 's/foo/bar/g' rename rename 's/foo/bar/g' foobarrootopen.com
linux命令:rename 's/foo/bar/g' foobar foobar 's/foo/bar/g' rename rename 's/foo/bar/g' foobarrootopen.com
2018-02-18 19:22:48

linux命令:rename 's/(^|[\s\(\)\[\]_-])([a-z])/$1\u$2/g' * * 's/(^|[\s\(\)\[\]_-])([a-z])/$1\u$2/g' rename rename 's/(^|[\s\(\)\[\]_-])([a-z])/$1\u$2/g' *rootopen.com
linux命令:rename 's/(^|[\s\(\)\[\]_-])([a-z])/$1\u$2/g' * * 's/(^|[\s\(\)\[\]_-])([a-z])/$1\u$2/g' rename rename 's/(^|[\s\(\)\[\]_-])([a-z])/$1\u$2/g' *rootopen.com
2018-03-30 16:57:48

linux命令:rename 's/\b([a-z])/\u$1/g' * * 's/\b([a-z])/\u$1/g' rename rename 's/\b([a-z])/\u$1/g' *rootopen.com
linux命令:rename 's/\b([a-z])/\u$1/g' * * 's/\b([a-z])/\u$1/g' rename rename 's/\b([a-z])/\u$1/g' *rootopen.com
2018-04-05 16:56:48

linux命令:rename 's/\b((?!(a|of|that|to)\b)[a-z]+)/\u$1/g' * * 's/\b((?!(a|of|that|to)\b)[a-z]+)/\u$1/g' rename rename 's/\b((?!(a|of|that|to)\b)[a-z]+)/\u$1/g' *rootopen.com
linux命令:rename 's/\b((?!(a|of|that|to)\b)[a-z]+)/\u$1/g' * * 's/\b((?!(a|of|that|to)\b)[a-z]+)/\u$1/g' rename rename 's/\b((?!(a|of|that|to)\b)[a-z]+)/\u$1/g' *rootopen.com
2018-05-09 18:15:48

linux命令:rename 'y/ /_/' * * /_/' 'y/ rename rename 'y/ /_/' *rootopen.com
linux命令:rename 'y/ /_/' * * /_/' 'y/ rename rename 'y/ /_/' *rootopen.com
2018-03-09 09:19:48

linux命令:rename 's/ /-/g' * * /-/g' 's/ rename rename 's/ /-/g' *rootopen.com
linux命令:rename 's/ /-/g' * * /-/g' 's/ rename rename 's/ /-/g' *rootopen.com
2018-01-05 03:43:48

linux命令:rename 's/\.jpeg/\.jpg/' *.jpeg; rename 's/\.JPG/\.jpg/' *.JPG *.JPG 's/\.JPG/\.jpg/' rename *.jpeg; 's/\.jpeg/\.jpg/' rename rename 's/\.jpeg/\.jpg/' *.jpeg; rename 's/\.JPG/\.jpg/' *.JPGrootopen.com
linux命令:rename 's/\.jpeg/\.jpg/' *.jpeg; rename 's/\.JPG/\.jpg/' *.JPG *.JPG 's/\.JPG/\.jpg/' rename *.jpeg; 's/\.jpeg/\.jpg/' rename rename 's/\.jpeg/\.jpg/' *.jpeg; rename 's/\.JPG/\.jpg/' *.JPGrootopen.com
2018-03-27 06:20:48

linux命令:rename 's/\.jpe?g$/.jpg/i' * * 's/\.jpe?g$/.jpg/i' rename rename 's/\.jpe?g$/.jpg/i' *rootopen.com
linux命令:rename 's/\.jpe?g$/.jpg/i' * * 's/\.jpe?g$/.jpg/i' rename rename 's/\.jpe?g$/.jpg/i' *rootopen.com
2018-01-04 01:31:48

linux命令:rename 's/.txt/.md/i' * * 's/.txt/.md/i' rename rename 's/.txt/.md/i' *rootopen.com
linux命令:rename 's/.txt/.md/i' * * 's/.txt/.md/i' rename rename 's/.txt/.md/i' *rootopen.com
2018-03-21 07:43:48

linux命令:rename 's/\.txt$/\.md$/i' * * 's/\.txt$/\.md$/i' rename rename 's/\.txt$/\.md$/i' *rootopen.com
linux命令:rename 's/\.txt$/\.md$/i' * * 's/\.txt$/\.md$/i' rename rename 's/\.txt$/\.md$/i' *rootopen.com
2018-04-16 08:33:48

linux命令:rename .txt .md *.txt *.txt .md .txt rename rename .txt .md *.txtrootopen.com
linux命令:rename .txt .md *.txt *.txt .md .txt rename rename .txt .md *.txtrootopen.com
2018-05-29 11:33:48

linux命令:rename -n "s/-.*//" * * "s/-.*//" -n rename rename -n "s/-.*//" *rootopen.com
linux命令:rename -n "s/-.*//" * * "s/-.*//" -n rename rename -n "s/-.*//" *rootopen.com
2018-03-15 03:41:48

linux命令:rename 's/foo/bar/g' ./* ./* 's/foo/bar/g' rename rename 's/foo/bar/g' ./*rootopen.com
linux命令:rename 's/foo/bar/g' ./* ./* 's/foo/bar/g' rename rename 's/foo/bar/g' ./*rootopen.com
2018-03-08 11:10:48

linux命令:rename 's/\d+/sprintf("%04d",$&)/e' *.jpg *.jpg 's/\d+/sprintf("%04d",$&)/e' rename rename 's/\d+/sprintf("%04d",$&)/e' *.jpgrootopen.com
linux命令:rename 's/\d+/sprintf("%04d",$&)/e' *.jpg *.jpg 's/\d+/sprintf("%04d",$&)/e' rename rename 's/\d+/sprintf("%04d",$&)/e' *.jpgrootopen.com
2018-05-01 23:31:48

linux命令:rename foo bar directory/filename directory/filename bar foo rename rename foo bar directory/filenamerootopen.com
linux命令:rename foo bar directory/filename directory/filename bar foo rename rename foo bar directory/filenamerootopen.com
2018-04-04 14:14:48

linux命令:rename foo bar filename filename bar foo rename rename foo bar filenamerootopen.com
linux命令:rename foo bar filename filename bar foo rename rename foo bar filenamerootopen.com
2018-04-15 01:50:48

linux命令:rename 's/ /_/g' * * /_/g' 's/ rename rename 's/ /_/g' *rootopen.com
linux命令:rename 's/ /_/g' * * /_/g' 's/ rename rename 's/ /_/g' *rootopen.com
2018-04-06 12:09:48

linux命令:rename.ul "" 00 ?.jpg; rename "" 0 ??.jpg; ??.jpg; 0 "" rename ?.jpg; 00 "" rename.ul rename.ul "" 00 ?.jpg; rename "" 0 ??.jpg;rootopen.com
linux命令:rename.ul "" 00 ?.jpg; rename "" 0 ??.jpg; ??.jpg; 0 "" rename ?.jpg; 00 "" rename.ul rename.ul "" 00 ?.jpg; rename "" 0 ??.jpg;rootopen.com
2018-03-06 02:24:48

linux命令:rename s/^/./ * * s/^/./ rename rename s/^/./ *rootopen.com
linux命令:rename s/^/./ * * s/^/./ rename rename s/^/./ *rootopen.com
2018-05-19 15:53:48

linux命令:rename ' ' '_' * * '_' ' ' rename rename ' ' '_' *rootopen.com
linux命令:rename ' ' '_' * * '_' ' ' rename rename ' ' '_' *rootopen.com
2018-03-14 10:39:48

linux命令:rename 's/ /-/g' *.mp3 *.mp3 /-/g' 's/ rename rename 's/ /-/g' *.mp3rootopen.com
linux命令:rename 's/ /-/g' *.mp3 *.mp3 /-/g' 's/ rename rename 's/ /-/g' *.mp3rootopen.com
2018-03-25 20:20:48

linux命令:rename 's/ /_/g' * * /_/g' 's/ rename rename 's/ /_/g' *rootopen.com
linux命令:rename 's/ /_/g' * * /_/g' 's/ rename rename 's/ /_/g' *rootopen.com
2018-01-01 05:46:48

linux命令:rename(){ txtToReplace=${1} ; replacementTxt=${2} ; shift 2 ; files=${@} ; for file in $files ; do mv ${file} ${file/${txtToReplace}/${replacementTxt}} ; done ; } } ; done ; ${file/${txtToReplace}/${replacementTxt}} ${file} mv do ; $files in file for ; files=${@} ; 2 shift ; replacementTxt=${2} ; txtToReplace=${1} rename(){ rename(){ txtToReplace=${1} ; replacementTxt=${2} ; shift 2 ; files=${@} ; for file in $files ; do mv ${file} ${file/${txtToReplace}/${replacementTxt}} ; done ; }rootopen.com
linux命令:rename(){ txtToReplace=${1} ; replacementTxt=${2} ; shift 2 ; files=${@} ; for file in $files ; do mv ${file} ${file/${txtToReplace}/${replacementTxt}} ; done ; } } ; done ; ${file/${txtToReplace}/${replacementTxt}} ${file} mv do ; $files in file for ; files=${@} ; 2 shift ; replacementTxt=${2} ; txtToReplace=${1} rename(){ rename(){ txtToReplace=${1} ; replacementTxt=${2} ; shift 2 ; files=${@} ; for file in $files ; do mv ${file} ${file/${txtToReplace}/${replacementTxt}} ; done ; }rootopen.com
2018-04-03 16:49:48

linux命令:rename 's/\d+/sprintf("%02d",$&)/e' -- $@ $@ -- 's/\d+/sprintf("%02d",$&)/e' rename rename 's/\d+/sprintf("%02d",$&)/e' -- $@rootopen.com
linux命令:rename 's/\d+/sprintf("%02d",$&)/e' -- $@ $@ -- 's/\d+/sprintf("%02d",$&)/e' rename rename 's/\d+/sprintf("%02d",$&)/e' -- $@rootopen.com
2018-05-10 11:28:48

linux命令:rename "s/ /_/g" * .* .* * /_/g" "s/ rename rename "s/ /_/g" * .*rootopen.com
linux命令:rename "s/ /_/g" * .* .* * /_/g" "s/ rename rename "s/ /_/g" * .*rootopen.com
2018-04-09 08:39:48

linux命令:rename 's/.xls/.ods/g' *.xls *.xls 's/.xls/.ods/g' rename rename 's/.xls/.ods/g' *.xlsrootopen.com
linux命令:rename 's/.xls/.ods/g' *.xls *.xls 's/.xls/.ods/g' rename rename 's/.xls/.ods/g' *.xlsrootopen.com
2018-01-09 02:54:48

linux命令:rename s/ .php/ .html/ *.html *.html .html/ .php/ s/ rename rename s/ .php/ .html/ *.htmlrootopen.com
linux命令:rename s/ .php/ .html/ *.html *.html .html/ .php/ s/ rename rename s/ .php/ .html/ *.htmlrootopen.com
2018-03-30 17:01:48

linux命令:rename *.JPG *.jpg *.jpg *.JPG rename rename *.JPG *.jpgrootopen.com
linux命令:rename *.JPG *.jpg *.jpg *.JPG rename rename *.JPG *.jpgrootopen.com
2018-01-11 09:14:48

linux命令:rename 's/\.sh//' ./* ./* 's/\.sh//' rename rename 's/\.sh//' ./*rootopen.com
linux命令:rename 's/\.sh//' ./* ./* 's/\.sh//' rename rename 's/\.sh//' ./*rootopen.com
2018-05-19 02:15:48

linux命令:rename 's/result_([0-9]+)_([0-9]+)_([0-9]+)\.json\.txt/sprintf("%d%02d%02d.txt",$3,$2,$1)/ge' result_*.txt result_*.txt 's/result_([0-9]+)_([0-9]+)_([0-9]+)\.json\.txt/sprintf("%d%02d%02d.txt",$3,$2,$1)/ge' rename rename 's/result_([0-9]+)_([0-9]+)_([0-9]+)\.json\.txt/sprintf("%d%02d%02d.txt",$3,$2,$1)/ge' result_*.txtrootopen.com
linux命令:rename 's/result_([0-9]+)_([0-9]+)_([0-9]+)\.json\.txt/sprintf("%d%02d%02d.txt",$3,$2,$1)/ge' result_*.txt result_*.txt 's/result_([0-9]+)_([0-9]+)_([0-9]+)\.json\.txt/sprintf("%d%02d%02d.txt",$3,$2,$1)/ge' rename rename 's/result_([0-9]+)_([0-9]+)_([0-9]+)\.json\.txt/sprintf("%d%02d%02d.txt",$3,$2,$1)/ge' result_*.txtrootopen.com
2018-04-11 20:49:48

linux命令:rename -fc * * -fc rename rename -fc *rootopen.com
linux命令:rename -fc * * -fc rename rename -fc *rootopen.com
2018-03-01 09:59:48

linux命令:rename 's/ //g' * * //g' 's/ rename rename 's/ //g' *rootopen.com
linux命令:rename 's/ //g' * * //g' 's/ rename rename 's/ //g' *rootopen.com
2017-12-26 02:50:48

linux命令:rename 's/^hospital\.php\?loc=(\d{4})$/hospital_$1/' hospital.php* hospital.php* 's/^hospital\.php\?loc=(\d{4})$/hospital_$1/' rename rename 's/^hospital\.php\?loc=(\d{4})$/hospital_$1/' hospital.php*rootopen.com
linux命令:rename 's/^hospital\.php\?loc=(\d{4})$/hospital_$1/' hospital.php* hospital.php* 's/^hospital\.php\?loc=(\d{4})$/hospital_$1/' rename rename 's/^hospital\.php\?loc=(\d{4})$/hospital_$1/' hospital.php*rootopen.com
2018-05-12 21:02:48

Linux的Bash命令行(A-Z排序)