Sometimes, while working on the command line, you need to create links between files. This can be achieved using a dedicated command, dubbed ln. In this tutorial, we will discuss the basics of this tool using some easy-to-understand examples. But before we do that, it’s worth mentioning that all examples here have been tested on an Ubuntu 22.04 machine.

Linux ln command

As you have understood by now, the ln command lets you make links between files. Following is the syntax (or rather a different syntax available) for this tool:

ln [OPTION]... [-T] TARGET LINK_NAME   (1st form)

ln [OPTION]... TARGET                  (2nd form)

ln [OPTION]... TARGET... DIRECTORY     (3rd form)

ln [OPTION]... -t DIRECTORY TARGET...  (4th form)

And here’s how the tool’s man page explains it:

In  the  1st form, create a link to TARGET with the name LINK_NAME. In the 2nd form, create a link 

to TARGET in the current directory. In the 3rd and 4th forms, create links to each TARGET in

DIRECTORY. Create hard links by default, symbolic links with --symbolic. By default, each

destination (name of new link) should not already exist. When creating hard links, each TARGET

must exist. Symbolic links can hold arbitrary text; if later resolved, a relative link is

interpreted in relation to its parent directory.

The following Q&A-styled examples will give you a better idea of how the ln command works. But before that, it’s good for you to get an understanding of what’s the difference between hard links and soft links.

That’s pretty straightforward – all you have to do is to use the ln command in the following way:

ln [file] [hard-link-to-file]

For example:

ln test.txt test_hard_link.txt

<img alt="How to create a hard link using ln" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/11/echo/ln-hard-link.png63779bf87aa35.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="107" loading="lazy" src="data:image/svg xml,” width=”428″>

So you can see a hard link was created with the name test_hard_link.txt.

For this, use the -s command line option.

ln -s [file] [soft-link-to-file]

For example:

ln -s test.txt test_soft_link.txt

<img alt="How to create soft/symbolic link using ln" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/11/echo/ln-soft-link.png63779bf8aa0a7.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="112" loading="lazy" src="data:image/svg xml,” width=”459″>

The test_soft_link.txt file is a soft/symbolic link, as confirmed by its sky-blue text color.

Q3. How to make ln remove existing destination files of same name?

By default, ln won’t let you create a link if a file of the same name already exists in the destination directory.

<img alt="ln command example" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/11/echo/ln-file-exists.png63779bf8eac7a.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="32" loading="lazy" src="data:image/svg xml,” width=”500″>

However, if you want, you can make ln override this behavior by using the -f command line option.

<img alt="How to make ln remove existing destination files of same name" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/11/echo/ln-f-option.png63779bf939dcc.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="57" loading="lazy" src="data:image/svg xml,” width=”500″>

Note: You can use the -i command line option if you want to make all this deletion process interactive.

Q4. How to make ln create a backup of existing files with the same name?

If you don’t want ln to delete existing files of the same name, you can make it create a backup of these files. This can be achieved using the -b command line option. Backup files created this way will contain a tilde (~) toward the end of their name.

<img alt="How to make ln create backup of existing files with same name" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/11/echo/ln-b-option.png63779bf984626.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="63" loading="lazy" src="data:image/svg xml,” width=”500″>

A particular destination directory (other than the current one) can be specified using the -t command line option. For example:

ls test* | xargs ln -s -t /home/himanshu/Desktop/

The aforementioned command will create links to all test* files (present in the current directory) and put them in the Desktop directory.

Conclusion

Agreed, ln isn’t something that you’ll require on daily basis, especially if you’re a newbie. But it’s a helpful command to know about, as you never know when it’d save your day. We’ve discussed some useful command line options the tool offers. Once you’re done with these, you can learn more about ln by heading to its man page. Another interesting command you might want to check out is the tee command.