-1

I have a directory which contains the following type of files:

01.firstname surname-track1 name1.mp3
02.firstname surname-track2 name2.mp3
03.firstname surname-track3 name3.mp3

I'm looking for a BASH command to batch rename all my files in the directory so that there is a space before and after the dash, such as:

01.firstname surname - track1 name1.mp3
02.firstname surname - track2 name2.mp3
03.firstname surname - track3 name3.mp3

Any help most appreciated!

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
ktb
  • 35
  • 5
  • 1
    Use the `rename` command, it allows you to provide a pattern-based replacement. – Barmar Sep 21 '19 at 14:23
  • 1
    Welcome to Stack Overflow! [so] is for programming questions, not questions about using or configuring Unix and its utilities. [unix.se] or [su] would be better places for questions like this. – Barmar Sep 21 '19 at 14:23

2 Answers2

1

As proposed Barmar, you can use rename (or file-name). In your case :

file-rename 's/-/ - /g' *.mp3

It's regular expression based so it's very powerfull (and therefor dangerous), before running it, you can run it "dry", it will show you what your command is about to change :

file-rename -n 's/-/ - /g' *.mp3
Django Janny
  • 610
  • 8
  • 13
  • Thank you, but this doesn't quite get what I'm after, it gives: 01 - firstname surname - track1 name1.mp3 etc etc... Can you make it so it gives: 01. firstname surname - track1 name1.mp3 ? Your command is getting rid of the period after the track number, and I'd like to keep that ideally. Thanks! – ktb Sep 21 '19 at 16:22
  • 1
    Which `rename` are you using ? the one from utils-linux doesn't support regular expressions. – Sorin Sep 22 '19 at 18:12
  • It's a perl package : /usr/bin/file-rename using File::Rename version 0.20 – Django Janny Sep 23 '19 at 22:03
  • I'm just using the standard one with Ubuntu 18.04LTS. Should I install a different one? – ktb Sep 25 '19 at 17:42
0

You can use bash parameter substitution:

for i in *.mp3; do mv $i ${i/-/ - }; done

Alternatively, you can use rename (from util-linux package)

rename '-' ' - ' *.mp3

Or prename (some distributions have this as rename i think, based on the above answer)

prename 's/-/ - /' *.mp3