Rename

根據一些計算重命名文件名

  • September 27, 2017

我有一些這樣的文件:

file.i001.trusted.txt
file.i002.trusted.txt
...
...
file.i212.trusted.txt

ETC..

現在我想將索引號從 i001 更改為 i030 到 A101 到 A130 和 i031 到 i060 作為 A201 到 A230 ..

我主要在 FreeBSD 下使用“renamex”(它支持正則表達式)..

Usage: renamex [OPTIONS] filename ...
OPTIONS:
 -f, --file              Load file names from the file
 -l, --lowercase         Lowercase the file name
 -u, --uppercase         Uppercase the file name
 -s/PATTERN/STRING[/SW]  Replace the matching PATTERN with STRING.
                         The SW could be:
                         [i] ignore case when searching
                         [b] backward searching and replacing
                         [s] change file's suffix name
                         [r] PATTERN is regular expression
                         [e] PATTERN is extended regular expression
                         [g] replace all occurrences in the filename
                         [1-9] replace specified occurrences in the filename
 -R, --recursive         Operate on files and directories recursively
 -o, --owner OWNER       Change file's ownership (superuser only)
 -v, --verbose           Display verbose information
 -t, --test              Test only mode. Do not change any thing
 -h, --help              Display this help and exit
 -V, --version           Output version information and exit
 -A, --always            Always overwrite the existing files
 -N, --never             Never overwrite the existing files
Please see manpage regex(7) for the details of extended regular expression.

你有什麼建議?

編輯:範圍可能不同。所以它不會總是30個項目。例如 A1 是 30 項,A2 是 40 項,A3 是 25 項,依此類推……

zsh

autoload zmv # best in ~/.zshrc
zmv -n '(file.)i(<->)(.trusted.txt)' '$1A$(($2+30+70*(($2-1)/30+1)))$3'

(開心時拔掉-n或管子)。sh

那會執行:

mv -- file.i001.trusted.txt file.A101.trusted.txt
mv -- file.i002.trusted.txt file.A102.trusted.txt
[...]
mv -- file.i029.trusted.txt file.A129.trusted.txt
mv -- file.i030.trusted.txt file.A130.trusted.txt
mv -- file.i031.trusted.txt file.A201.trusted.txt
mv -- file.i032.trusted.txt file.A202.trusted.txt
[...]
mv -- file.i059.trusted.txt file.A229.trusted.txt
mv -- file.i060.trusted.txt file.A230.trusted.txt
mv -- file.i061.trusted.txt file.A301.trusted.txt
mv -- file.i062.trusted.txt file.A302.trusted.txt
[...]
mv -- file.i211.trusted.txt file.A801.trusted.txt
mv -- file.i212.trusted.txt file.A802.trusted.txt

如果您只想處理前 60 個<->,則可以替換為。<1-60>

如果不總是 30 個批次,您總是可以執行幾個zmvs

i=1 j=100
for batch (30 40 30 50) {
 zmv -n "(file.)i(<$i-$((i+batch-1))>)(.trusted.txt)" \
        '$1A$(($2+j))$3'
 ((i += batch, j += 100 - batch))
}

這使:

mv -- file.i001.trusted.txt file.A101.trusted.txt
[...]
mv -- file.i030.trusted.txt file.A130.trusted.txt
mv -- file.i031.trusted.txt file.A201.trusted.txt
[...]
mv -- file.i070.trusted.txt file.A240.trusted.txt
mv -- file.i071.trusted.txt file.A301.trusted.txt
[...]
mv -- file.i100.trusted.txt file.A330.trusted.txt
mv -- file.i101.trusted.txt file.A401.trusted.txt
[...]
mv -- file.i150.trusted.txt file.A450.trusted.txt

引用自:https://unix.stackexchange.com/questions/394738