Linux Commands

Linux ln command examples

The ‘ln’ command creates the hard and symbolic links between the files.

We will talk about how to create the symbolic links between the files in this article. We will also discuss some useful examples to understand the basic working of the ln command. All examples have been implemented on the Ubuntu 20.04 distribution.

The ln command syntax

Using the ln command, you can create the symlinks between files. This command creates the hard links on a file by default. However, using the (-s or –symbolic) option can also create symbolic links. The following is the syntax of the ln command that is given below:

ln [options] file-name link-name

The ln command creates a link from the specified file (file-name) to the second argument (link-name). However, no second argument is given or only (.) used as the second argument; then, it will create a link of the specified file into a current directory.

Two different kinds of links exist in the Linux system, soft or symbolic links and hard links.

Creating hard links to a file

A hard link can create one on more on a file. You cannot create the hard links for files and directories on a different partition or filesystem. The simplest way to create the hard links is using the ln command.

$ ln test_file.txt link_file.txt

The above command creates a hard link with the name ‘link_file.’

Creating a symbolic or soft link to a file

The symlink is an indirect file pointer. Unlike the hard links, The symbolic or symlink can point to a single file or directory on a different partition or filesystem. To create a symbolic or soft link, use the -s option along with the ln command as follows:

$ ln -s test_file1.txt link_test_file.txt

The above command creates the symbolic link with the name ‘link_test_file.’

To show the created soft link, use the following ls command:

$ ls -l link_test_file

Create a symlink to a directory

You can also create a symlink to a directory through the ln command. For this purpose, use the directory name as the first argument, and the directory link will be used as the second argument.

For example, we are creating a symbolic link from the /home/kbuzdar/test-composer-project directory to the ~/my_project directory by using the following command:

$ ln -s /home/kbuzdar/test-composer-project ~/my_project

Overwrite an existing symbolic link

Using the ln command, you can overwrite an existing symlink. For example, if you try to create a symlink that already created, then the following error will show on the terminal:

$ ln -s test_file1.txt link_test_file.txt

Using the ‘-f’ option, you can forcefully overwrite an existing symbolic link as follows:

$ ln -sf test_file1.txt link_test_file.txt

The above will forcefully create the symlink that already exists.

Conclusion

We have discussed in this tutorial how to use the ln command. We have mentioned different examples of how to create symlinks using the ln command. Moreover, we have discussed how to create links between files and directories using the ln command. I hope the examples mentioned above will help you to understand the ln command. Explore more information about the ln command using the man page of the ln command.

About the author

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.