2014年10月29日星期三

批次更改檔名 in Linux

因為先前寫UVa時,檔案名稱有時會花心思改,有時就直接把題目名稱加上.cpp就貼上了
導致現在有不同的格式出現

  

現在要處理的事情很簡單

1. 去除空白
2. 將底線 ( _ ) 換成dash ( - )

經過一番查詢,終於發現最簡單的方法 - rename指令
它是基於 Perl 底下發展出來的




NAME
       rename - renames multiple files

SYNOPSIS
       rename [ -v ] [ -n ] [ -f ] perlexpr [ files ]

DESCRIPTION
       "rename" renames the filenames supplied according to the rule specified
       as the first argument.  The perlexpr argument is a Perl expression
       which is expected to modify the $_ string in Perl for at least some of
       the filenames specified.  If a given filename is not modified by the
       expression, it will not be renamed.  If no filenames are given on the
       command line, filenames will be read via standard input.

       For example, to rename all files matching "*.bak" to strip the
       extension, you might say

               rename 's/\.bak$//' *.bak

       To translate uppercase names to lower, you'd use

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


重點來了,因為它是用 Perl expression,要稍微了解一下它的方式
不過指令本身的格式很簡單:

     rename 's/要改的部份/要變成的部份/' 目的檔案

所以操作的方式為

    rename 's/ //' *
    rename 's/_/-/' *

這樣就可以了

沒有留言: