Linux ln utility

linux ln utility

How to link between files in linux? What is a hard link? What is a symbolic link and how is it different from a hard link? How do I create a symbolic link in linux? We will talk about this, talking about the linux ln utility.

Description of the ln utility

Linux ln utility allows you to create links between files and directories. Full documentation on the ln command can be viewed on the official page.

Using ln command

ln [OPTION] … [-T] TARGET LINK_NAME.

In this case, ln creates a link to a TARGET file named LINK_NAME. For example, let’s create a simple text file:

$ echo 'PocketAdmin' > test.txt

To create a link to the test.txt file named test.txt.link, you must run:

$ ln test.txt test.txt.link

Let’s check what we got:

$ ls -l
total 8
-rw-rw-r--. 2 user user 12 Oct 20 13:29 test.txt
-rw-rw-r--. 2 user user 12 Oct 20 13:29 test.txt.link

If we now look at the contents of the test.txt.link file, we will see that it is similar to the original:

$ cat test.txt.link 
PocketAdmin

It should be noted that by default ln creates hard links – these are links to a file (you cannot create hard links to directories), which have the following features:

  • have the same rights and permissions as the original file
  • the rights and permissions on the hard link change along with the rights and permissions of the original file
  • renaming, moving and deleting a file does not affect hard links

ln [OPTION] … TARGET

This example is similar to the previous one, but the link name is not specified – it will be the same as the original file. In this case, the link itself will be created in the current directory.

ln [OPTION] … TARGETDIRECTORY
ln [OPTION] … -t DIRECTORYTARGET

In the last two cases, linux ln utility will create links for each TARGET in DIRECTORY. For example, let’s create an include directory in the current directory:

$ mkdir include

We will have the following structure:

ls -l
total 8
drwxrwxr-x. 2 user user  6 Oct 20 13:36 include
-rw-rw-r--. 2 user user 12 Oct 20 13:29 test.txt
-rw-rw-r--. 2 user user 12 Oct 20 13:29 test.txt.link

Now we can create links for both files test.txt and test.txt.link in the include directory with one command:

ln test.txt test.txt.link include/

Let’s check the contents of this directory:

$ ls -l include/
total 8
-rw-rw-r--. 4 user user 12 Oct 20 13:29 test.txt
-rw-rw-r--. 4 user user 12 Oct 20 13:29 test.txt.link

Ln utility options

Linux ln utility accepts the following options:

-b – make a file backup. Let’s give an example. If we try to re-link from the first example, we get a warning that the file already exists:

$ ln test.txt test.txt.link
ln: failed to create hard link 'test.txt.link': File exists

If we use the -b option, then the link will be created, and the old one will be copied with the addition of the ~ suffix:

$ ln -b test.txt test.txt.link

$ ls -l
total 12
drwxrwxr-x. 2 user user 43 Oct 20 13:39 include
-rw-rw-r--. 5 user user 12 Oct 20 13:29 test.txt
-rw-rw-r--. 5 user user 12 Oct 20 13:29 test.txt.link
-rw-rw-r--. 5 user user 12 Oct 20 13:29 test.txt.link~

-d, -F, –directory – allows the root user to try to create a hard link to a directory (but maybe a standard warning about an error creating hard links to a directory)
-f, –force – removes the existing destination file, i.e. an error like ln: failed to create hard link ‘test.txt.link’: File exists will not be shown
-i, –interactive – interactive mode, allows you to specify whether you want to delete an existing file:

$ ln -i test.txt test.txt.link
ln: replace 'test.txt.link'?

-L, –logical – dereference targets that are symbolic links
-n, –no-dereference – treats LINK_NAME as a regular file if it is a symbolic link to a directory
-P, –physical – makes a hard link to the symbolic link itself
-s, –symbolic (-L and -P options are ignored) – makes a symbolic link instead of a hard link. A symbolic link is a file that, instead of data, contains the path to the file it links to. A symbolic link (as opposed to a hard link) is not limited to one file system and can refer to directories. At the same time, if you delete the file to which the link refers, then it will become a bit, and the data is not available. For example, let’s create a symbolic link to our include directory:

$ln -s include/ include2

$ ls -l
total 12
drwxrwxr-x. 2 user user 43 Oct 20 13:39 include
lrwxrwxrwx. 1 user user  8 Oct 20 15:22 include2 -> include/

-S, –suffix=SUFFIX – change the suffix in the name of files that were created using the -b option. Instead of ~ we can specify our own suffix.
-t, –target-directory=DIRECTORY – specify the directory where links will be created (by default, the current directory)
-T, –no-target-directory – treats LINK_NAME as a regular file
-v, –verbose – when creating, display the names of all links:

$ ln -v test.txt test.txt.link
'test.txt.link' => 'test.txt'

–help – display utility help and exit
–version – print version information and exit

$ ln --version
ln (GNU coreutils) 8.30

You can find even more useful Linux utilities on this page