1. Introduction
In this tutorial, we’ll discuss the Linux ln command. ln is a utility that allows us to create a link between files or directories. We’ll look at some examples for both files and directories.
2. Setup
Before we start, let’s set up a test scenario. We create a file using touch:
$ touch my-file.txt
Let’s also create a directory:
$ mkdir my-dir
Let’s do an ls -l to make sure they’re there:
$ ls -l
total 2
drwxrwxr-x. 2 at1349 at1349 6 May 15 15:27 my-dir
-rw-rw-r--. 1 at1349 at1349 0 May 15 15:27 my-file.txt
Now we’re ready to do some linking.
3. Hard Link
We’re going to start by using ln to create hard links.
3.1. Files
By default, ln creates a hard link. A hard link creates a link to a file that is indistinguishable from the original file.
Let’s create a hard link to our my-file.txt:
$ ln my-file.txt hard-link-to-file.txt
Now, if we edit the original file and then view the hard link, the changes will be there.
Conversely, if we edit the link, the change appears as if we edited the original file. This is because a hard link is another name that points to the same underlying index number, or inode, as the original file. Let’s try it out:
$ echo "here is test line one" >> my-file.txt
Here we wrote a string to the original file. Let’s then display the contents of the link we created, using cat:
$ cat hard-link-to-file.txt
here is test line one
Now let’s write another line except we’ll use the hard link this time:
$ echo "here is test line two" >> hard-link-to-file.txt
Checking the contents using the original file name, we see both lines:
$ cat my-file.txt
here is test line one
here is test line two
We can see they have the same inode number of 182491864:
$ ls -li
total 4
182491864 -rw-rw-r--. 2 at1349 at1349 44 May 15 15:38 hard-link-to-file.txt
182821797 drwxrwxr-x. 2 at1349 at1349 6 May 15 15:27 my-dir
182491864 -rw-rw-r--. 2 at1349 at1349 44 May 15 15:38 my-file.txt
The first column value is the same for both my-file.txt and hard-link-to-file.txt.
If we remove the original file, we can see the hard link still exists:
$ rm my-file.txt && ls -li
total 4
182491864 -rw-rw-r--. 1 at1349 at1349 44 May 15 15:38 hard-link-to-file.txt
182821797 drwxrwxr-x. 2 at1349 at1349 6 May 15 15:27 my-dir
It has the same contents as before:
$ cat hard-link-to-file.txt
here is test line one
here is test line two
Finally, let’s recreate my-file.txt by pointing to the previous inode. We do this by making a hard link to hard-link-to-file.txt:
$ ln hard-link-to-file.txt my-file.txt
We’ll verify that the inode is the same:
$ ls -li
total 8
182491864 -rw-rw-r--. 2 at1349 at1349 44 May 15 15:38 hard-link-to-file.txt
182821797 drwxrwxr-x. 2 at1349 at1349 6 May 15 15:27 my-dir
182491864 -rw-rw-r--. 2 at1349 at1349 44 May 15 15:38 my-file.txt
Finally, let’s also verify that the contents are the same:
$ cat my-file.txt
here is test line one
here is test line two
Pretty neat.
3.2. Directories
One last thing to note about ln for hard links is that they don’t work for directories:
$ ln my-dir hard-link-to-dir
ln: ‘my-dir’: hard link not allowed for directory
4. Symbolic (or Soft) Link
Next, let’s look at using ln to create a symbolic link.
4.1. Files
Rather than being a second reference to a file as when using a hard link, a symbolic link is just a pointer to the linked file. Let’s start off by creating one:
$ ln -s my-file.txt sym-link-to-file.txt
Listing the directory, we see the difference between hard and soft links:
$ ls -li
total 8
182491864 -rw-rw-r--. 2 at1349 at1349 44 May 15 15:38 hard-link-to-file.txt
182821797 drwxrwxr-x. 2 at1349 at1349 6 May 15 15:27 my-dir
182491864 -rw-rw-r--. 2 at1349 at1349 44 May 15 15:38 my-file.txt
272172305 lrwxrwxrwx. 1 at1349 at1349 11 May 15 16:13 sym-link-to-file.txt -> my-file.txt
We now have an item pointing to the original file. Notably, the inode value is different because they aren’t the same file.
Operating on a symbolic link works the same as a hard link:
$ echo "here is test line three" >> sym-link-to-file.txt && cat my-file.txt
here is test line one
here is test line two
here is test line three
4.2. Directories
Unlike with a hard link, we can use symbolic links on directories by using the -s option:
$ ln -s my-dir sym-link-to-dir
We now see all three types of links, including our newly created symbolic links:
$ ls -li
total 8
182491864 -rw-rw-r--. 2 at1349 at1349 68 May 15 16:17 hard-link-to-file.txt
182821797 drwxrwxr-x. 2 at1349 at1349 6 May 15 15:27 my-dir
182491864 -rw-rw-r--. 2 at1349 at1349 68 May 15 16:17 my-file.txt
272172304 lrwxrwxrwx. 1 at1349 at1349 6 May 15 16:21 sym-link-to-dir -> my-dir
272172305 lrwxrwxrwx. 1 at1349 at1349 11 May 15 16:13 sym-link-to-file.txt -> my-file.txt
Navigating using the symbolic directory link works the same as going to the actual directory. Let’s use cd and pwd to see this in action:
$ cd sym-link-to-dir/
Using pwd, let’s verify our current location:
$ pwd
/home/at1349/sym-link-to-dir
It’s worth noting that the symbolic link name is the path. This is the default, but it’s also how pwd -L works. To see the actual path, we can use pwd -P:
$ pwd -P
/home/at1349/my-dir
One reason to use a symbolic link would be the ability to change the underlying file but have the symbolic link stay the same. For example, we could create a symbolic link named my-app. Every time we release a new version, we could just repoint the symbolic link to the new version:
$ ln -s my-app-2.1 my-app
We’d always call my-app when we start, but the underlying location can change as we deploy new versions.
5. Conclusion
In this article, we discussed a few general ways to use the ln command in Linux. It’s a useful tool to create hard and symbolic links to files and directories. We can also use it to keep persistent file system paths while changing the underlying files.