2

I have files named in the style 02.04.11 DJ Kilbot.mp3 (for several dates) and I want to reformat the name in this manner: DJ Kilbot 2011-02-04.mp3. In other words, the current format is MM.DD.YY DJ-NAME.mp3 and I want to change it to DJ-NAME YYYY-MM-DD.mp3. What's the easiest way to do this, for several year's worth of files?

5

cd to the directory then run the following (using perl-rename). This is a "dry-run" first.

rename -n 's/^([0-9]{2})\.([0-9]{2})\.([0-9]{2}) (.*)\.mp3$/$4 20$3-$1-$2.mp3/' *
02.04.11 DJ Kilbot.mp3 -> DJ Kilbot 2011-02-04.mp3

If you are happy with the output, then run it for real.

rename 's/^([0-9]{2})\.([0-9]{2})\.([0-9]{2}) (.*)\.mp3$/$4 20$3-$1-$2.mp3/' *

Explanation

  • rename -n: run a test "dry-run".
  • 's/FOO/BAR/' substitute the regex FOO and replace with BAR.
  • ^([0-9]{2})\.([0-9]{2})\.([0-9]{2}) (.*)\.mp3$: regex to capture. Match the start of the string ^, then three lots of [0-9]{2} (i.e. two consecutive numbers) separated by a dot (\. when escaped). Then a space and (.*)\.mp3$. Parens () capture the contents for use in the replacement.
  • $4 20$3-$1-$2.mp3: replace with the DJ name the fourth capturing group ($4), or (.*) above, then the rest of the string as specified (i.e. the third, first and second groups).
  • *: act on all files in the directory.

Simplify

This regex has a bit of error checking built in. If you are sure that all files are named consistently, you can simplify it slightly to the following.

rename 's/^(..)\.(..)\.(..) (.*)\.mp3$/$4 20$3-$1-$2.mp3/' *
  • You are welcome. Perl-rename is such a great tool! – Sparhawk Jan 11 '16 at 2:16
3

Without rename:

for file in *.mp3
do
  no_extension=${file%.mp3}
  the_date=$(echo "${no_extension}" | cut -d ' ' -f 1)
  year=20${the_date:6}
  month=${the_date:0:2}
  day=${the_date:3:2}
  date_part=${year}-${month}-${day}
  dj_part=$(echo "${no_extension}" | cut -d ' ' -f 2-)
  new_file="${dj_part} ${date_part}.mp3"
  mv "${file}" "${new_file}"
done

Explanation:

  • for file in *.mp3 loops through every file in the current directory that ends with the .mp3 extension
  • ${file%.mp3} strips the .mp3 extension from the end of the file using bash string manipulation
  • $(echo ${no_extension} | cut -d ' ' -f 2-) extracts the date part of the file name by using the cut utility, which can parse character-delimited strings
  • then we change the format of the date by extracting the substrings
  • "${dj_part} ${date_part}.mp3" is just string concatenation of the parts we've built
  • mv "${file}" "${new_file}" renames the file
  • Excellent answer. I would just recommend using printf %s in place of echo, and quoting the variable no_extension when you use it. – Wildcard Jan 11 '16 at 2:38
0

Assume that the files are named strictly that way.

for file in "*.mp3";do 
  date="${file:0:8}" #get the date in the filename. 
  dj="${file%.mp3}" #strip the extension off the filename. 
  dj="${dj:8}" # get dj name.
  mv -nv $file "$dj $date.mp3" # -n don't overwrite files. 
done
  • 1
    This is nice but it doesn't change the date format : / – user394 Jan 11 '16 at 2:10
0

pyRenamer has a GUI and allows for automatic preview.
Put this into the Original file name pattern field:

{#}.{#}.{#} {X}.mp3

And this into the Renamed file name patter:

{4} {3}-{1}-{2}.mp3

Only downside is that it sorts in "Windows" mode (1, 10, 11, 2, 3, 4 ...).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.