Technical stuff, mostly to remind myself when I forget in a couple of months' time.

Friday, 31 May 2013

Copying music files

My music files are stored on a server; sometimes I want to copy a selection onto sn SD card for playing in the car. The steps are:

  1. Make a playlist on the Squeezebox.
  2. Grab the playlist m3u file.
  3. Remove all the lines that start with the # character: sed '/#/d' playlist.m3u > music.txt
  4. Mount the Samba music share on my server: sudo mount -t cifs -o username=me //192.168.1.2/music /media/music
  5. Do a search and replace in a text editor on the text file so that the paths all reference /media/music
  6. Copy the files: first set IFS to a new line, then: for file in $(<music.txt); do cp -v "$file" music/; done
Then I run the files through Sound Converter because the player in my car can only deal with mp3 files.

Wednesday, 6 March 2013

"Editing" symlinks on Linux

Firstly, let's be clear. There *is* no way of editing symlinks. All you can do is delete them and recreate them. However, there is a way of automating this process.

I had lots of photos on one disk and then moved them. The links to the files needed changing en masse and I didn't want to have to redo it by hand. What I needed was a way of finding out where the link *used* to point to and use that to construct the new link.

The answer was readlink. Thus:
#!/bin/bash
for fn in *.jpg; do
    NEWFN=$(readlink $fn | sed 's,/media/d1,/media/d2,')
    ln -sf $NEWFN $fn
done
Pipe the data from readlink into sed, which is used to replace an occurrence of the string /media/d1 with /media/d2. Then recreate the link, using the -f flag to delete an existing link.

Easy. Full credit to this post on SuperUser.

Your Host

Husband, father, pop music anorak, guitarist