我有以下forms的640个文件
string02_01.ext, string02_02.ext, string02_03.ext ...
我需要重新命名所有这些,例如,每改变40个项目
first 40s: a0b0c0.ext, a1b0c0.ext, a2b0c0.ext ... second 40s: a0b1c0.ext, a1b1c0.ext, a2b1c0.ext ... third 40s: a0b2c0.ext, a1b2c0.ext, a2b2c0.ext ... fourth 40s: a0b3c0.ext, a1b3c0.ext, a2b3c0.ext ...
什么是这样做的好方法? 任何想法将不胜感激。
提前致谢
你不需要这样的正则expression式,你需要一个常规的shell脚本,根据需要遍历文件。 假设你想要第40个文件被命名为a39b0c0.ext
,像这个shell脚本应该做的伎俩:
a=0 b=0 c=0 for f in string02_*.ext; do # Assuming that you're running this in the directory with the files if [ "$a" = "40" ]; then a=0 b=$(($b + 1)) fi if [ "$b" = "40" ]; then b=0 c=$(($c + 1)) fi mv $fa${a}b${b}c${c}.ext a=$(($a + 1)) done
这是我期望你的640文件名被组织,
第1行:a0b0c0.ext a1b0c0.ext ... a39b0c0.ext 第2行:a0b1c0.ext a1b1c0.ext ... a39b1c0.ext ... 第16行:a0b15c0.ext a1b15c0.ext ... a39b15c0.ext
假设你有一个640个文件的列表(每行一个),
按您希望将其重命名的顺序sorting。
(可以使用下面的命令完成)。
cd /directory/containint/the/files find . -name string02_*.ext > files.lst # maybe you'll run a 'sort' pipe to get files.lst ordered as you want.
请注意,您的640个文件不能是string02_nn.ext
的格式,
他们可能是`string02_nnn.ext然后….
不要紧,因为我们使用上述scheme。
重命名的bash脚本应该看起来像这样,
#!/bin/bash i=0; j=0; k=0; for f in $(<files.lst) do mv $fa${k}b${j}c0.ext # rename happens here if (( $i % 40 == 0 )); then j=$(($j + 1)); k=0; else k=$(($k + 1)); fi i=$(($i + 1)); done
不知道这是一个完全符合你的需求,但看看mmv。 它可以做批量重命名,如:
file1.txt file2.txt file3.txt mmv "file*.txt" "#1_file.txt" 1_file.txt 2_file.txt 3_file.txt
如果你使用像发行版这样的Debian,只需apt-get mmv。