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
1
vote
0 answers

How to deal with createLink failed with access denied?

On android 8(API26), I want to use Files.createLink to create a hard link. I am very sure fExists does indeed exist, and f does not exist. And both of them are in app's data storage area. fExists: /data/user/0/appPakageName/myFolder/a.png f:…
jackiszhp
  • 767
  • 1
  • 5
  • 11
1
vote
1 answer

Subdirectories in linux

I have read that hard links to directories are not allowed in Unix because they might cause loops in the file system. Assume I have a sub directory inside directory, how the link to the sub directory is represented if not by a hard link? Does it…
roy cabouly
  • 487
  • 4
  • 12
1
vote
2 answers

How do I redirect from a built-in bin to execute an AppImage instead?

Scenario: the current version of Kate in Ubuntu 18LTS points at their customized version (which doesn't appear to support regex search capability). The bin is: /usr/bin/kate. Desired solution: run the Kate AppImage (which has the regex…
Jay Marm
  • 556
  • 5
  • 12
1
vote
1 answer

How to create a hard link to an existing file or directory in C?

I am looking to create a hard link to a file or directory that already exists. For instance a user chooses to create a hard link to a file named "pathname" and the user can choose the name of the link file.
Brad Bieselin
  • 107
  • 1
  • 11
1
vote
1 answer

Hard links in windows xp

is there any way to create hard links in windows xp ? i came across this link but it says minimum vista is needed , any way to do it without using link magic software ?
GoodSp33d
  • 6,252
  • 4
  • 35
  • 67
1
vote
0 answers

Creating hardlinks in docker container takes long the first time

I am creating hard links of all the files in a directory (some 500MB) in a docker container using following command: cp -al source destination Interestingly, when I issue the above command the first time (i.e. when there is no hardlink for each…
pranavk
  • 1,774
  • 3
  • 17
  • 25
1
vote
1 answer

Find Out If Two HANDLEs are Hardlinks to the Same File

(This question is a toughie... it might require knowledge of NTFS and/or the use of NT Native APIs; be warned.) :) If I'm given two HANDLEs to two files, how can I definitively (not just with high probability) find out if the two HANDLEs belong to…
user541686
  • 205,094
  • 128
  • 528
  • 886
1
vote
2 answers

on android how to find the original file if got a hard link

in the app when receiving an intent which was created from other app and has a file path, it can access the file's content using the file path. the question is if that path (call it as 'link-path') is a 'hard link' to the original file, is it…
lannyf
  • 9,865
  • 12
  • 70
  • 152
1
vote
2 answers

How to find out if a folder is a hard link and get its real path

I'm trying to findout if a folder is actually a hard link to another, and in that case, findout its real path. I did a simple example in python in the following way(symLink.py): #python 3.4 import os dirList = [x[0] for x in os.walk('.')] print…
Andres Tiraboschi
  • 543
  • 1
  • 7
  • 17
1
vote
0 answers

Why won't my Makefile use the hardlink to the file I want compiled?

So this is a little bit messy, however I have a single .cpp file that I want to be used in two different ways; as a c class called by a java class using JNI, and as a c program standalone. The reason for this is because this code moves around…
danglingPointer
  • 882
  • 8
  • 32
1
vote
1 answer

Does creating a hard link create a new inode?

From this StackOverflow thread, I get to know that symbolic links have their own inodes that are different from those of their targets. This can be verified with ls -i. The command will show that a symbolic link and its target have distinct inode…
Lingxi
  • 14,579
  • 2
  • 37
  • 93
1
vote
0 answers

How can I create a link to existing file with python

Short description of the problem: I have some directories (dir_1,...dir_N) and want to merge them in a new directory (dir_X) but without copying all the files from those directories (would be a waste of memory). All directories are on the same…
Feuerteufel
  • 571
  • 5
  • 16
1
vote
1 answer

Working with hardlinks in *nix

I have done the following operations: echo "test" >> t1 echo "test2" >> t2 ln t1 l1 cp t2 t1 cat l1 To my surprise after overwriting t1 with t2 the hard link was still working. As per my understanding when you create a new version of a file the…
triple fault
  • 13,410
  • 8
  • 32
  • 45
1
vote
0 answers

security policy diference: fs.link() vs mklink /h

UPDATED: On a windows 2008R2 server (where we have admin priveges, but are not running elevated) I am able to manually use the console command > mklink /h < link> to create hard links between files on a single volume. However, in node.js…
Dr.YSG
  • 7,171
  • 22
  • 81
  • 139
1
vote
1 answer

List hard links of a file C

I need to list all hard links of a given file in "pure" C, so without help of bash commands. I googled for hours but couldn't find anything useful. My current idea is to get inode number then loop through directory to find files with same inode. In…
user2976389
  • 525
  • 1
  • 5
  • 7