通过增加数量来更改许多文件的名称

changing the name of many files by increasing the number

我想从终端更改文件名。我有很多文件,所以我无法一一更改所有文件。

a20170606_1257.txt -> a20170606_1300.txt
a20170606_1258.txt -> a20170606_1301.txt

我只能通过以下方式更改它:

rename 57.txt 00.txt *57.txt

但这还不够

只是玩参数扩展来提取最长和最短的 ${str##*}${str%%*}

类型的字符串
offset=43
for file in *.txt; do 
    [ -f "$file" ] || continue
    woe="${file%%.*}"; ext="${file##*.}"
    num="${woe##*_}"
    echo "$file" "${woe%%_*}_$((num+offset)).${ext}"
done

一旦你让它工作,删除 echo 行并将其替换为 mv -v。根据您希望从何处开始重命名文件,根据需要更改 offset 变量。

Perl e 救援标志

rename -n -v  's/(?<=_)(\d+)/+43/e' *.txt

测试

 dir $  ls | cat -n
     1  a20170606_1257.txt
     2  a20170606_1258.txt
 dir $  
 dir $  
 dir $  rename -n -v  's/(?<=_)(\d+)/+43/e' *.txt
rename(a20170606_1257.txt, a20170606_1300.txt)
rename(a20170606_1258.txt, a20170606_1301.txt)
 dir $  
 dir $  rename -v  's/(?<=_)(\d+)/+43/e' *.txt
a20170606_1257.txt renamed as a20170606_1300.txt
a20170606_1258.txt renamed as a20170606_1301.txt
 dir $  
 dir $  ls | cat -n
     1  a20170606_1300.txt
     2  a20170606_1301.txt
 dir $  


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