Search code examples
bashmacosbatch-rename

Batch rename multiple files from different path


I have a file directory music/artist/{random_name}/{random_music}.ogg

There's a lot of folder in {random_name} and different kind of music title {random_music}.

So, I wanted to rename the {random_music}.ogg to music.ogg. Each {random_name} folder only have one .ogg files.

I've tried with the Bash scripts for hours but didn't managed to find out.

for f in ../music/artist/*/*.ogg
do
  echo mv  "$f" "${f/.*.ogg/music.ogg}"
done

It only rename the file on my current dir, which will ask for replace/overwrite.

My goals is, I wanted to rename all the {random_music}.ogg files to music.ogg with their respective directories for example,

music/artist/arai/blue.ogg to music/artist/arai/music.ogg

music/artist/sako/sky.ogg to music/artist/sako/music.ogg


Solution

  • Your pattern replacement is incorrect. Because all your paths start with .., .*.ogg actually matches the entire path, so every file gets turned into music.ogg in your current directory.

    You want ${f/\/*.ogg/music.ogg} instead, or better yet, ${f%/*}/music.ogg. That's the rough equivalent of "$(dirname "$f")"/music.ogg.