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
3
votes
3 answers

Disadvantages to creating/removing many hard links?

I need to create hundreds to thousands of temporary hard or symbolic links that will be deleted shortly after creation. For my purposes both types of links will work (i.e. the target is not a directory and it always exists on the same file…
agartland
  • 1,654
  • 2
  • 15
  • 19
3
votes
3 answers

Can a hard link ever point to a deleted file?

I understand the different between hardlinks and softlinks in Linux, but I am having trouble understanding this one problem: Can a hard link ever point to a deleted file? Why or why not? I think it can but I am not certain. An explanation would be…
user8930130
  • 59
  • 2
  • 8
3
votes
1 answer

C# - count and list hardlink locations of a file

I'm currently searching for a C# solution which can read the number of hardlinks of a file and their links locations. I'm thinking of the program link shell extension which can do this by going to the file properties and lists all the hardlinks of…
fpdragon
  • 1,867
  • 4
  • 25
  • 36
3
votes
2 answers

Does git use hardlinks for a remote on the same disk?

I sometimes work with two sibling directories both containing the same repository. Using git remote add sibling ../the-other-directory I take over changes simply via git fetch sibling git cherry-pick sibling/a-branch This is pretty practical,…
maaartinus
  • 44,714
  • 32
  • 161
  • 320
3
votes
2 answers

How to find all the hard and symbolic links to a file/folder (Windows and UNIX)?

How to find all the hard and symbolic links to a file/folder in Windows (using standard OS functions)? What is opposed to UNIX?
Artyom Ionash
  • 405
  • 7
  • 17
3
votes
2 answers

How to create hard link in Linux from a C program

We know we can create hard link in Linux using ln file1 file2 which will make file2 a hard link of file1. However when I try to do this by using a C program, I face issues. Below is the C…
sps
  • 2,720
  • 2
  • 19
  • 38
3
votes
3 answers

How to get the number of hardlinks

Is there some library function, or some other way, to get the hard link count of some file in Python?
user3921265
3
votes
1 answer

Recursive hard link

With Unix cp you can use the --link option. When used with a folder, it will hard link the files involved instead of copying, example cp --recursive --link foo bar This can be ideal in certain situations because it is faster than regular copying.…
Zombo
  • 1
  • 62
  • 391
  • 407
2
votes
4 answers

How to get file MFT entry/inode using Java or C++

I've written a duplicate finder in Java, but I need to include hard link support for it. Unfortunately, there seems to be no way to dig out a file's MFT entry in Java. Although there is a method called fileKey() in the BasicFileAttributeView class,…
user1079475
  • 379
  • 1
  • 5
  • 17
2
votes
4 answers

Hard link to a file not working as expected on OS X

I've a file in a folder and I don't know anything about this file (how it's generated and updated) because it comes from an application running on my system of which I don't have the source code. The file format is clearly json and I successfully…
Francesco
  • 1,047
  • 11
  • 26
2
votes
2 answers

How to test in two paths are hard linkable?

I have file paths in one side and their new paths where I want to duplicate them. How to test whether they can be simply hardlinked or they should be copied?
Zebooka
  • 380
  • 2
  • 10
2
votes
2 answers

Python import error when execute Mercurial

My system is RedHat 5 Linux and has default python 2.4 installed. In order to execute Mercurial1.81, I tried to upgrade python from 2.4 to 2.6 and save it in different directory. Here are the commands I have used: find -xdev -samefile…
Cassie
  • 1,179
  • 6
  • 18
  • 30
2
votes
4 answers

How to identify a hardlink using python?

I would like to know if it is possible identify whether a file (or link) is a hardlink or not, on Linux. For instance, If I created: dd if=/dev/urandom bs=1024 count=10000 of=file_10MB conv=notrunc ln file_10MB file_10MB_link1 I would like to…
uilianries
  • 3,363
  • 15
  • 28
2
votes
2 answers

Is there a way to completely remove an inode when the Link count is 2?

Currently my data is organised in a volume which has a cache directory (where all the files are first created or transferred). After that there are suitable directories on the volume which in their subdirs, contain files hardlinked to files in the…
Nishank
  • 23
  • 2
2
votes
1 answer

Can't make hard link for mounted host file in LXD container

I configed a host directory as a disk device in an unprivileged LXD container, like /opt/app/var, and I created a backup directory on container self filesystem, like /backup. I used rsync to backup /opt/app/var files to /backup with hard link, but I…
dawncold
  • 183
  • 1
  • 1
  • 12