我想要從終端更改文件名稱。我有很多文件,所以我不能一個一個地改變它們。通過增加數字更改多個文件的名稱
a20170606_1257.txt -> a20170606_1300.txt
a20170606_1258.txt -> a20170606_1301.txt
我只能通過改變它:
rename 57.txt 00.txt *57.txt
,但它是不夠的。
我想要從終端更改文件名稱。我有很多文件,所以我不能一個一個地改變它們。通過增加數字更改多個文件的名稱
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+)/$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 --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
含義'1257'爲'1300'和'1258'爲'1301'? – Inian
是的,我想以這種方式改變它。 – raquela
你想更改名稱或增加號碼的值 –