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

Is Android M not allowing hard links?

I have an Android app with some C code that uses the link(2) system call to create a hard link to an existing file. When I execute the app on Android 5.0.2, this part of the app works. When I execute the exact same app on an Android-M device the…
Sam
  • 128
  • 9
5
votes
3 answers

Check whether a file is a hard link

How do you check whether a file is a hard link in Go? os.FileMode only has a mode for symlinks, not hard links. I had an idea that unfortunately doesn't work: package main func main() { filename := os.Args[1] var hardlink bool link, _…
GreenRaccoon23
  • 3,603
  • 7
  • 32
  • 46
5
votes
1 answer

How to get hardlink cloning on Windows

I've got a Win7 x64 box running Tortoise 1.0 x64 release. About Tortoise reveals that it's 1.0 "with Mercurial-1.5, Python-2.6.4, PyGTK-2.16.0, GTK-2.18.7". I've also got ActivePython 2.6 and Mercurial 1.5 x64 installed (installed via the Mercurial…
Mike Caron
  • 5,674
  • 4
  • 48
  • 71
5
votes
4 answers

Finding files that are *not* hard links via a shell script

Given a directory, how do I find all files within it (and any sub-directories) that are not hard-linked files? Or more specifically, that are not hard-linked files with more than one reference? Basically I want to scan a folder and return a list of…
Haravikk
  • 3,109
  • 1
  • 33
  • 46
5
votes
2 answers

Django: Copy FileFields

I'm trying to copy a file using a hardlink, where the file is stored as a Django FileField. I'd like to use a hardlink to save space and copy time (no changes are expected to be made to the original file or copy). However, I'm getting some odd…
Joe J
  • 9,985
  • 16
  • 68
  • 100
4
votes
2 answers

Bash: Create hardlink if destination is inside same volume, copy if not

My bash script makes copies of some files to some multiple directories. In order to save space and maximize speed, I would prefer to make hardlinks instead of copies, since all copies need to remain identical during their life anyway. The script is…
elmimmo
  • 442
  • 2
  • 11
4
votes
1 answer

MEM_SHARED, mmap, and hard links

Just wondering if the key to shared memory is the file name or the inode. I have a file called .last, which is just a hard link to a file named YYYYMMDDHHMMSS. A directory looks like this: 20110101143000 .last .last is just a hard link to…
johnnycrash
  • 5,184
  • 5
  • 34
  • 58
4
votes
6 answers

List Hard Links of a file (in C#)

I want to write a program that shows the files of another drive with hard links. I want to keep both hardlinks consistent in filename and other things, so I have to get a function/method where I can list all current hard links of a file. For…
fpdragon
  • 1,867
  • 4
  • 25
  • 36
4
votes
1 answer

How to know if two hard links point to the same inode? (C#)

Anyway of checking, in C#, that two files (hard links) point to the same inode? And also get the count of this inode, in case there are more than two... ?
Andreas Zita
  • 7,232
  • 6
  • 54
  • 115
4
votes
3 answers

How can I programmatically distinguish hard links from real files in Windows 7?

I have a difference between files size and used disk space (total file size is even more than disk size). I suppose because there are many hard links exist (to WinSxS components) in Windows 7/Vista. But how can I programmatically distinguish hard…
SKINDER
  • 950
  • 3
  • 17
  • 39
4
votes
1 answer

Create a hardlink with Inno Setup

I have thousand of own installers that requires a critical dll file for uninstallation step, this dll file sizes about 2 mb then to avoid unnecessary disk space (2mb*100 installers) I would like to store the file once in {cf} then make a hardlink…
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
4
votes
3 answers

Delete link to file without clearing readonly bit

I have a set of files with multiple links to them. The files are owned by TFS source control but other links to them are made to them. How do I delete the additional links without clearing the readonly bit. It's safe to assume: The files have more…
Joshua
  • 40,822
  • 8
  • 72
  • 132
3
votes
3 answers

How to realize "mklink /H" (hardlinking) in Java?

i want to create a hardlink from a file "C:\xxx.log" to "C:\mklink\xxx.log" . In cmd it works of course, but i want to write a software for this usecase. So have to locate the existing file Then make a hardlink Then delete the old file I started…
MäxL
  • 195
  • 1
  • 1
  • 6
3
votes
1 answer

What happens to permissions when a file goes to the recycle bin?

I have a problem related to file management done by a service application. My problem is that users are able to move files to the recycle bin for which I've created a hardlink, and once they do this, I loose the ability to list the hardlinks…
Pablo Montilla
  • 2,941
  • 1
  • 31
  • 35
3
votes
1 answer

Quick way to enumerate all hardlinks in python3

Is there a quick way/function to enum all hardlinks of a file? Something that directly gives me the paths of all hardlinks to a given file? On windows if that matters any. I know about os.stat("foo.txt").st_nlink and about os.path.samefile(path1,…
martixy
  • 784
  • 1
  • 10
  • 26
1 2
3
12 13