Page Body

Creating Hard and Symbolic Links With the ln Command

The ln command makes links between files. It can create two kinds of links.

Note: If you are not familiar with the GNU/Linux command line interface, review the Conventions page before proceeding.

A hard link is a directory entry that ties a name with a file on a file system. Directory-based file systems have at least one hard link for each file on the file system.

Hard Link Illustration
"Simplified illustration of hard links on typical UN*X filesystem.png" by Mtodorov 69 is licensed under a CC BY-SA 4.0 license

Hard-linked files have the same inode numbers (essentially, file system directories are tables mapping filenames to inode numbers). Hard link files are not really distinct files, but links to the same file on the disk. Hard links can have different names, and it is not possible to determine which name was the original filename.

Hard links can be created for files, but not for directories, and they can only be created for files on the same file system. If you ever see two files with identical inode numbers in ls's output (which can be determined with the -ef file test) that you know are distinct, they are likely on different file systems.

A hard link is created like so:

ln ex_target ex_link_name

Alternatively, you can specify one or more targets and a destination directory to create your hard links in (in this case, the hard links will have the same names as the original targets, so the destination directory should not contain any of the original target files):

ln ex_target... ex_destination_directory

To understand how a hard link works, it helps to see an example. Assume a link.txt file in the current user's home directory that contains some lorem ipsum:

$ ls
Desktop  Documents  Downloads  link.txt  Music  Pictures  Public  Templates  Videos
$ cat 'link.txt'
Unde sed dolorem dignissimos expedita doloribus impedit. Possimus in ratione nulla eaque magni et facilis iste. Et temporibus deleniti reiciendis molestiae. Aut voluptatem et cumque veniam qui sunt laboriosam voluptas. Unde sapiente minus non harum ut. Voluptatem cum temporibus vel animi.

This command creates a hard link to link.txt called li.txt:

$ ln 'link.txt' 'li.txt'
$ ls -il
total 40
274893 drwxr-xr-x. 2 amnesia amnesia 4096 May  5 14:52 Desktop
274898 drwxr-xr-x. 2 amnesia amnesia 4096 May  5 14:52 Documents
274894 drwxr-xr-x. 2 amnesia amnesia 4096 Jun  6 14:24 Downloads
262374 -rw-rw-r--. 2 amnesia amnesia  290 Jul 17 10:59 link.txt
262374 -rw-rw-r--. 2 amnesia amnesia  290 Jul 17 10:59 li.txt
274899 drwxr-xr-x. 2 amnesia amnesia 4096 May  5 14:52 Music
274900 drwxr-xr-x. 2 amnesia amnesia 4096 May  5 14:52 Pictures
274897 drwxr-xr-x. 2 amnesia amnesia 4096 May  5 14:52 Public
274896 drwxr-xr-x. 2 amnesia amnesia 4096 May  5 14:52 Templates
274901 drwxr-xr-x. 2 amnesia amnesia 4096 May  5 14:52 Videos

As we can see above, the link.txt and li.txt files have the same inode number, 262374, which we evaluate with ls's -i (--inode) option.

Let us see what happened after we added some text to li.txt:

$ cat 'li.txt'
Unde sed dolorem dignissimos expedita doloribus impedit. Possimus in ratione nulla eaque magni et facilis iste. Et temporibus deleniti reiciendis molestiae. Aut voluptatem et cumque veniam qui sunt laboriosam voluptas. Unde sapiente minus non harum ut. Voluptatem cum temporibus vel animi.

Libero quo et facere. Asperiores nostrum quo recusandae ipsam ducimus. Nemo et quo quos quia officia possimus.
$ cat 'link.txt'
Unde sed dolorem dignissimos expedita doloribus impedit. Possimus in ratione nulla eaque magni et facilis iste. Et temporibus deleniti reiciendis molestiae. Aut voluptatem et cumque veniam qui sunt laboriosam voluptas. Unde sapiente minus non harum ut. Voluptatem cum temporibus vel animi.

Libero quo et facere. Asperiores nostrum quo recusandae ipsam ducimus. Nemo et quo quos quia officia possimus.

Even though text was only added to li.txt, link.txt has the same content because link.txt and li.txt are really the same file. To be more precise, they are two different named references to the location in the file system data pool where the file data actually resides.

The example below creates new links to link.txt and li.txt in the new_links directory in the current directory:

$ ls
Desktop  Documents  Downloads  link.txt  li.txt  Music  new_links  Pictures  Public  Templates  Videos
$ ln 'link.txt' 'li.txt' 'new_links'
$ ls -il 'new_links'
total 8
262374 -rw-rw-r--. 4 amnesia amnesia 402 Jul 17 11:00 link.txt
262374 -rw-rw-r--. 4 amnesia amnesia 402 Jul 17 11:00 li.txt

Helpful ln options include:

-b
Back up a copy of each existing destination file by appending a tilde to their names. Like the --backup option, but does not accept an argument.
-f, --force
Force copy and remove existing destination files without prompting.
-i, --interactive
Interactively ask (once per file) whether existing destination files should be removed.
-v, --verbose
Verbose output. Prints name of each linked file.

A symbolic link is a file that contains a reference to another file or directory in the form of a path, with a flag signifying that the file is a symbolic link and that accesses should be redirected to the target file. Creating or deleting a symbolic link does not impact the target file. However, when the target is removed, the symbolic link dangles, i.e., it points nowhere (accesses elicit an error message).

Unlike hard links, symbolic links are distinct from their targets and have different inode numbers. Symbolic links can be created for both files and directories, and can point to objects on different file systems that may or may not be currently available (or even exist).

A symbolic link is created with the ln command's -s (--symbolic) option:

ln -s ex_target ex_link_name

The following creates a symbolic link of link.txt called s_link.txt:

$ ln -s 'link.txt' 's_link.txt'
$ ls -il
total 44
274893 drwxr-xr-x. 2 amnesia amnesia 4096 May  5 14:52 Desktop
274898 drwxr-xr-x. 2 amnesia amnesia 4096 May  5 14:52 Documents
274894 drwxr-xr-x. 2 amnesia amnesia 4096 Jun  6 14:24 Downloads
262374 -rw-rw-r--. 4 amnesia amnesia  402 Jul 17 11:00 link.txt
262374 -rw-rw-r--. 4 amnesia amnesia  402 Jul 17 11:00 li.txt
274899 drwxr-xr-x. 2 amnesia amnesia 4096 May  5 14:52 Music
262342 drwxrwxr-x. 2 amnesia amnesia 4096 Jul 17 11:00 new_links
274900 drwxr-xr-x. 2 amnesia amnesia 4096 May  5 14:52 Pictures
274897 drwxr-xr-x. 2 amnesia amnesia 4096 May  5 14:52 Public
262979 lrwxrwxrwx. 1 amnesia amnesia    8 Jul 17 11:01 s_link.txt -> link.txt
274896 drwxr-xr-x. 2 amnesia amnesia 4096 May  5 14:52 Templates
274901 drwxr-xr-x. 2 amnesia amnesia 4096 May  5 14:52 Videos

Above, we can see that s_link.txt has a distinct inode number from both link.txt and li.txt, 262979. Also, we can see that s_link.txt is a link pointing to the link.txt file, s_link.txt -> link.txt.

Documentation

For more information on the ln command, refer to its man page, either from the command line, or online.

Enjoyed this post?

Subscribe to the feed for the latest updates.