0

I have this dir with multiple *.mp4 files. I would like to add serial no. in order as a prefix, still keeping the original name unchanged. I'm quite new to shell scripting, (also couldnt find a proper answer on google) so It would be nice if someone explains it to me. If it can be done with python, It would be even better!(i know py little bit)

The shell code i wrote:
n=01
for f in *.mp4; do
    mv -- "$f" "$n+01. {f%.mp4}"
done
What my dir looked like:
***.mp4
***.mp4
***.mp4
What it looks like now:
01+01. {f%.mp4}
What I expect to happen:
01. ***.mp4
02. ***.mp4
.
.
12. ***.mp4

Thanking you in advance.

1 Answers1

0

In Python, several ways to do this, for example:

import os

os.chdir(path)  # Enter your desired path like r"D:\My Articles\Phd\ACCRUFER"
allFiles = os.listdir()

mp4Files = [file for file in allfiles if file.endswith('.mp4')]
for i, file in enumerate(files, 1):
    index = str(i).zfill(2)   # to make 1 -> '01' 
    newName = index + '.' + file
    os.rename(file, newName)
    print(file, 'Renamed to', newName)