Introduction to Links

Wrapping my head around links
Table of Contents

Links, links, links everywhere. Everything is about links these days. Let’s talk about the links that may be a little bit more tricky to handle. Yes, I look at you file system links.

Overview

  • There are 2 kinds of links. Hard and soft.
  • The names, implementation and support depend entirely on the File System / Operating System used.
  • The accessibility of the links is defined by where they are implemented.
    • If a link is a file system object it can be interpreted by any application on the system without additional support.
    • If a link is a file it can be interpreted only by the applications / APIs that have support for that file type.

This is a little diagram how I visualize the relationship between the links:

A hard link is a directory entry that associates a name with a file on a file system. All directory-based file systems must have (at least) one hard link giving the original name for each file.
All files are hard links, with link counts of at least 1.
Every directory is itself a file, only special because it contains a list of file names maintained by the file system.
The term “hard link” is usually only used in file systems that allow more than one hard link for the same file. Hard links – that is, multiple directory entries to the same file which independently connect to the same data on the disk, none of which depends on any of the others.
Adding a hard link creates a new name attribute and increases the hard link count (for a newly created file this count equals to one). Deleting a hard link removes the appropriate name and decreases the hard link count. When the count goes to zero, the system deletes the file, freeing up its allocated disk space and releasing its disk inode. All the name attributes are independent, so deleting, moving, or renaming the file doesn't affect other hard links.

Advantages / Disadvantages:

Only valid within the same File System.
May take less disk space as they only take up a directory entry.
Take less time to resolve.
Hardlink, being a link directly to the inode for the file, wouldn't break if you move the file around the file system.
Since directories themselves are files, multiple hard links to directories are possible which would create loops within the structure of the directories, rather than a branching structure like a tree. To prevent loops in the filesystem, and to keep the interpretation of .. (parent directory) consistent, many modern operating systems do not allow hard links to directories.
A soft link (also symbolic link, symlink or “shortcut”) is the nickname for any file that is not a direct link to the data itself.
It contains a reference to another file or directory usually in 2 flavours:
  • A short file that holds the text of a file name
  • A location that gives direct access to yet another file name within some directory in the form of an absolute or relative path and that affects pathname resolution
The name contained in or referred to by the soft link may either be a hard link or another soft link.

Advantages / Disadvantages:

May take more space - soft link needs its own inode to store the name it points to.
May take more time to resolve - soft links can point to other soft links that are in softlinked directories.
Most kinds of soft links will break if you move them around the file system.
Can span file systems as they are simply the name of another file.
Can be used to reference both directories and files.

Typically these names refer to:

Symbolic link - Implemented within the filesystem and thus functional at any level of the OS. An application needs no special support to use a symbolic link. It usually has no size.
Shortcut - Implemented as a small file containing a target URI or GUID to an object, or the name of a target program file that the shortcut represents. The shortcut might additionally specify parameters to be passed to the target program when it is run. Each shortcut can have its own icon. It has a size and requires an application to support that filetype in order to be used because it is a file on the disk that must be interpreted by an API.

Linux / Unix-like

Support hard and symbolic links.

Some desktop environments such as GNOME or KDE provide freedesktop.org .desktop files (a kind of shortcut). These can be used to point to local or remote files, folders, and applications.

Management

To manage links the command line tool ln can be used. By default it creates hard links. To create symblinks you need to specify -s option.

 ln [ -v  | -vs  ] target link_name
 ln [ -v  | -vs  ] target_1 target_2 ... destination
 ln [ -vt | -vst ] destination target
  • For the target or destination don't forget to give the full path - /path/[target|destination] unless you want to use -r
  • -v, --verbose print name of each linked file
  • -s, --symbolic make symbolic links instead of hard links
  • -t, --target-directory=DIRECTORY specify the DIRECTORY in which to create the links
  • -r, --relative create symbolic links relative to link location

Mac OS

Support hard links, symbolic links and aliases (a kind of shortcut).

Alias

An alias is a small file that represents another object in a local, remote, or removable file system and provides a dynamic reference to that object.
The target object may be moved to another place within the same filesystem or renamed, without breaking the link, but the source and target of the link may be on different filesystems.
It is a filesytem object.

Windows / NTFS

Windows links can be divided into 2 categories. You have those on file system level called NTFS reparse points and those interpreted by the Windows APIs.

  • Currently NTFS (the Windows file system) supports hard links, symbolic links and junction points.
  • Additionaly, the Windows APIs can interpret also shortcuts and shell objects
Hard links can only be created to files on the same file system.

Junction Point

An NTFS junction point is a symbolic link to a directory that acts as an alias of that directory.
Junction points can only link to an absolute path and only to a local volume.
Internally they are represented as a mount point.
A filesystem object in the NTFS filesystem that points to another filesystem object called the target. Links appear as normal files or directories, and can be acted upon by the user or application in exactly the same manner.
Symbolic links can be created for any kind of file system object. Symbolic links to files are distinct from symbolic links to directories.
They can link to a relative path and point to a file or remote SMB network path.
Provide full support for cross-filesystem links.
Use the .lnk filename extention to link to local files and folders.
Use the .url filename extention to link to remote files, like web pages.
Operate as Windows Explorer / Windows shell extension.
May break if the target is moved to another location. Explorer will attempt to repair broken shortcuts automatically.
Some shortcuts also store Application User Model IDs (AppUserModelIDs). Instead of the target command line, AppUserModelIDs may directly be used to launch applications. Shortcuts with AppUserModelIDs are used by some desktop programs and all WinRT Modern/Universal Windows Platform apps for launching.

Shell Object

The shell objects or shell folders are defined in the Windows registry and can be used to implement a sort of symbolic link.
They are transparent to the Windows shell.

Management

To manage links on Windows you can use the command line tools mklink and fsutil hardlink or the GUI tool Link Shell Extention.

rem Create a hard link

mklink /H link_name target
fsutil hardlink path/<link_name> path/<source_filename>

rem Create a symblink to a file

mklink link_name target

rem Create a symblink to a directory

mklink /D link_name target

rem Create a junction

mklink /J link_name target
  • /D Creates a directory symbolic link. Default is a file symbolic link.
  • /H Creates a hard link instead of a symbolic link.s
  • /J Creates a Directory Junction.
  • target Specifies the path (relative or absolute) that the new link refers to.

The dir command can be used to find out what kind of link you have . When listing you will see the following outputs:

  • <SYMLINK> - for a symblink to a file
  • <SYMLINKD> - for a symblink to a directory
  • <JUNCTION> - for a junction

To verify if a file is a hard link:

fsutil hardlink list path/<filename>


Thanks for reading my article. It was made possible using these sources.

 
comments powered by Disqus

categories

tags