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
9
votes
1 answer

How to create hard link to file in a docker volume

I am in the process of refactoring and "dockerizing" a legacy application made of shell scripts, C++ binaries, and various open-sources packages (among which httpd) Is there a way to create, in a docker container, hard links to files located in a…
E. Mabille
  • 103
  • 1
  • 9
9
votes
2 answers

Hard link and Symbolic links in Unix

I just wanted to clarify if a hard/symbolic link is actually a file that is created ?? I ran the command: ln source hardlink ln -s source softlink -- The ls command shows this 2 links as a file. So my query is, does ln / ln -s actually create a…
name_masked
  • 9,544
  • 41
  • 118
  • 172
9
votes
2 answers

Create Hardlink with golang

I want to create a hardlink to a file using golang. os.Link() tells me, that windows is not supported. Therefore i tried to use os.exec, to call "mklink.exe". cmd := exec.Command("mklink.exe", "/H", hardlink_path, file_path) err :=…
user2089648
  • 1,356
  • 2
  • 17
  • 31
8
votes
1 answer

Is it possible to create hard links to files in an iOS application bundle?

In order to save on server side bandwidth costs associated with my iOS application, I've packaged a bunch of assets that would otherwise be downloadable at runtime into my iOS application bundle. In the context of the application as written, it…
zemelenda
  • 89
  • 1
  • 3
7
votes
2 answers

Detecting Hard Links in Node.js

How can I tell if a file-system path is a hard link with Node.js? The function fs.lstat gives a stats object that, when given a hard link will return true for stats.isDirectory() and stats.isFile() respectively. fs.lstat doesn't offer up anything to…
ThomasReggi
  • 55,053
  • 85
  • 237
  • 424
7
votes
2 answers

Deduplicate Git forks on a server

Is there a way to hard-link all the duplicate objects in a folder containing multiple Git repositories? Explanation: I am hosting a Git server on my company server (Linux machine). The idea is to have a main canonical repository, to which every user…
Mudassir Razvi
  • 1,783
  • 12
  • 33
7
votes
2 answers

creating soft links with the same name as the target file

ln -s /dir1/file1 /dir2/file1 I'd like to create a softlink in target dir1 with same filename as source in dir2 How is this done without typing the file1 name over in the target path
user1874594
  • 2,277
  • 1
  • 25
  • 49
7
votes
1 answer

Using hardlinks when cloning a Mercurial repository under Windows

I am using Mercurial under Windows XP (using the TortoiseHg distribution) and I want to use NTFS hardlinks when cloning a repository. Out of the box Mercurial does not do this. I have read that a win32file python extension needs to be enabled. So…
Mike Thompson
  • 6,708
  • 3
  • 32
  • 39
7
votes
4 answers

Can't Hard Link the gitconfig File

I am attempting to create a git repository to store all of my dotfiles and config files. My idea was to simply create hard links to all of the files I cared about and store those links in their own directory that I could turn into a repository. I've…
Matt Garriott
  • 362
  • 2
  • 12
7
votes
2 answers

How to create a copy of a directory on Linux with links

I have a series of directories on Linux and each directory contains lots of files and data. The data in those directories are automatically generated, but multiple users will need to perform more analysis on that data and generate more files, change…
Greg B
  • 609
  • 6
  • 19
6
votes
2 answers

Can we create a hard link to a symbolic link in UNIX?

I created a symbolic link of a file and a hard link of that symbolic link. But instead of hard link symbolic link is being created and inodes of both of them are equal. Is it possible to create a hard link for a symbolic link ?
Gagan
  • 85
  • 1
  • 11
6
votes
4 answers

How to replace duplicate files with hard links using python?

I'm a photographer and doing many backups. Over the years I found myself with a lot of hard drives. Now I bought a NAS and copied all my pictures on one 3TB raid 1 using rsync. According to my script about 1TB of those files are duplicates. That…
JasonTS
  • 2,479
  • 4
  • 32
  • 48
6
votes
2 answers

How to create a Hardlink using the New-Hardlink PowerShell PSCX command

I want to create a new Hardlink with the PowerShell Community Extensions PSCX commandlet New-Hardlink http://pscx.codeplex.com/. I have read the man file and tried almost every combination of commands but it won't work. What am I missing? (I know…
jedatu
  • 4,053
  • 6
  • 48
  • 60
5
votes
1 answer

Python copy-on-write using hardlinks

Using Python 2.5+, UNIX: I have a program which simulates directory "copy-on-write" functionality, by hardlinking all entries. Currently all the underlying code, some of which I don't have access to, uses standard open(fname, 'w') to write the…
user124114
  • 8,372
  • 11
  • 41
  • 63
5
votes
1 answer

How to enumerate paths for all NTFS hard links pointing to a file?

I can use GetFileInformationByHandle to determine the number of hard links associated with a file. How can I enumerate the paths which make up those links? For example, if C:\TEMP_1.BIN and C:\TEMP_2.BIN are hard links to the same content, and I…
g01d
  • 651
  • 5
  • 24
1
2
3
12 13