目录中的文件:
$ ls | sort -n
1.mp4
2 .mp4
3 .mp4
4 .mp4
5 .mp4
6 .mp4
7 .mp4
8 .mp4
9 .mp4
10 .mp4
播放列表文件中的文件名列表:
$ cat playlist.m3u8
1- Introduction-OxIDLw0M-m0.mp4
2 - How React Works-pKYiKbf7sP0.mp4
3 - React Setup (with CDN)-SAX6RMEFVM4.mp4
4 - React Components-Fis_Q3rkgtM.mp4
5 - State-yuN4EMjR4K4.mp4
6 - React Dev Tools--XQ2zCdxw0I.mp4
7 - DOM Events--ZB8I2PmiOw.mp4
8 - Changing State (and 'this')-XJzDF9bj368.mp4
9 - Intro to Forms-BVbdZ1133JU.mp4
10 - Create React App-5QwNCX3UbXc.mp4
文件名以数字开头。如何按播放列表文件中列出的名称及其相应的编号重命名目录中的文件,结果将是:
$ ls | sort -n
1- Introduction-OxIDLw0M-m0.mp4
2 - How React Works-pKYiKbf7sP0.mp4
3 - React Setup (with CDN)-SAX6RMEFVM4.mp4
4 - React Components-Fis_Q3rkgtM.mp4
5 - State-yuN4EMjR4K4.mp4
6 - React Dev Tools--XQ2zCdxw0I.mp4
7 - DOM Events--ZB8I2PmiOw.mp4
8 - Changing State (and 'this')-XJzDF9bj368.mp4
9 - Intro to Forms-BVbdZ1133JU.mp4
10 - Create React App-5QwNCX3UbXc.mp4
答案1
while read -r i ; do
file=$(echo $i | cut -d- -f1).mp4
if [ -e "$file" ]; then
mv "$file" "$i"
fi
done < playlist.m3u8
for i in ?*.mp4; do
mv "$i" "$(grep -xm1 "${i%.*}"'-.*\.mp4' playlist.m3u8)"
done
答案2
如果您有bash4+
,这是使用mapfile
aka 的一种方法readarray
#!/usr/bin/env bash
shopt -s nullglob
rawfiles=(*.mp4)
mapfile -t files < <(printf '%s\n' "${rawfiles[@]}"| sort -n)
mapfile -t playlistfile < <(sort -n playlist.m3u8)
for i in "${!files[@]}"; do
mv -v "${files[$i]}" "${playlistfile[$i]}"
done
Linux 上的输出
renamed '1.mp4' -> '1- Introduction-OxIDLw0M-m0.mp4'
renamed '2 .mp4' -> '2 - How React Works-pKYiKbf7sP0.mp4'
renamed '3 .mp4' -> '3 - React Setup (with CDN)-SAX6RMEFVM4.mp4'
renamed '4 .mp4' -> '4 - React Components-Fis_Q3rkgtM.mp4'
renamed '5 .mp4' -> '5 - State-yuN4EMjR4K4.mp4'
renamed '6 .mp4' -> '6 - React Dev Tools--XQ2zCdxw0I.mp4'
renamed '7 .mp4' -> '7 - DOM Events--ZB8I2PmiOw.mp4'
renamed '8 .mp4' -> '8 - Changing State (and '\''this'\'')-XJzDF9bj368.mp4'
renamed '9 .mp4' -> '9 - Intro to Forms-BVbdZ1133JU.mp4'
renamed '10 .mp4' -> '10 - Create React App-5QwNCX3UbXc.mp4'
这是单线
rawfiles=(*.mp4); mapfile -t files < <(printf '%s\n' "${rawfiles[@]}"| sort -n); mapfile -t playlistfile < <( sort -n playlist.m3u8); for i in "${!files[@]}"; do mv -v "${files[$i]}" "${playlistfile[$i]}"; done
答案3
你可以paste
这样使用:
paste <(ls -1 *mp4 | sort -n) playlist.m3u8 | sed 's,\t,\t",' | sed 's,^,",' | sed 's,mp4,mp4",g' | xargs -L1 mv
结果:
$ ll
total 4.0K
-rw-r--r-- 1 ja users 0 Feb 24 15:09 9 - Intro to Forms-BVbdZ1133JU.mp4
-rw-r--r-- 1 ja users 0 Feb 24 15:09 8 - Changing State (and 'this')-XJzDF9bj368.mp4
-rw-r--r-- 1 ja users 0 Feb 24 15:09 7 - DOM Events--ZB8I2PmiOw.mp4
-rw-r--r-- 1 ja users 0 Feb 24 15:09 6 - React Dev Tools--XQ2zCdxw0I.mp4
-rw-r--r-- 1 ja users 0 Feb 24 15:09 5 - State-yuN4EMjR4K4.mp4
-rw-r--r-- 1 ja users 0 Feb 24 15:09 4 - React Components-Fis_Q3rkgtM.mp4
-rw-r--r-- 1 ja users 0 Feb 24 15:09 3 - React Setup (with CDN)-SAX6RMEFVM4.mp4
-rw-r--r-- 1 ja users 0 Feb 24 15:09 2 - How React Works-pKYiKbf7sP0.mp4
-rw-r--r-- 1 ja users 0 Feb 24 15:09 10 - Create React App-5QwNCX3UbXc.mp4
-rw-r--r-- 1 ja users 0 Feb 24 15:09 1- Introduction-OxIDLw0M-m0.mp4
尽管在这种特殊情况下它会起作用,但请注意解析 ls 的输出不是一个好主意。