HowTo: Create a Hard Links in Linux / UNIX

by on October 9, 2007 · 7 comments· LAST UPDATED October 1, 2011

in , ,

Both Linux / UNIX allows the data of a file to have more than one name in separate places in the same file system. Such a file with more than one name for the same data is called a hard-linked file. How do I create a hard link under Linux / UNIX / Apple Mac OS X / BSD operating systems?

A hard link to a file is indistinguishable from the original directory entry; any changes to a file are effectively independent of the name used to reference the file. Hard links may not normally refer to directories and may not span file systems.

ln command Example To Make a Hard Link

The ln command make links between files. By default, ln makes hard links.

ln Command Syntax

The syntax is as follows for Unix / Linux hard link command:

 
ln {source} {link}
 

Where,

  • source is an existing file.
  • link is the file to create (a hard link).

To create hard link for foo file, enter:
echo 'This is a test' > foo
ln foo bar
ls -li bar foo

Sample outputs:

4063240 -rw-r--r-- 2 root root 15 Oct  1 15:30 bar
4063240 -rw-r--r-- 2 root root 15 Oct  1 15:30 foo

Hard Links Limitations

There are some issues with hard links that can sometimes make them unsuitable. First of all, because the link is identical to the thing it points to, it becomes difficult to give a command such as "list all the contents of this directory recursively but ignore any links". Most modern operating systems don't allow hard links on directories to prevent endless recursion. Another drawback of hard links is that they have to be located within the same file system, and most large systems today consist of multiple file systems.

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 7 comments… read them below or add one }

1 zafar October 1, 2009 at 2:59 am

hi i m zafar
i wanna to update in linux command
but i know some cmd so plz me update continu
thanks

Reply

2 amu December 7, 2009 at 5:45 pm

hard link

ln -fs file_location link_file_location
where
fs is forcefully and source respectively

Reply

3 RonCam September 30, 2011 at 8:53 pm

Another drawback of hard links is that they have to be located within the same file system, and most large systems today consist of multiple file systems.

Does mean, for example, you cannot make a hard link between and ext3 and ext4 file system, or does it mean you cannot link between file systems on two separate volumes?

I know this is two years old, but maybe some is watching? I have seen the above quote in a number of sources, but this one is the only one where there’s a chance to ask.

Reply

4 nixCraft October 1, 2011 at 9:55 am

We are talking about two devices. For example /dev/sdc1 mounted on /disk1 and /dev/sdd1 mounted on /disk2. You can not create a hard link between /disk1 and /disk2. You will get an error as follows if you try:

ln: creating hard link `hard' => `/disk2/somefile': Invalid cross-device link

Also, you can not format /dev/sdd1 as ext3 or ext4 at the same time or mount same device as ext3 or ext4 as same time as you need to format the file system. In short, hard link is not allowed between cross devices (it does not matter if devices are formatted as ext3 or ext4).

Reply

5 RonCam October 1, 2011 at 11:50 am

Thanks for the clarification and thanks for still watching this thread. Hope your reply will be useful to others who come across your excellent blog, by searching.

Now, because of what you said, I may instead try to get a similar result using ‘mount –bind’.

I’m trying to get the ‘/doc’ from ‘usr/share/doc’ off of a small SSD and onto a separate ‘/doc’ on a MMC in the memory slot of the same netbook. As well, I think the above command should also be insensitive to the SSD being ext4 and the MMC being formatted in ext3.

Reply

6 Roland October 15, 2011 at 3:26 pm

How can you display all of the hard links on a file system to a particular file?

Reply

7 ThomasCle November 5, 2013 at 1:43 pm

Thank you! This was exactly what I needed on my Mac! It is perfect to link files between two Git repositories :)

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , , , , , , , , , , , ,

Previous Faq:

Next Faq: