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

Exclude System Hardlinks from File.Copy

So my problem is that I want to export my user account. But inside C:\%user%\AppData\Local\ are System Hardlinks e.g.: Application Data which I obviously have no right to use them. Is there a way to exclude those System Hardlinks from the copying…
Amachi
  • 100
  • 11
2
votes
1 answer

Windows - hard links to files in a git repository break often

I maintain a private Git repository with all of my config and dotfiles (.bashrc, profile.ps1, .emacs etc.). On Windows this repository is stored under C:\git\config. Most applications expect the files to be elsewhere, so I added hard links between…
Ayrton Massey
  • 471
  • 3
  • 13
2
votes
0 answers

How to use CreateHardLink(string lpSymlinkFileName, string lpTargetFileName, IntPtr lpSecurityAttributes) with a relative path for target?

Using [DllImport("kernel32.dll")] static extern bool CreateHardLink(string lpSymlinkFileName, string lpTargetFileName, IntPtr lpSecurityAttributes); should I be able to use a relative path for the lpTargetFileName? It doesn't seem to work for me.…
Matt
  • 25,943
  • 66
  • 198
  • 303
2
votes
2 answers

how to use lstat() to determine if hard link or not

My OS is linux. I program in C. I know I can use the lstat() to recognize the soft link, i.e., use S_ISLNK(st.st_mode). But how can I recognize the link is a hard link? if the link is a hard link, it will be thought of as a regular file. However, I…
Eddie Lin
  • 55
  • 1
  • 6
2
votes
2 answers

Get Windows hardlink count without GetFileInformationByHandle()

Is there a way to get a file hardlinks count on Windows without using GetFileInformationByHandle()? MSDN says: Depending on the underlying network features of the operating system and the type of server connected to, the GetFileInformationByHandle…
pmezard
  • 427
  • 3
  • 11
2
votes
0 answers

c link() function failing to execute

I have a question regarding the link() function in C. I am using it to create a hard link to a file on a Unix system. First, in my home directory (the same problem happens in other directories) I create a test directory and go into it (cd). Now…
2
votes
2 answers

How do I use Mac OS aliases in Sublime Text 3?

I want to open the tree of folders which contains any code on my Mac in Sublime 3. Something like this: But I don't have all of it in one folder, and it isn't convenient enough to open many folders in Sublime every time I close the app. So I have…
Kyryl Havrylenko
  • 674
  • 4
  • 11
2
votes
1 answer

Using git to back up configuration files

I want to use git as a means of backing up and having a history of changes for configuration files of software that I'm using on my desktop. This would be a local repository with a local installation of git. I am however unsure of these: Is it…
mechalynx
  • 1,306
  • 1
  • 9
  • 24
2
votes
1 answer

EACCES during hardlink creation on Linux not matching any reason in link(2) man page

I have a service which assembles hardlinks to a group of files to which it has read but not write access (and traversal permissions for all parent directory trees) into a transient staging directory. This worked perfectly, until my AWS-hosted nodes…
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
2
votes
2 answers

Size of directory .(DOT) does not decrease?

I am studying the linux file system. I had an experiment to explore how linux saves the hard links. I made 1000 hard links for a file in the same directory. The size of .(DOT) increased to 28672; I remove 500 hard links, the size of .(DOT) did not…
York
  • 103
  • 10
2
votes
1 answer

Find all hard links of a certain file

I have a filename for which I have to get all hard links(that are in the same dir). I thought about using readlink in combination with dir->d_name maneuver, but that only applys to softlinks. Any ideas?
jabk
  • 1,388
  • 4
  • 25
  • 43
2
votes
0 answers

Creating directory hardlinks in android

How to create a hard link to a directory or a file in android environment? I read the man page and tried. But the commands seem to be different in android environment. Did anyone try it?
Sandeep
  • 18,356
  • 16
  • 68
  • 108
2
votes
2 answers

Does the PHP link() command really require elevated privileges on Windows?

The manual says Note: For Windows only: This function requires PHP to run in an elevated mode or with the UAC disabled. But on my Windows 8 machine I can run mklink /H without elevated privileges, so I don't understand why PHP would require…
user2667066
  • 1,867
  • 2
  • 19
  • 30
2
votes
2 answers

replace file with hardlink to another file atomically

I have two directory entries, a and b. Before, a and b point to different inodes. Afterwards, I want b to point to the same inode as a does. I want this to be safe - by which I mean if I fail somewhere, b either points to its original inode or the a…
Ben Clifford
  • 1,398
  • 1
  • 12
  • 23
2
votes
4 answers

Deduplicating identical files using hard links

I have a couple of identical files stored in more than one place on my hard disk. I figure I can save a lot of disk space by hard-linking them to point to the same file. I am a little worried about possibly disastrous side effects. I guess it does…
Thilo
  • 257,207
  • 101
  • 511
  • 656