Linux ln command: Creating links in Linux

You can easily create links in Linux using the command line. The ln command for creating links is easy to use - most use cases require just a few lines of code. If you want to work efficiently and precisely, though, you should be aware of the difference between hard and soft links. Keep reading to find out about the two types of links, view Linux ln examples, and learn which code you’ll need.

What is the Linux ln command?

The ln command is used to create links to files or directories. (‘ln’ is short for ‘link’.) The command is given to the Linux command line (also called the shell), which can be opened and operated using a terminal window. It is one of the most important and most frequently used terminal commands and can be used to create soft or hard links.

What are soft and hard links?

If you want to take full advantage of the ln command, you’ll need to know the difference between soft and hard links. Soft links are cross references that point to a file or directory. If you move or delete the original file, there is no longer a target and the link leads to nothing. On the other hand, if you delete the link, the original final link remains intact. Soft links are also called symbolic links or symlinks. Soft links can be created in the Linux terminal and in the graphical user interface in the Linux file manager. You can spot a soft link in Linux by the arrow symbol on the file icon.

Hard links can only be created using the command line. They’re more extensively integrated into the memory management of an operating system and the file system being used. A hard link is a mirror copy of the original file, which has its own directory entry. Several hard links can point to the same file. In fact, Linux doesn’t distinguish technically or operationally between the original and a hard link generated later, meaning that all directory entries are treated the same and work independently of one another.

Let’s take a look at a concrete example. Say there is a video file that is only accessible in the directory ‘My Videos’. After creating a hard link, it can also be retrieved from another directory, e.g. ‘My Videos Backup’. If the original file is deleted from ‘My Videos’, it is still perfectly accessible via the hard link path to the file in ‘My Videos Backup’. This is an advantage of hard links - the additional entry in memory management doesn’t use any extra memory. It simply serves as an alternative option for access (via file path) and is not a second physical copy of the file.

Hard links are closely connected with special internal bookkeeping in Linux. Every hard link refers to an inode and is assigned a unique inode number, which is the same one assigned to the original file. A file isn’t really deleted from the inode system (and thus the entire system) until all entries (i.e. all references to the file) are made invalid by delete operations and an internal link counter is set to zero. On the other hand, if only one address entry among many is deleted or if the original file is moved into another directory, nothing changes. The remaining options for accessing the original file, which are stored in inode, are still valid.

Fact

Inodes are defined data structures that uniquely describe a file, contain meta-information about it (group membership, owner, access rights) and document where it is saved (in the form of a memory address).

How should hard links and soft links be used?

Soft links are usually sufficient for normal users to create links for most purposes. They can be used to link files and directories across file systems and partitions and on various hard drives. However, soft links aren’t as flexible as hard links when it comes to making changes to the reference object (such as moving and deleting it).

Hard links can usually link files, not directories or folders. And because inode numbers can only be managed within partitions, hard links can only connect files within a single partition. However, modern Linux systems often contain various file systems located on different hard drives and partitions. So, if you’re working with hard links, you should know your system and know the basics of partitioning and formatting.

Hard links have advantages, especially when it comes to backups. You get alternative access to the original file, while saving space and increasing data security. The software HardlinkBackup takes advantage of the positive aspects of hard linking.

Since hard links directly represent the original file, they don’t need to be dereferenced using computing processes. This means they can be processed faster and are fully transparent for applications. You can also use hard links to solve special problems. For example, if a program needs a library that is no longer up to date, a hard link can keep it functioning. It will take on the name of the out-of-date (and deleted) library and redirect to a more up-to-date version.

How is the Linux ln command implemented?

We’ll now present some simple examples of how to create links in Linux. The command syntax is suitable for important routine tasks that regularly arise. More complex linking processes can also be implemented using the ln command, though these will require a bit of experience with the command line and involve more complex code.

Creating a hard link within one directory

The default setting for the Linux ln command is to create hard links. The format for the command is as follows:

ln TargetFilePath Reference

For example, in the code below a hard link is created for a video file (video.mp4) in the user profile named ‘Peter’ (/home/peter/). The original file and the newly created hard link are located in the same directory for videos. The hard link can then be moved without any problems and without losing its validity.

ln /home/peter/videos/video.mp4 hard_link_to_video_file

Creating a hard link across directories

If you want to, for example, create a link to the video file on the desktop, the code would look as follows:

ln /home/peter/videos/video.mp4 /home/peter/Desktop/hard_link_to_video_file

Creating a soft link for files

If you want to create a soft link with the Linux ln command, you can use the ‘-s’ option (s = symbolic). The syntax for a soft link is as follows:

ln -s TargetFilePath Reference

The code for creating a soft link to a video file in the same folder would look like this:

ln -s /home/peter/video.mp4 soft_link_to_video_file

Creating a soft link in another directory

You can also create soft links in other directories, such as on the desktop:

ln -s /home/peter/videos/video.mp4 /home/peter/Desktop/soft_link_to_video_file

If you want to have more control over the linking process and view more information, the option ‘-v’ (v = verbose) may be of help. It lists the names of every linked file on the screen, which can be especially useful in larger undertakings. It’s implemented as follows:

ln -s -v /home/peter/videos/movie.mp4 /home/peter/Desktop/soft_link_to_video_file

A more complex linking operation is shown below. This Linux ln example uses the option ‘-t’ (t = target directory), which specifies the directory in which links should be created.

ls Invoice* | xargs ln -s -t /home/peter/Desktop/

This code creates links to all files that start with ‘Invoice’ and are located in the current directory and places them in the desktop directory. (The asterisk serves to include all files whose name starts with ‘Invoice’, e.g., ‘Invoice_January’, ‘Invoice_February’).

Removing soft links

Soft links can be removed with the Linux command ‘rm’ (rm = remove). The following code deletes a soft link to a video file in the current directory.

rm soft_link_to_video_file

If you want to delete several files in the current directory, you can simply list them one after another and separate them with a space.

rm softlink_video1 softlink_video2 softlink_video3 softlink_video4

The option ‘r’ (r = recursive) can be used to delete all the soft link files in a soft link folder. If the folder contains sub-folders, those will be deleted as well.

rm -r ~/Desktop/Softlink-Files/*

Creating a soft link for directories

This code is used to link to a directory rather than a file. The following line of code places a soft link to the video folder onto the desktop:

ln -s /home/peter/videos/ /home/peter/Desktop/soft_link_for_video_folder

You don’t always need to use full path names (absolute paths) when working with the Linux ln command. The Linux shell and the ln command accept relative paths and shortcuts. For example, a tilde (‘~’) can stand in for the absolute path of the current user’s home directory (in our example /home/peter/). This is how the shortened code looks for a soft link that points to the directory for videos on the desktop:

ln -s ~/videos ~/Desktop/soft_link_for_video_folder
Note

It’s not possible to create new directory structures with the ln command. When creating links using the Linux ln command, all directories and subfolders listed should already exist, otherwise you could run into errors.

Creating several soft links at once

You can also link to both a directory and a file at once. The syntax for that is as follows:

ln <Option> <Linkobject1> <Linkobject2> <Target directory with soft links for link objects>

To see how this works in practice, let’s make ‘Linkobject1’ the film file in Peter’s video folder again. ‘Linkobject2’ is a folder with the name ‘Test’, which is located in a sub-folder of Peter’s video directory. The soft links to the file and the folder will be generated in the target directory ‘Soft_link_folder’. The code looks as follows:

ln -s ~/videos/video.mp4 ~/videos/test/ ~/Desktop/Soft_link_folder

The target directory ‘Soft_link_folder’ should already exist on the desktop, otherwise the operation won’t work.

Creating soft links with backup

If you want to use the ln command to create a backup copy of files that already exist in the target directory, you can use the option ‘-b’ (b = backup).

ln -s -b /home/peter/videos/movie.mp4 /home/peter/Desktop/soft_link_to_video_file

The backup file will then appear with a tilde at the end of its name (e.g., soft_link_to_video_file~). If you don’t need a backup and want to overwrite an older soft link file, use the option ‘-f’ (f = force):

ln -s -f /home/peter/videos/movie.mp4 /home/peter/Desktop/soft_link_to_video_file

Insert the option ‘-i’ (i = interactive) if you’d like to see a prompt before removing targets. And if at any point you need more help with the Linux ln command, the option ‘--help’ will show you all available parameters and options for the ln command.

Was this article helpful?
Page top