GNU `rename` - 在同一文件名中多次出现数字键盘

GNU `rename` - number pad multiple occurrences within the same file name

GNU 实用程序的高级使用 rename

TLDR: Number pad every number occurrence delimited by a .
Example 11.2.7 to 11.02.07

注意:不是递增的 - 我希望保留现有数字,因为它们引用了章节

这里有一个文件列表

1.1.4 Video - Anatomy of an IoT Attack-hspse3ffdx.mp4
11.1.11 Video - MAC Address Tables on Connected Switches-r4jd8ywqw8.mp4
11.1.2 Video - End devices-egvgnpczdg.mp4
11.1.8 Video - Static and Dynamic Routing-9hmp3pmjgs.mp4
11.2.1 Video - WLAN Operation-jgvaoufho4.mp4
12.1.6 Video - Three-Layer Network Design-pqo7z0dofi.mp4
12.2.1 Video - Security Devices-ba1rn27np7.mp4
12.3.1 Video - Security Services-doe61o7ndw.mp4
14.2.5 Video - Access and Social Engineering Attacks-ff2jvdisrh.mp4
14.3.1 Video - Denial of Service Attacks-0xc6nbzpnd.mp4
14.3.4 Video - Mirai Botnet-q22ur5mjby.mp4
14.6.2 Video - TCP Reliability - Sequence Numbers and Acknowledgments-12v657687z.mp4
16.2.3 Video - Amplification, Reflection, and Spoofing Attacks-gqc5r60t0x.mp4
21.2.8 Video - Cryptography-sd8nizpdl1.mp4
22.3.4 Video - Demonstration - Using a Sandbox to Launch Malware-4gfqq7cg6h.mp4
27.2.11 Video - Isolate Compromised Host Using 5-Tuple-41pcc9yx2j.mp4
27.2.13 Video - Interpret HTTP and DNS Data to Isolate Threat Actor-ke3dqr55hr.mp4
4.7.7 Video - Applications, Rootkits, and Piping Commands-7pptcz20yp.mp4
6.3.5 Video - Network, Host, and Broadcast Addresses-18zcrbwlfn.mp4
6.6.6 Video - Layer 2 and Layer 3 Addressing-k45ocxkbc5.mp4
7.2.1 Video - Network Testing and Verification with CLI Commands-asimubm3xa.mp4
8.2.2 Video - Sample IPv4 Headers in Wireshark-n29n6dgzc2.mp4
8.2.3 Video - ARP Operation - ARP Request-34um4wqbzd.mp4
8.2.4 Video - ARP Operation - ARP Reply-ghtdvvay1o.mp4
8.2.5 Video - ARP Role in Remote Communications-aaod3u3jsh.mp4
8.3.2 Video - ARP Spoofing-4drx7ijaex.mp4
8.3.5 Video - Sample IPv6 Headers in Wireshark-ibw3std2hh.mp4
9.2.5 Video - 3-Way Handshake-hsypao6yat.mp4
9.3.4 Video - TCP Reliability - Data Loss and Retransmission-ieh5e8m9k4.mp4

如果我们使用 rename 's/\d+/sprintf("%02d",$&)/e' *.mp4 我们只能部分满足我们的目标,因为只有第一个数字出现被填充:

01.1.4 Video - Anatomy of an IoT Attack-hspse3ffdx.mp4
04.7.7 Video - Applications, Rootkits, and Piping Commands-7pptcz20yp.mp4
06.3.5 Video - Network, Host, and Broadcast Addresses-18zcrbwlfn.mp4
06.6.6 Video - Layer 2 and Layer 3 Addressing-k45ocxkbc5.mp4
...etc

我希望将出现的前 3 个数字从 x.x.x 填充到 xx.xx.xx

目标是有如下数字填充:

01.01.04 Video - Anatomy of an IoT Attack-hspse3ffdx.mp4
... etc

我建议:

rename -n 's/^([0-9]{1,2})\.([0-9]{1,2})\.([0-9]{1,2})/sprintf("%02d.%02d.%02d",,,)/e' *.mp4

如果一切正常,请删除 -n


参见:The Stack Overflow Regular Expressions FAQ