-1

I need a Bash script that will create a symlink for every *.mp3 file in folder X (and its subfolders), and store those symlinks in folder Y, without subfolders and probably skipping duplicates.

For the curious, this is to automate a radio station using Libretime.

And sorry if this is a dumb question, I never used a Bash script.

Aulis Ronkainen
  • 164
  • 2
  • 4
  • 12
ANIMATEK
  • 71
  • 1
  • 1
  • 7

1 Answers1

2

As in the comment: use find to create a list of the mp3-files:

find /top/dir/for/mp3s -name '*mp3'

You will want to use that output to loop over it, so:

find /top/dir/for/mp3s -name '*mp3' | while read mp3file, do
    # do the linking
done

You want create a link in a specific directory, probably with the same filename. You can get the filename with basename. So, that would make it something like this:

find /top/dir/for/mp3s -name '*mp3' | while read mp3file; do
    filename=$(basename $mp3file)
    ln -s $mp3file /dir/where/the/links/are/$filename
    echo Linked $mp3file to /dir/where/the/links/are/$filename
done

However, this will probably give you two types of error:

  • If the mp3 filename contains spaces, basename will not produce the correct filename and ln will complain. Solution: use correct quoting.
  • If you have duplicate filenames, ln will complain that the link already exists. Solution: test if the link exists.

Because you're not destroying anything, you can try it and actually see the problems. So, our next iteration would be:

find /top/dir/for/mp3s -name '*mp3' | while read mp3file; do
    filename=$(basename "$mp3file")
    if [ ! -l "/dir/where/the/links/are/$filename" ] ; then
        ln -s "$mp3file" "/dir/where/the/links/are/$filename"
        echo "Linked $mp3file to /dir/where/the/links/are/$filename"
    else
        echo "Not linked $mp3file; link exists"
    fi
done

That should give you a fairly good result. It also gives you a good starting point.

Ljm Dullaart
  • 4,273
  • 2
  • 14
  • 31