2017-06-21 55 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

相關問題