我想从终端更改文件名。我有很多文件,所以我无法逐一更改所有文件。
a20170606_1257.txt -> a20170606_1300.txt
a20170606_1258.txt -> a20170606_1301.txt
我只能通过以下方式进行更改:
rename 57.txt 00.txt *57.txt
但这还不够。
答案 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 :(得分: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 --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