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.

3
  • Please check : stackoverflow.com/a/23229147 iterate and use mv command Commented Nov 14, 2022 at 6:23
  • It is not clear from your question: Do you have problems looping over files, or, given a single file, do you have problems renaming it? Commented Nov 14, 2022 at 8:20
  • Have a read here stackoverflow.com/a/56579912/2836621 Commented Nov 14, 2022 at 9:28

1 Answer 1

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) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.