2

Files in directory :

$ ls | sort -n

1.mp4
2 .mp4
3 .mp4
4 .mp4
5 .mp4
6 .mp4
7 .mp4
8 .mp4
9 .mp4
10 .mp4

A list of file names in a playlist file :

$ 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

The file names start with numbers. How do I rename the files in directory by names listed in playlist file with their corresponding numbers so the outcome would be :

$ 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
7
  • You can use mv or rename and for loop in shell script for example. – Arkadiusz Drabczyk Feb 24 '20 at 14:03
  • Or maybe paste? – Arkadiusz Drabczyk Feb 24 '20 at 14:05
  • 2 .mp4 or 2.mp4? – Arkadiusz Drabczyk Feb 24 '20 at 14:06
  • @Arkadiusz , it's 2 .mp4 , yes there is gap between – Just Khaithang Feb 24 '20 at 14:10
  • @LinuxSecurityFreak, Just wanted specify I am open to any type of solutions. – Just Khaithang Feb 24 '20 at 14:11
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
1

If you have bash4+, here is one way using 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

Output on 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'

Here is the oneliner

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
0

You could use paste like that:

paste <(ls -1 *mp4 | sort -n) playlist.m3u8  | sed 's,\t,\t",' | sed 's,^,",' | sed 's,mp4,mp4",g' | xargs -L1 mv

Result:

$ 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

Although it would work in this particular case notice that parsing output of ls is not a good idea.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.