我想要从终端更改文件名称。我有很多文件,所以我不能一个一个地改变它们。通过增加数字更改多个文件的名称
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
你想更改名称或增加号码的值 –