zoukankan      html  css  js  c++  java
  • (转)rename命令详解

    rename命令详解:

    原文:http://www.cnblogs.com/amosli/p/3491649.html

    文件重命名是常用的操作之一,一般对单个文件的重命名用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命令每个命令都可以拿出来仔细研究,但想深入研究,一定要掌握正则,正则表达式,主流语言都支持,对文本处理有很大帮助,接下来的篇幅将会研究一些正则表达式。

  • 相关阅读:
    括号序列
    秘密信息
    大奖赛
    订单
    摆花
    利用spring自己实现观察者模式
    Spring操作mongo排序,限制查询记录数
    Hbse的读写过程
    使用aop记录数据库操作的执行时间
    分享一个关于jackson的Json工具类
  • 原文地址:https://www.cnblogs.com/liujiacai/p/8313548.html
Copyright © 2011-2022 走看看