Search code examples
linuxbashshellloopsln

How Do I Create Links from Filepaths in a File?


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)?


Solution

  • 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