How Can I Recursively Rename MP3 Files Based on Their Parent Directory?

0
19
Asked By SonicWave2000 On

I have a collection of audiobooks stored in a folder structure that looks like this: there's a main folder for the book, containing subfolders for each disc, and each disc contains multiple MP3 tracks. I'm trying to rename and relocate these tracks so that they follow this new structure: each track should include the disc number in the filename. For example, I want 'track 1.mp3' in 'disc 1' to become 'disc 01 - track 1.mp3'. I know I can use the `mv` command for basic renaming, like `for f in *.mp3; do mv "$f" "CD 1 - $f"; done`, but I'm unsure how to make this work recursively based on the folder names. Any suggestions?

3 Answers

Answered By FileNinja88 On

You’ve got the right structure in mind! Just to clarify what you want:
- Original:
```
book/
├─ disc 1/
│ ├─ track 1.mp3
│ └─ track 2.mp3
└─ disc 2/
├─ track 1.mp3
└─ track 2.mp3
```
- New:
```
book/
├─ disc 01 - track 1.mp3
├─ disc 01 - track 2.mp3
├─ disc 02 - track 1.mp3
└─ disc 02 - track 2.mp3
```
Am I right? If so, I can help with a more detailed code example!

Answered By CuriousCoder42 On

I came across a script called [genFRN](https://github.com/Jim-JMCD/genFRN) that can rename files based on parent directory names. However, it only processes one directory at a time and doesn't work recursively. If you're looking for a script that runs through subdirectories, there’s plenty of advice available on how to accomplish that.

Answered By TechWizard93 On

Here's a script that could work for you! It will find all the MP3 files in the book directory and rename them based on their parent folder:

```bash
find /book -name '*.mp3' | while read -r track; do
cp "${track}" "/book/$(basename "$(dirname "$track")") - $(basename "${track}")"
done
```
This uses the `find` command to locate the MP3 files, and then iterates over each one to create a new filename that includes the disc number.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.