Questions tagged [hardlink]

Links a name with actual data (file). Hard-linking allows the file to have multiple names (handles). Present in POSIX-compliant systems (also only partially!): GNU/Linux, Android, Apple Mac OS X and even Windows, though with limitations. Allows for slightly different aliasing than soft-linking (aka symbolic linking) - there are trade-offs to each methods.

In a nutshell

Files contain data. Filenames point to files. If you have more than one name pointing to the same file, you have a hard link.

Most file systems support hard links, but not all (FAT, for one, doesn't). Usually this means keeping track of the number of distinct filenames used for a particular file; the filesystem then continues to make file content available for access as long as at least one hard link is left. If you ever wondered why C uses unlink to remove files, that's the reason: it doesn't remove the file, it removes the fileNAME and decreases the aforementioned reference counter.

Hard vs soft linking

Two hard links to a given set of content will reference the same inode. In other words, they are different names for the same file. A soft link to a file is a different file (its own inode) which contains data pointing to a target.

MyFile in (say) inode 3333 can have "my text" as its data. MyHardLink will point to the same inode, 3333 and thus will have same data. MySoftLink will be a different file, occupying a different inode (say, 3334) and its data will be a pointer to the name MyFile.

Illuminating and illustrated! explanation of both concepts by Lew Pitcher, from Linux Gazette.

Limitations

Hardlinks cannot point to a parent of the directory containing them. This avoids endless recursion. There is also generally a limit on how many hard links can be made to the same inode, stemming from the reference counter stored by the filesystem; if too many hardlinks were to be created to one inode, the reference count would overflow. These limits are usually are worked around with symbolic links, and are very well described on Wikipedia.

Additional Windows limitations

  1. The minimum client and server Windows OS supporting hardlinks are - correspondingly - XP and Server 2003.
  2. Windows hard links are NTFS-only.
  3. NTFS uses 10 bits for the filename counter, so there can be only 1023 distinct names per file.

MSDN page on Hard Links

Windows API for CreateHardLink function

192 questions
1046
votes
23 answers

What is the difference between a symbolic link and a hard link?

Recently I was asked this during a job interview. I was honest and said I knew how a symbolic link behaves and how to create one, but do not understand the use of a hard link and how it differs from a symbolic one.
Nick Stinemates
  • 41,511
  • 21
  • 59
  • 60
127
votes
4 answers

Git and hard links

Considering that Git does not recognize symbolic links that point outside of the repository, is there any problem using hard links? Could Git break them? Can you please point me to detailed information?
Alfredo Palhares
  • 1,421
  • 2
  • 9
  • 7
101
votes
4 answers

Nginx sites-enabled, sites-available: Cannot create soft-link between config files in Ubuntu 12.04

I am trying to create soft links between config files containing server blocks in the sites-enabled and sites-available directories in /etc/nginx/. The command I am using is: sudo ln -s sites-available/foo.conf sites-enabled/ When I then…
iamyojimbo
  • 4,233
  • 6
  • 32
  • 39
66
votes
4 answers

Creating directory hard links in Mac OS X

How can I create a hard link to a directory in Mac OS X? This feature has been added to their file system in Mac OS X v10.5 (Leopard) (for time machine), but I could not find any information on actually using it from the command line.
Felix Geisendörfer
  • 2,902
  • 5
  • 27
  • 36
47
votes
9 answers

Find out whether a file is a symbolic link in PowerShell

I am having a PowerShell script which is walking a directory tree, and sometimes I have auxiliary files hardlinked there which should not be processed. Is there an easy way of finding out whether a file (that is, System.IO.FileInfo) is a hard link…
Joey
  • 344,408
  • 85
  • 689
  • 683
30
votes
3 answers

Filesystem links on a FAT32 formatted storage

I know FAT32, as well as FAT16/12 neither support symbolic links nor hard-links. However I came up with this idea: The FAT specification describes that every file is associated with a directory-entry. In my understanding, one could say that a…
fishbone
  • 3,140
  • 2
  • 37
  • 50
29
votes
5 answers

How to create a hardlink in C#?

How to create a hardlink in C#? Any code snippet, please?
Swapnil Gupta
  • 8,751
  • 15
  • 57
  • 75
28
votes
1 answer

How do I create file hardlink in PowerShell on Windows 10?

How do I create file hardlink in PowerShell on Windows 10? PSCX has New-Hardlink, but is it still needed on Windows 10? You could always shell out to mklink, but from powershell that requires you prefix the command with cmd /c, which is ugly and…
yzorg
  • 4,224
  • 3
  • 39
  • 57
17
votes
2 answers

Recursively creating hardlinks using python

What I basically would like to do is cp -Rl dir1 dir2. But as I understand it, python only provides shutils.copytree(src,dst) which actually copies the files, but has no possibility of hardlinking the files instead. I know that I could invoke the cp…
devsnd
  • 7,382
  • 3
  • 42
  • 50
16
votes
2 answers

difference between hardlink and bind mount?

it might be a stupid question but: http://dwaves.de/2015/05/26/linux-search-find-files-locate-find-linux-locate-scope/ bind mounts under linux: as far as i understand it: you can mount the same dir in two different places. but where is the…
canoodle
  • 506
  • 5
  • 10
16
votes
4 answers

How to find all files which are basically soft or hard links of other directories or files on linux?

How could I get the list of all linked files on my system or from a certain directory. I used to create links but they became unmanageable with time. I want the list of all such links from a directory. Can anyone help?
DKSRathore
  • 3,043
  • 6
  • 27
  • 36
15
votes
2 answers

Creating Hardlinks and Symlinks in Android

I am creating an app in which I would like to make use of hardlinks and symlinks in the Android external memory filesystem. I have tried using the commands Os.link("oldpath", "newpath"); Os.link("oldpath", "newpath"); However, when I try this, I…
Ashwin Kudva
  • 325
  • 2
  • 16
13
votes
2 answers

Soft link in Mercurial

Is there some equivalent in Mercurial to NIX soft- or hard- links to directories or files. Basically that a file (or directory) is linked to a file "somewhere else" and follows the version of that location (Unlike a regular branch I think, where one…
Olav
  • 1,758
  • 4
  • 27
  • 49
11
votes
3 answers

Are hard links possible within a zip archive?

I am creating a zip archive containing two identical files at different paths. Does the zip archive format support something akin to the Unix concept of hard links? By this I mean the ability to store the file only once (saving space), but to index…
Grampoulos
  • 121
  • 1
  • 4
10
votes
1 answer

Counting hard links to a file in Go

According to the man page for FileInfo, the following information is available when stat()ing a file in Go: type FileInfo interface { Name() string // base name of the file Size() int64 // length in bytes for regular…
Elle Fie
  • 681
  • 6
  • 21
1
2 3
12 13