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

NFS + Hard Links?

I know it is a condition of hard links that they cannot span filesystems. Does this apply to NFS mounts? Given the following directory structure, would I be able to create a hard link in directory A that points to a file in directory B? /root …
Brian
  • 2,107
  • 6
  • 22
  • 40
2
votes
3 answers

Difference between creating a duplicate file descriptor using dup() and creating a hard link?

I just tried out this program where I use dup to duplicate the file desciptor of an opened file. I had made a hard link to this same file and I opened the same file to read the contents of the file in the program. My question is what is the…
Arpith
  • 570
  • 2
  • 10
  • 26
2
votes
1 answer

Is it possible to create a "weakly referenced" hard link on filesystems?

Some programming languages have the ability to distinguish strongly referenced objects from weakly referenced objects such that weak ones are candidates for garbage collection after all the strong references disappear. Is there a similar concept…
Anthony Bishopric
  • 1,306
  • 11
  • 23
1
vote
3 answers

Simulate concatenated file using hard link?

I have multiple parts of a single file which I want a 3rd party c++/c# plugin to read as a single file. Basically, when the plugin file reader gets to the end of one file-part, I want it to continue to the next one. (For anyone interested, the…
Sugrue
  • 3,629
  • 5
  • 35
  • 53
1
vote
3 answers

fsutil hardlink doesn't work?

I was looking for a way to create hard links under Windows and I found this page: http://technet.microsoft.com/en-us/library/cc788097.aspx To try it out, I created a file (1.txt) on the root of my C: drive with 100 lines of the following…
Alix Axel
  • 151,645
  • 95
  • 393
  • 500
1
vote
1 answer

Looping through sub dirs in large data set, making a new folder with the subdir name, & then hardlinking select files to that new directory

I'm struggling immensely with getting a nested for loop to work for this. The data set I am working with is very large (a little over a million files). I was looking at a nested for loop but it seems unstable. count=0 for dir in $(find "$sourceDir"…
rptatum
  • 21
  • 3
1
vote
2 answers

Copy a folder programmatically without resolving hardlinks in Windows (Win32 API)

I want to copy an entire folder without resolving the hardlinks example: Folder1 | +---File1 File2 HardLink3 -> File3 (HardLink3 created using fsutil hardlink create or mklink) I want to copy this folder to Folder2 | +---File1 …
PabloG
  • 25,761
  • 10
  • 46
  • 59
1
vote
0 answers

How to create hardlinks recursively?

If I were to do it on Linux alone, it'd be easy with the following code: package main import ( "fmt" "log" "os/exec" ) func main() { err := exec.Command("cp", "-rl", "src/path", "target/path").Run() if err != nil { …
Varo OP
  • 71
  • 6
1
vote
1 answer

How do I create a hardlink in WiX

Is it possible to create a hardlink in WiX without creating a Custom Action? I found this thread describing the problem, but it doesn't provide an answer. The reason for wanting to create a hardlink is that I've got four different applications…
Bjarne
  • 131
  • 1
  • 1
  • 3
1
vote
1 answer

Cannot find path _ because it does not exist

I'm trying to create small script in powershell that would move files and directories to correct localizations. I made the following command: Get-ChildItem -Path '.\list\' | ForEach-Object { if ($($_.Name) -like '*[1]*') { $file = $($_.Name) $path =…
plW0lf
  • 13
  • 1
  • 3
1
vote
1 answer

How to detect (FSEvent) if a file is modified through a (hard) symlink?

Some context The attempt is keep a hash for the content of each file in a list. Example file1.txt "This is a long story about dragons and..." -> Hash A28F30... file2.txt "Larousse dictionary. Letter A..." -> Hash 98BC012... For this, the program…
Adrian Maire
  • 14,354
  • 9
  • 45
  • 85
1
vote
1 answer

What is a "Failed to create hard link: File exists" error?

I'm trying to do a SSH login from VSCode using the Remote SSH extension and I'm getting this error. It is working fine when I login from my GIT terminal. The error stack: [20:44:36.547] > \ln…
amshu
  • 131
  • 1
  • 8
1
vote
0 answers

CreateHardLink and CreateSymbolicLink Win32 Functions

I am completing a project to create dummy file systems for backup testing and need to develop a method of creating a Hardlinks and Softlinks within the structures. The CreateHardLink and CreateSymbolicLink functions in windows.h receive file…
jscott
  • 397
  • 2
  • 5
  • 18
1
vote
2 answers

hardlinks in Linux

What is the size of the hardlink in Linux? Will it be the size of the inode? If I have two of them? Thanks in advnace for any explanation, I tried to google it, but didn't find anything
macindows
  • 4,303
  • 4
  • 18
  • 9
1
vote
1 answer

What's the name of the "create hard link" function in MSVCRT?

To fix a bug in this answer: What's the correct name of the function to create a hard link in MSVCRT?
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820