Symbolic Links – Making life better using symlinks

On my server I have more than a dozen different volumes and none of my videos are organised in a family-friendly way, I’ve simply stored things on my server in the most efficient way from a storage point of view. Well, actually I just started filling drives up with my videos as I went along and once one drive was full I moved onto the next one. When I ran out of space completely I bought more drives. Not untypical of the way many people use their storage I’d imagine.

However, the downside of this strategy is that only I know what is stored where on those drives. I could of course move the files around to try and make things more logical (eg. have all movies which have a title starting with the letters “A” to “C” on one drive etc) but this takes time, plus of course if I ever bought more movies than would fit into a category on the one drive then I’d be back with the same “mess”.

So, to make life easier for my family to find what they are looking for when using their streamers I’ve created several core shares: Photos, Music, Movies, TV Shows. Within those core shares I’ve created symbolic links (symlinks) which point to the various locations on my server where the files are physically stored. This way it is very easy for anyone in my family to find what they are looking for without having to guess, or ask me where it is.

Browsing videos on the NTV550

You might be asking yourself “why bother with all this”? Well, there are two main ways to browse your movies on the NeoTV 550 (or indeed on the EVA series of streamers): From the main menu you can go into Video -> Folders or you can go into Video -> All x video(s). The latter menu option collates all the videos on your shares into a nice long list. This is great in that it overcomes the above issue where nothing is stored in a logical way. However, the Neo will collate ALL video files into the one list. So, my TV Shows will be in amongst by DVDs & Blu-rays. Furthermore, the Neo flattens the folder structures and so there would be loads of “Season x” folders in the list for each of the seasons of my TV series. The other way you can browse your Videos (Video -> Folders) will overcome the collation problem (and so my TV Shows will be separate from my DVDs and Blu-rays) but then I’d hit the problem of not being able to quickly find what I’m looking for because nothing is organised logically on my drives (Remember, the Folders view is very similar to browsing from your desktop computer).

It’s probably easier to explain this issue with a screenshot:

Many Drives

As you can see from the screenshot on the left we have a couple of the drives on my server. So, to find a Toy Story movie you’d have to know it was stored on the HD203WI drive. To find Click you’d have to know it was stored on the WD20EADS drive. I’m sure you’ll agree this is not very intuitive.

To overcome this problem we could of course create one huge volume (using something like LVM, mhddfs or perhaps even RAID) but I’ve already explained elsewhere in these guides why I’ve not done this so I will not repeat myself here. Furthermore, if you are storing your movies on an off-the-shelf NAS then it’s likely you won’t be able to use LVM or mhddfs anyway.

The simplest strategy, and by far the safest, is to use symlinks to make all the files on all the different volumes appear under a single “drive” or share. This means we can then browse by Video -> Folders and everything will appear in a nice structured, logical, way. So, I would have my TV Shows separated from my DVDs and these would be separated from my Blu-rays. Furthermore, each of my TV Series would appear in the relevant folder rather than having all the Seasons being displayed individually.

So, as can be seen in the screenshot below, with the use of symlinks the above drives (plus others which I’ve not included screenshots for) would appear under one drive or share. I can now remove all the existing shares from the Neo and point my Neo at this new “drive” or share instead.

Symlinks in action

Great, but how do I create these symlinks?

Creating a symlink on a unix-based system or on MS Windows Vista or later is simply a matter of typing a single command. If you want to create many symlinks in one go then you’ll probably use a script. I’ve written such a script which creates hundreds of symlinks in one go and I run this script regularly to ensure that as and when there are changes to the underlying folders (additions, deletions, moves etc) the symlinks are updated to reflect these changes. For info, my server is constantly recording TV programmes and so dozens of new folders are added daily. If you rarely change the underlying folders then you could probably run the script as and when you’ve made any changes rather than on a schedule. To clarify, the script creates symlinks to folders, not individual files, and so you only need to run it when folders change not when individual files within those folders are added/deleted.

Unix-based symlinks

On a unix-based system the syntax to create a symlink is as follows:

ln -s name_and_location_of_original_folder_or_file new_name

So, using Putty or from within a Terminal Session typing the following command would create a symbolic link named Movies A-C pointing to /media/WD20EADS/volume3/rips/ISOs/part1_of_5/

ln -s /media/WD20EADS/volume3/rips/ISOs/part1_of_5/ "Movies A-C"

Note that if either the source or destination path/filename has spaces in it then you must enclose them in double-quotes.

Windows-based symlinks

On Windows Vista and later you can create Symlinks using the mklink command. The syntax of the mklink command is: mklink /D new_name name_and_location_of_original_folder_or_file

If you are creating a link to a file and not to a directory then omit the /D.

So, typing the following command would create a symlink called Movies in the Media folder which points to a folder called C:/Documents and Settings/hkth/My Documents/Videos/rips/ISOs/.

mklink /D "C:\Media\Movies" "C:/Documents and Settings/hkth/My Documents/Videos/rips/ISOs/"

As above, if the source or destination path or filename has spaces in it then you must enclose it in double-quotes.
You would then make the C:\Media folder shareable and point the Neo to this folder. When you then browse by Folders view on the Neo you would see a top level folder called Movies within the Media share.

Symlinks in action

So, here we can see a selection of my DVDs as they would appear when viewed on my streamer: 

Nice Folder Layout

And the following screenshot shows how things look “behind the scenes”.  So for example, you can see that Swordfish lives in /media/WD203WI/RAIDMain/Pre-recorded_DVDs/Swordfish whereas Team America lives on a different drive completely. It lives in /media/WD20EADS/RAIDMain/Videos/Do_not_have_originals/Team America but from the above screenshot you’d never know that.

This screenshot reminds me of another job I must do: Recently I went through my entire collection and re-tagged all my movies from scratch. I then fine-tuned the tags by referring to the movie sleeve from each disc case. There were many movies I’d ripped onto my server that I couldn’t find the original discs for. I moved said rips into a folder called Do_not_have_originals on each drive. Seeing this screenshot reminds me that I really need to search my house for them!

Underlying folder structure

Using a linux script to create the symlinks

Here you’ll find the script which you can use to create the symlinks. I’ll explain the key points in the script so you can edit it as required.

The section of code between the #DO NOT TOUCH THIS SECTION and the #END OF – DO NOT TOUCH THIS SECTION comments should not be touched unless the code is not working properly.

The user configurable section starts on line 85 (ie. starting after #EDIT THESE VALUES AS REQUIRED)

You simply replicate a sub-section in the script for each entry point. By entry point I mean the top level folder(s) when browsing on the Neo. So this could be a share or indeed a folder within a share. As can be seen in the script I have several entry points: DVDs, Blu-rays and TV Shows.

The sub-routine, called CreateTheLinks which does the actual work of creating the links, takes several parameters:

Clear down the existing links in this folder before creating new ones?: TRUE/FALSE
Setting to FALSE is handy if you want to run the script with different levels of drill-down for the same destination folder. Normally leave as TRUE.

Maximum number of folder levels to drill-down into the SourceFolders folder(s). In my setup I have each movie or TV Series in a separate folder within the SourceFolder folder. So I’d specify 1 for this parameter.

MinDepth illustrative example

Number of folder levels to ignore from the SourceFolder folder downwards. Normally you’d set this to 0. However, if you have your videos in category folders within the SourceFolder then you can specify a number greater than 0 to ignore the categories. Consider the screenshot on the right:

I want to create links for the videos in each of the main subfolders found in the RAIDMain\Videos Folder. i.e. I want links for everything in Do_not_have_originals, Prerecorded_DVDs folders etc. but do not want these main sub-folders linked. I could of course specify each of these main subfolders in the script itself and set the parameter to 0. The trouble is, if I did that and then added a new main subfolder at a later date then I would have to remember to update the script too else it would not create links for the videos in the new subfolder. So, by specifying 1 for this parameter the script will skip the main subfolders and instead create links for everything found inside the folders beneath them.

You do not need to touch the last two parameters “$DestFolder” and “${SourceFolders[@]}” so they can be ignored.

Once you have edited the script you need to make it executable:

sudo chmod a+x YOURSCRIPTNAMEGOESHERE

and we run it as follows:

./YOURSCRIPTNAMEGOESHERE

Using a Windows script to create the symlinks

Hmm, well. I don’t currently own a MS Windows Vista or later machine so I’m afraid I can’t write you a script. If you’re desperate for one then head over to my forum and ask me and I’ll see if I can get my hands on a copy of Windows and knock you up a script. Alternatively if someone wants to write such a script and is happy to share it then I’ll be happy to host it.