0

Backstory:

I have MP3 and FLAC files in my music collection. I have MP3s organized under music/mp3 and FLACs in music/flac. While I prefer listening to FLAC files, most of my music are MP3s. Several of the songs in music/mp3 are the same songs as ones in music/flac.

When my desktop music player creates a library of the music directory, those songs are duplicated. I want to link my MP3s that are unique to /music/mp3 to their own directory so that my music player can scan it and /music/flac. That way the all songs that I have a FLAC encoding of, and unique MP3s of, will be played.

Question:

I determined which songs are unique to music/mp3, and I have a file of their absolute path names separated by newlines. How do I get the ln command to loop through each line (creating a link to say, music/mp3_unique)?

cheapous
  • 3
  • 1

2 Answers2

0

I don't think ln has the ability to create links from a file, but since you're using bash, you can do this:

for i in `cat file`;do ln -s $i;done

where file is the name of the file with absolute paths

Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • I had a 'duh!' moment once I saw your elegant and simple solution. The point I was missing was to cat the file to create a stream. Thanks! – cheapous Feb 26 '12 at 21:32
  • Oh yeah, and the 2nd argument to ln should be the target directory to link to. – cheapous Feb 27 '12 at 07:26
0

If all of the files are on the same file system, you can use hard links rather than soft. This has the advantage that you no longer need to use absolute path names. In that case, I would be tempted to do something like this:

So... say that you have a file tree that looks something like this:

/music /music/mp3 /music/flac

Create a 'scanme' directory under music, and create hard links to all of the music that you want to play:

mkdir /music/scanme
cd /music/scanme
( cat ../mp3/unique.txt; find ../flac ) | while read file
do
    ln "$file" $(basename "$file")
done

This will work when the file names contain spaces, which a straight for loop won't.

Barton Chittenden
  • 4,238
  • 2
  • 27
  • 46