linux shell 腳本攻略學習9--rename命令詳解


rename命令詳解:

文件重命名是常用的操作之一,一般對單個文件的重命名用mv命令,如:

amosli@amosli-pc:~/learn/example$ ls
abc.txt
amosli@amosli-pc:~/learn/example$ mv abc.txt a.txt
amosli@amosli-pc:~/learn/example$ ls
a.txt

那么如何對指文件進行重命名呢?當然你可以全部手動去賦值,但很影響效率,如下,將所有文件名稱都改為大寫的,如何做呢?

 

amosli@amosli-pc:~/learn/example$ ls
a.txt  b.txt  c.txt  d.txt  e.txt
amosli@amosli-pc:~/learn/example$ rename 'y/a-z/A-Z/' *
amosli@amosli-pc:~/learn/example$ ls
A.TXT  B.TXT  C.TXT  D.TXT  E.TXT

如果用mv命令可能手動要花很長時間,但rename命令一句就搞定了。

下面來介紹rename命令工具:

Linux的rename 命令有兩個版本,一個是C語言版本的,一個是Perl語言版本的,早期的Linux發行版基本上使用的是C語言版本的,現在已經很難見到C語言版本的了,因為Perl版本的支持正則處理,所以功能更加強大,基本上現在linux下默認的rename命令都是Perl版的。

如何查看系統里的rename命令是哪個版本的?

輸入man rename命令,我的是ubuntu12.04,出現下面的提示:

amosli@amosli-pc:~/learn/example$ man rename

RENAME(1)                                        Perl Programmers Reference Guide                                        RENAME(1)

NAME
       rename - renames multiple files
..................

很明顯是Perl版的,如果輸入man rename出現下方提示,那說明是C版:

RENAME(1) Linux Programmer’s Manual RENAME(1)

這里由於沒怎么接觸過C版的,就不做介紹了。

Perl語言是公認的正則表達式之王,對正則的支持相當給力,所以在linux命令里都能使用正則。

rename的語法中就有正則:

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

在linux的rename help提示下有如下一段話:

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.

大致意思是rename命令修改符合后面條件的文件的文件名,只有符合perlexpr的文件名才會被修改,否則將不會被修改。

OPTIONS
       -v, --verbose
               Verbose: print names of files successfully renamed.

       -n, --no-act
               No Action: show what files would have been renamed.

       -f, --force
               Force: overwrite existing files.

對於參數:

-v 表示會顯示修改成功的文件名;

-n 則表示不執行任何操作,主要用來測試rename過程,並不直接運行,可以查看測試效果后,然后再運行;

-f 則表示會強制修改。

例:

drwxrwxr-x 2 amosli amosli 4096 12月 26 00:30 ./
drwxrwxr-x 5 amosli amosli 4096 12月 25 23:58 ../
-rw-rw-r-- 1 amosli amosli    0 12月 26 00:26 a.txt
-rw-rw-r-- 1 amosli amosli    0 12月 26 00:30 Untitled Document
-rw-rw-r-- 1 amosli amosli    0 12月 26 00:30 Untitled Document 2

amosli@amosli-pc:~/learn/example$ rename  -v  's/ /_/g' *
Untitled Document renamed as Untitled_Document
Untitled Document 2 renamed as Untitled_Document_2

這里用到的是-v參數,'s/ /_/g' 正則表示的是將空格替換為_, *則表示應用於所有文件。

amosli@amosli-pc:~/learn/example$ rename     's/\.txt$//'       *.txt

則表示刪除所有txt 的文件后綴名,執行結果如下:

amosli@amosli-pc:~/learn/example$ ll
total 8
drwxrwxr-x 2 amosli amosli 4096 12月 26 00:35 ./
drwxrwxr-x 5 amosli amosli 4096 12月 25 23:58 ../
-rw-rw-r-- 1 amosli amosli    0 12月 26 00:26 a
-rw-rw-r-- 1 amosli amosli    0 12月 26 00:30 Untitled_Document
-rw-rw-r-- 1 amosli amosli    0 12月 26 00:30 Untitled_Document_2

將所有目標.mp3文件移入指定的目錄中,則可以用:

find . -type f -name '*mp3' -exec mv {} ../ \; 

例:

amosli@amosli-pc:~/learn/example$ find . -type f -name '*mp3';
./b.mp3
./a.mp3
amosli@amosli-pc:~/learn/example$ find . -type f -name '*mp3' -exec mv {} ../ \; 
amosli@amosli-pc:~/learn/example$ ls
amosli@amosli-pc:~/learn/example$ cd ..
amosli@amosli-pc:~/learn$ ll
total 120
drwxrwxr-x  5 amosli amosli 4096 12月 26 00:40 ./
drwxr-xr-x 69 amosli amosli 4096 12月 25 22:11 ../
----------  1 amosli amosli    3 12月 18 22:49 a1
-rw-rw-r--  1 amosli amosli    3 12月 18 22:49 a2
-rw-rw-r--  1 amosli amosli    3 12月 18 22:49 a3
-rw-rw-r--  1 amosli amosli    0 12月 26 00:39 a.mp3
-rw-rw-r--  1 amosli amosli   16 12月 23 01:49 args.txt
-rw-rw-r--  1 amosli amosli    0 12月 26 00:39 b.mp3
-rw-rw-r--  1 amosli amosli   11 12月 23 01:45 cecho.sh
-rw-rw-r--  1 amosli amosli   79 12月 19 00:56 debug.sh

將文件名稱的大寫全部轉換為小寫則為:

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

將*.JPG更名為*.jpg則可以用:

rename *.JPG *.jpg

其他參數:rename支持的參數相當多,-a 到-z均有,如需獲取更多信息可以輸入如下命令:

man rename
h

會列出一系列的參數列表,如下:

。。。。。。。。。
-?  ........  --help
                  Display help (from command line).
  -a  ........  --search-skip-screen
                  Forward search skips current screen.
  -A  ........  --SEARCH-SKIP-SCREEN
                  Forward search always skips target line.
  -b [N]  ....  --buffers=[N]
                  Number of buffers.
  -B  ........  --auto-buffers
                  Don't automatically allocate buffers for pipes.
  -c  ........  --clear-screen
                  Repaint by clearing rather than scrolling.
  -d  ........  --dumb
                  Dumb terminal.
  -D [xn.n]  .  --color=xn.n
                  Set screen colors. (MS-DOS only)
  -e  -E  ....  --quit-at-eof  --QUIT-AT-EOF
                  Quit at end of file.
  -f  ........  --force
                  Force open non-regular files.
  -F  ........  --quit-if-one-screen
                  Quit if entire file fits on first screen.
  -g  ........  --hilite-search
                  Highlight only last match for searches.
  -G  ........  --HILITE-SEARCH
                  Don't highlight any matches for searches.
  -h [N]  ....  --max-back-scroll=[N]
                  Backward scroll limit.
  -i  ........  --ignore-case
                  Ignore case in searches that do not contain uppercase.
。。。。。。

可以根據自己需要進行使用。

linux命令每個命令都可以拿出來仔細研究,但想深入研究,一定要掌握正則,正則表達式,主流語言都支持,對文本處理有很大幫助,接下來的篇幅將會研究一些正則表達式。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM