2

I have a bunch of mp3s I've organised into a playlist for a party. What I'd like to be able to do is easily rename these files so that they start with the position in the playlist:

001.Track.mp3
002.Track.mp3
.
.
.

Then they can simply be played in alphabetical order. Is this possible without just doing it manually?

3 Answers 3

2

You could try something like this: I'm not sure if it needs something more but this should work.

    cat playlist.m3u | I=1 while read file; do
        mv $file $I.$file.mp3;
        I=$I + 1;
    done;

Edit: You could need I=`expr $I + 1'; instead of I=$I + 1;

3
  • maybe check first if $file exist
    – Emmanuel
    Jun 28, 2013 at 15:41
  • Yes you're right, if you have to use it more often this should be done.
    – Falk
    Jun 28, 2013 at 15:44
  • Of course! How nice and simple. Should have occurred to me! Thanks :)
    – kevtufc
    Jun 28, 2013 at 15:51
2

Assuming you have a file my_playlist.txt that contains the tracks in the order you want to play them, e.g.,

dancing all night long.mp3
charles trenet - je t'aime mon amour.mp3
mon petit poney - bo.mp3
charlie parker - donna lee.flac

and assuming that this file is in the same directory you have your music files. If this is not the case, please specify your question accordingly.

Then (observe good practice that will work with file names that have spaces in them):

#!/bin/bash

pl=my_playlist.txt

# need number of files, to know the number of leading zeros
n=$(wc -l < "$pl")
lz=0
while ((n)); do ((++lz)); ((n/=10)); done

i=1
while read f; do
    [[ -z $f ]] && continue
    if [[ ! -f $f ]]; then
        printf "*** Error, file \`%s' not found.\n" "$f"
        continue
    fi
    printf -v target "%0*d.%s" "$lz" "$i" "$f"
    echo mv -vn -- "$f" "$target"
    ((++i))
done < "$pl"

If you run it as is, the renames will no occur, because of the echo in front of mv. This is just a little test. When you're happy with it, remove the echo and the rename will take place. The mv command has the two flags -v and -n:

  • -v: be verbose (I like to know what's happening)
  • -n: no clobber, so as to not overwrite otherwise existing files (safer)

Note about the printf command. It looks a little bit ugly, but there's nothing to be scared about. The part %0*d will take the two arguments $lz and $i and will be replaced by the number $i with $lz leading zeros. Then the %s will be replaced by the third argument, which is the file name. All this will be put in the variable target, thanks to the -v option. So that target will be something like:

0001.title of the file.mp3

Adapt and extend to your needs!


Now let's assume you've been playing with that, and you want to undo the renaming... there are two possibilities:

  1. You only have files of the form XXXX.YYYY where XXXX are digits, and you'd like to remove the digits and the period from the name:

    shopt -s extglob
    for i in +([[:digit:]]).*; do
        target=${i#*.}
        echo mv -nv -- "$i" "$target"
    done
    

    and remove the echo when you're happy.

  2. You only want to do it for the files in your playlist my_playlist.txt:

    shopt -s extglob
    while read f; do
        a=( +([[:digit:]])."$f" )
        if ((${#a[@]}==0)); then
            printf "*** Error: can't find a match for file \`%s'\n" "$f"
        elif ((${#a[@]}>1)); then
            printf "*** Error: found several matches for file \`%s':\n" "$f"
            printf "    %s\n" "${a[@]}"
        else
            target=${a[0]#*.}
            echo mv -nv -- "$f" "$target"
        fi
    done < my_playlist.txt
    

    and as usual, remove echo when you're happy.

1

I use Dolphin, a replacement for Nautilus and all I need to do is select the files I would like to rename, right click on the selection and...

enter image description here

Hope this helps

2
  • I'm assuming that this is going to name the files in the order they're already in though? I want to rename in an order from a playlist.
    – kevtufc
    Jun 28, 2013 at 15:52
  • Ah, that's a different matter. I'll take a look :)
    – Simon
    Jun 28, 2013 at 16:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .