Linux批量修改文件名前缀rename命令

作者:matrix 发布时间:2019-02-22 分类:零零星星

win上的打包的文件丢到linux解压发现中文的前缀乱码,本来mv命令到是可以修改 顾于文件太多,发现用rename方便的多
需要将╬в╨┼═╝╞м_20180626102853.jpg修改还原为微信图片_20180626102853.jpg
图片4379-Linux批量修改文件名前缀rename命令

执行操作

文件数量有点多执行
命令$:rename 's/╬в╨┼═╝╞м(.*)/微信图片$1/' *

rename --help

Usage:
    rename [ -h|-m|-V ] [ -v ] [ -n ] [ -f ] [ -e|-E *perlexpr*]*|*perlexpr*
    [ *files* ]

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

    -n, -nono
            No action: print names of files to be renamed, but don't rename.

    -f, -force
            Over write: allow existing files to be over-written.

    -h, -help
            Help: print SYNOPSIS and OPTIONS.

    -m, -man
            Manual: print manual page.

    -V, -version
            Version: show version number.

    -e      Expression: code to act on files name.

            May be repeated to build up code (like "perl -e"). If no -e, the
            first argument is used as code.

    -E      Statement: code to act on files name, as -e but terminated by
            ';'.


perlexpr表达式

perlexpr还可用于其他命令,如sed

perlexpr表达式形如:

  1. Substitution替换
    s / expr1 / expr2 / [gi]
    /为分界符,可以使用其他任意字符为分界符
    expr1,expr2都支持正则
    expr1会被查找替换为expr2
    ig两个字母分别为正则的匹配模式:忽略大小写和全局匹配,否则默认换行为分界符。方括号[]包裹表示他们可不填。

  2. Translation字符转译
    类似于替换s,它可用于将一个字符串转换为另一个字符串,即字符转换。表达式如:y/charset1/charset2/

如:
转换为小写:rename 'y/A-Z/a-z/' *
添加txt后缀:rename 's/$/\.txt/' *

PEACE~

参考:
https://www.computerhope.com/unix/rename.htm
https://wangchujiang.com/linux-command/c/rename.html
https://blog.csdn.net/qq_37858386/article/details/78404001
http://bbs.chinaunix.net/thread-4119882-1-1.html