How to create a hard links in Linux or Unix

I am a new Linux system user. How do I create a hard link in Linux / UNIX / Apple Mac OS X / BSD operating systems using the command line?

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. 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.

How to create a hard links in Linux or Unix

To create a hard links on a Linux or Unix-like system:

  1. Create hard link between sfile1file and link1file, run: ln sfile1file link1file
  2. To make symbolic links instead of hard links, use: ln -s source link
  3. To verify soft or hard links on Linux, run: ls -l source link

Let us see examples to make a hard link on a Linux / Unix systems.

ln command example to make a hard link on a Linux

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

ln Command Syntax T Create Hard Link in Linux

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

ln {source} {link}
ln /path/to/source /path/to/link
ln target link
ln target directory


  • 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


  • 4063240: Inode. From the above sample output we can see that inodes are identical. We passed the -i option to the ls command to display the index number of each file (inodes).
  • 2: The number of hard links to file is shown in the third column. So if you run, ln foo hlink2, the counter will increase to three.

How do I delete a hard link on Linux or Unix?

The rm command deletes files on Linux or Unix including a hard link. However, data is still accessible as long as another hard link exists even if you delete source file. To get rid of data you must remove all hard links including source.

Use the rm command:
$ echo 'I love Linux and Unix' > file.txt
$ cat file.txt
## create hard links ##
$ ln -v file.txt hardlink1
$ ln -v file.txt hardlink2
## list all files with inodes ##
$ ls -li file.txt hardlink?
## remove 1st hardlink ##
$ rm hardlink1
$ ls -li file.txt hardlink?
## remove source file ##
$ rm file.txt
$ ls -li file.txt hardlink?
## but we can still access original file.txt data ##
$ cat hardlink2
## to get rid of file.txt data delete all hard links too ##
$ rm hardlink2
## error error all data gone ##
$ cat file.txt hardlink?
$ ls -li file.txt hardlink?

How to create a hard links in Linux or Unix

Hard Links Limitations on Linux and Unix

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.

How to create symbolic (soft) links in Linux or Unix

The syntax is as follows:

ln -s source link
ln -s /path/to/foo/ /path/to/link
ln -v /path/to/foo/ /path/to/link

In this following example, create a soft links to as and
$ ln -sv
$ ln -sv

Verify it:
$ ls -li
Linux create symbolic (soft) links


You learned how to use the ln command to create hard and soft links on Linux or Unix-like system including limitations and how to delete hard links. For more info see GNU/ln command page here.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 11 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
11 comments… add one
  • zafar Oct 1, 2009 @ 2:59

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

    • avinash Jan 23, 2017 @ 5:55

      after reading this of you, i vomit want to.

  • amu Dec 7, 2009 @ 17:45

    hard link

    ln -fs file_location link_file_location
    fs is forcefully and source respectively

  • RonCam Sep 30, 2011 @ 20:53

    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.

    • 🐧 nixCraft Oct 1, 2011 @ 9:55

      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).

  • RonCam Oct 1, 2011 @ 11:50

    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.

  • Roland Oct 15, 2011 @ 15:26

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

    • Benjamin West Apr 19, 2016 @ 23:00


      If I understood the question properly you could:
      $ cd dir-the-file-is-in
      $ ls -li filename*
      $ cd /
      $ find . -inum “23432432” -exec ls -li {} \;

      Replace the inod number with what you received from the first command. The exec part is optional. All depends on how much info you want upon finding the files.

      Hope that helps.


  • ThomasCle Nov 5, 2013 @ 13:43

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

  • shashi Apr 14, 2016 @ 10:13


  • Hardik Jan 24, 2017 @ 11:07

    Hey Can we create a Hardlink across a netwok ?

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum