批量修改文件名

OOo,TeX,KO,ABI,GIMP,Picasa,ProE,QCAD,Inkscape,Kicad,Eagle
回复
头像
laborer
帖子: 1016
注册时间: 2005-10-25 11:15
送出感谢: 0
接收感谢: 1 次
联系:

批量修改文件名

#1

帖子 laborer » 2005-12-14 17:41

weblog_entry.php?e=441

rename充分发挥了perl所具有的regular expression的功能,将其用于批量文件改名,弥补了mv的不足,既方便又强大。举几个例子:
  • 将所有的.htm文件(不区分大小写)改为.html文件

    代码: 全选

    $ rename 's/\.htm$/.html/i' *
  • 所有文件名改为小写字母

    代码: 全选

    $ rename ’y/A-Z/a-z/’ *
  • 将形如aaaa123.bbb的文件中数字部分改为定长,不足的以0填充

    代码: 全选

    $ ls
    asdf0.txt   asdf13.txt  asdf17.txt  asdf2.txt  asdf6.txt  asdf.txt
    asdf10.txt  asdf14.txt  asdf18.txt  asdf3.txt  asdf7.txt
    asdf11.txt  asdf15.txt  asdf19.txt  asdf4.txt  asdf8.txt
    asdf12.txt  asdf16.txt  asdf1.txt   asdf5.txt  asdf9.txt
    $ rename 's/^(\D*)(\d+)(\D*)$/sprintf("%s%03d%s",$1,$2,$3)/e' *
    $ ls
    asdf000.txt  asdf004.txt  asdf008.txt  asdf012.txt  asdf016.txt  asdf.txt
    asdf001.txt  asdf005.txt  asdf009.txt  asdf013.txt  asdf017.txt
    asdf002.txt  asdf006.txt  asdf010.txt  asdf014.txt  asdf018.txt
    asdf003.txt  asdf007.txt  asdf011.txt  asdf015.txt  asdf019.txt
hreiser@oakland:~$ killall -9 wife
police@oakland:~$ sudo find / -user hreiser
court@oakland:~$ sudo mv /home/hreiser /jail/
court@oakland:~$ sudo usermod -d /jail/hreiser -s "/usr/sbin/chroot /jail/" hreiser
回复

回到 “办公、图像、机械电子设计等”