rename單次及批次修改檔案名稱

sudo apt install rename

練習,產生10個檔案

touch f{1..10}.txt
f1.txt  f10.txt  f2.txt  f3.txt  f4.txt  f5.txt  f6.txt  f7.txt  f8.txt  f9.txt

rename $1 $2 $3

  1. # $1: 要被取代的關鍵字

  2. # $2: 新的關鍵字

  3. # $3: 檔名符合這個規則的才取代

rename支持通配符

?可替代單個字符
*可替代多個字符
[charset]可替代charset集中的任意單個字符

rename f1 newf1 f1.txt

f10.txt f3.txt f5.txt f7.txt f9.txt f2.txt f4.txt f6.txt f8.txt newf1.txt

# 把檔案f2, ..., f9, f10 => 改成f002, ..., f009, f010

rename f f0 f?.txt

> ls
f10.txt  f3.txt  f5.txt  f7.txt  f9.txt
f2.txt   f4.txt  f6.txt  f8.txt  newf1.txt
 
> rename f f0 f?.txt

> ls
f02.txt  f04.txt  f06.txt  f08.txt  f10.txt
f03.txt  f05.txt  f07.txt  f09.txt  newf1.txt

把所有.txt 檔案改成.html

rename .txt .html *.txt

> ls
f1.txt  f10.txt  f2.txt  f3.txt  f4.txt  f5.txt  f6.txt  f7.txt  f8.txt  f9.txt 
> rename .txt .html *.txt
> ls
f1.html   f2.html  f4.html  f6.html  f8.html
f10.html  f3.html  f5.html  f7.html  f9.html

rename .html .php f*.html

rename .html .php f*.html
> ls
f10.php  f2.php  f3.php  f4.php  f5.php  f6.php  f7.php  f8.php  f9.php

rename 'y/a-z/A-Z/' * 或 reanme f F *

ls
f10.php  f1.php  f2.php  f3.php  f4.php  f5.php  f6.php  f7.php  f8.php  f9.php
rename 'y/a-z/A-Z/' *
F10.PHP  F1.PHP  F2.PHP  F3.PHP  F4.PHP  F5.PHP  F6.PHP  F7.PHP  F8.PHP  F9.PHP

把F變成F00

> ls
F1.php  F10.php  F2.php  F3.php  F4.php  F5.php  F6.php  F7.php  F8.php  F9.php
> rename F F00 *
> ls
F001.php   F002.php  F004.php  F006.php  F008.php
F0010.php  F003.php  F005.php  F007.php  F009.php

Last updated