2017-06-21 76 views
0

我想要从终端更改文件名称。我有很多文件,所以我不能一个一个地改变它们。通过增加数字更改多个文件的名称

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

我只能通过改变它:

rename 57.txt 00.txt *57.txt 

,但它是不够的。

+0

含义'1257'为'1300'和'1258'为'1301'? – Inian

+0

是的,我想以这种方式改变它。 – raquela

+0

你想更改名称或增加号码的值 –

回答

1

与参数扩展只是玩提取${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变量。

1

的Perl e标志营救

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

测试

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


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 
+0

它不起作用。我收到重命名:无效选项 - 'n'。 – raquela

+0

尝试'rename --help'并查看它是否有'-n'选项,它应该有它。 –

+0

我只有v,s,h,V。 – raquela