Linux – Operating System Agnostic Symlink/Alias/Shortcut

linuxmacosshortcutssymbolic-linkwindows

I have a folder that I sync with online services such as Dropbox, that I sync to other computers with different operating systems. Part of my file/folder organization in this folder uses symlinks/aliases (my home computer is a Mac). When it syncs the folder to a Windows computer, unfortunately symlinks and aliases are not recognized, but shortcuts are.

Is there a way to create a symlink/shortcut once on a Mac or Windows with cross-platform compatibility? Otherwise I'll have to create two links per file, once for Mac/Linux and one for Windows.

NOTE: The purpose of this folder, because I know someone will ask, is to organize and store a lot of files in a hierarchy of folders. Occasionally, a file or folder seems to fit into two or more places, hence the symlink. This is a very common practice on Mac/Linux.

Best Answer

According to this How-to-geek post, you can use mklink from cmd (Windows Vista - 10) to make symlinks that also work in linux. The guide also has a way to install an tool that can make linking easier (by adding it to the right-click menu.)

The command-line syntax is:

mklink /prefix link_path file/folder_path 

Where prefix can be:

/D – creates a soft symbolic link, which is similar to a standard folder or file shortcut in Windows. This is the default option, and mklink will use it if you do not enter a prefix.

/H – creates a hard link to a file

/J – creates a hard link to a directory or folder

An example that I just did:

mklink /J D:\Dropbox\school\archive\14Winter\cs355 D:\Dropbox\school\classes\cs355

Which makes a link in 14Winter called cs355 to the cs355 in classes

After creating the link on Windows, the link worked on my Linux box, too. I have not tried Mac. If you find it doesn't work for mac, let me know.

EDIT: Old answer

Here is a dumb, but possibly useful solution: You could write a little python script that is an agnostic link (Code found here)

#!/usr/bin/python
import subprocess
import sys

if sys.platform == 'darwin':
    def openFolder(path):
        subprocess.check_call(['open', '--', path])
elif sys.platform == 'linux2':
    def openFolder(path):
        subprocess.check_call(['gnome-open', '--', path])
elif sys.platform == 'win32':
    def openFolder(path):
        subprocess.check_call(['explorer', path])

openFolder("relative/path")

This will, of course, always open a new window. Not a great solution, but it may be all you get. (Keep praying that one day windows will be Linux based... or just die.)

Related Question