Location>code7788 >text

Hard Links, Directory Joins (Soft Links), Symbolic Links, Shortcuts in Windows

Popularity:16 ℃/2024-09-30 17:53:36

Hard Link and Symbolic Link are often mentioned in Linux file system, Windows can also create links, but due to the rich graphical interface operation, links are seldom mentioned.Windows NTFS file system supports three kinds of links: Hard Link, Symbolic Link and directory link (junction point), in addition, there is a very familiar link mechanism: shortcut. The NTFS file system on Windows supports three types of links: Hard Link, Symbolic Link, and Junction Point, in addition to a very familiar linking mechanism: shortcuts.

Creating Links

Links can be created with the dos command mklink or with New-Item in powershell.
The mklink command is described in the following figure. The default is to create a symbolic link to a file, using the/D parameter creates a symbolic link to a directory, using the/H is to create hard links using the/Jis to create a directory link, also known as a soft link.
image

Links are created using powershell as follows.

New-Item <LinkPath> -ItemType <LinkType> -Target <LinkTarget>.

The value of ItemType is optional: HardLink, SymbolicLink, Junction.

Difference between several types of links

Similar to inodes and blocks in the Linux filesystem, data objects in the NTFS filesystem are given uniqueDocument ID as well asPath to the file that corresponds to itIn addition, the file path corresponds to the file ID, and the file ID is bound to the data object before it is finally presented as a file that can be opened and edited by the user.

Shortcut

The shortcut is based on.lnkIt exists as a file, with a file size of only a few hundred bytes, independent of the original file size. Suitable for applications such as Explorer, not a built-in mechanism of NTFS, supported since Win95, also supported in FAT32. FAT32 is also supported. Applies to files and directories, only absolute paths can be used. Can cross disk drives, can cross hosts, can use UNC paths, network drives.

symbolic link

A symbolic link is a path that links itself to a target file or directory. When the system recognizes a symbolic link, it jumps to the target pointed to by the symbolic link without changing the file path at that point.
image

Symbolic links are supported since Vista, NTFS built-in mechanism. Applies to files, directories. Can be interpreted as another form of shortcut, with a file size of 0 bytes and no space. Can use relative/absolute paths, across disk drives, across hosts, UNC paths and network drives.

hard link

The principles of hard links and symbolic links are completely different. Symbolic links are links that point to the target path, while hard links are links that point to the target data object. Since all data objects in a volume have a unique file ID, it can be said that a hard link is a link to the target file ID.
image

Hard links are supported since Windows NT4 and are a built-in mechanism for NTFS, not supported by FAT32. It only applies to files, and can only use absolute paths. There is no file itself, and it does not take up any extra space. hardlink and targetfile must be located in the same volume, which can be simply understood as not being able to cross disk symbols.

Catalog Link

Directory joins have been supported since Windows 2000/XP and are a built-in mechanism of NTFS. Applies only to directories. Only absolute paths can be used. Directory linking is done via thereanalyze pointimplementation, directory links can span volumes, but not hosts.

detailed comparison

A detailed comparison of several linking methods is shown in the table below

shortcut hard link junction point symbolic link
Creation method Right click -> Create Shortcut mklink /H Link Target mklink /J Link Target mklink /D Link Target
mode of existence Exists as a .lnk file for applications such as Explorer. Non-NTFS built-in mechanism, supported since Win95. supported by FAT32. NTFS built-in mechanism, supported since Windows NT4. not supported in FAT32. NTFS built-in mechanism, supported since Windows 2000/XP. is a feature of NTFS 3.0 and above file systems (Windows 2000 and above) that serves as an access point for linking local directories (which can span volumes), and operations through the handoff point are mapped by the system to the actual directory. By creating a handoff point, it is possible to allow a user or program to access this directory from multiple locations in the local file system while maintaining a single directory instance (directory consistency). NTFS built-in mechanism, supported since Vista. The file type is .SYMLINK
Scope of application Also applies to files and directories, only absolute paths can be used. For files only, only absolute paths can be used. Only for directories. Only absolute paths can be used. Even if a relative path is used when creating a junction point, it will be implicitly converted to an absolute path when saving to NTFS. Also works with files, directories. This is a super shortcut with a file size of 0 bytes and no space.
Limitations on use You can cross disk drives, you can cross hosts, you can use UNC paths, network drives. The hard link and targetfile must be located in the same volume, which can be simply understood as not being able to cross disk drives. The junction point must be on the same local computer as the target directory, which simply means that it cannot cross hosts, and within the local computer, it can cross disk drives. UNC paths cannot be used; assuming that Z is a network mapped disk, the same does not apply to Z. Relative and absolute paths can be used. Assuming that a relative path was used when creating the symbolic link, what is saved to NTFS is the relative path and will not be implicitly converted to an absolute path. Can cross disk drives, can cross hosts, can use UNC paths, network drives.
mobility There are files in itself that can be copied, moved, etc. / / /
file The file size is only a few hundred bytes, independent of the original file size, and the file type is .lnk. No files per se, create multiple entries for files. Since different files point to the same data, no matter how many hard links are created to the same file, they take up the same size of data for the entire volume. Create, delete, and modify files and subdirectories in the handoff point are mapped to the files and subdirectories in the corresponding directory, and copying, pasting, cutting, and configuring ACLs for the handoff point will only affect this handoff point, and moving the handoff point within the same volume will only affect this handoff point. Moving the handoff point within the same volume will only affect this handoff point, but moving the handoff point between volumes will convert the handoff point to a normal directory, and all the contents under the corresponding directory of the handoff point will be moved. Symlink (Softlink) is a reference to a file or directory, in fact, the symbolic link itself is a "record of the absolute or relative path of the referenced file or directory" of a special file, through the symbolic link operations will be redirected to the target file or directory. Read, write, traverse" operations on symbolic links and shortcuts will be redirected to the target file or directory, but "copy, delete, move, configure ACL" operations on them are only for themselves.
related parties Remove shortcut without affecting target. Deleting a hard link in Explorer does not affect the targetfile. deleting the target file does not affect the hardlink. in fact, due to the semantics of the hard link, the remaining hardlinks at this point are the only access points to the original data. Only when all the hard links corresponding to a file ID are deleted is the data actually marked as deleted. When the target directory is removed, the junction point will still exist, but it will be invalidated and become unavailable. This is easy to understand, because at this point the function point is pointing to a directory that doesn't exist. Deleting symboliclinks in Explorer does not affect the target. deleting the target, symboliclinks will still exist, but are invalidated and become unavailable. They can be manipulated like normal files, but all operations on symbolic links actually act on the target object. Symbolic links are transparent to the user, they look no different from normal files and folders, and operate in exactly the same way (more like Linux soft links).

Application of links

  • Hard links: Allows for quick access to files without copying them as well as file backups, and also prevents accidental deletion of important files, since it is the link to the file that is deleted, not the file data itself.
  • Symbolic links: can map one path to another, or point to a remote file or directory, or even to a file on another computer over a network.
  • Directory Linking: To realize path redirection, when accessing the linked directory, the system will automatically redirect to the actual directory, e.g., Vista's "C:\Documents and Settings" is a junctionpoint to "C:\Users" so that some old programs that use the hard-coded "C:\Documents and Settings" can work properly on Vista. Settings" will work on Vista. It also solves the problem of Windows file path length limitations (starting with Windows 10 version 1607, long paths can be enabled in the registry as well as in the application list).

consultation

  1. /zh-cn/windows/win32/fileio/hard-links-and-junctions
  2. /en-us/powershell/module//new-item?view=powershell-7.4
  3. /zh-cn/windows/win32/fileio/maximum-file-path-limitation?tabs=registry#enable-long-paths-in-windows-10-version-1607-and-later