The ln command in Linux is a very easy-to-use command. It is used to create links (shortcuts) to the source file/directory. The benefit of using links is more than one file refers to the same original file. In this article, I will demonstrate to you the syntax, options & practical applications of this command in Linux.
A. Description
The ln command in Linux creates links(shortcuts) to the files/directories. There are two types of links and these are hard link & symbolic link.
Hard Link
When a file is linked with a hard link, if you make any changes to any of the files this will reflect on both files. No option is needed to create a hard link to the file.
Symbolic Link
When a file is linked with a symbolic link if any of the files/directories are moved or deleted, this will break the link between the files. Option -s is required to create a symbolic link to the file. The symbolic link is also known as the soft link.
B. Syntax
The ln command in Linux has a simple syntax. It includes the ln command, options & name of the source file & name of the linked file. And the syntax is as follows:
ln [OPTION]... [SOURCE_FILE] [LINKED_FILE]
C. Options
A few options are available for the ln command in Linux. I have listed here some of the most useful options for this command. However, you can look for the ln command on the man page to see more options for this command.
man ln
Useful Options
- -s, Creates symbolic links to files/directories.
- -b, Creates a backup of the file
- –version, Displays version information & exits.
- –help, Displays help page & exits.
Practical Examples of the “ln” Command in Linux
The ln command in Linux is a user-friendly command which is used to create links to source files/directories. Here you will learn some useful examples of the ln command in Linux. In this article, I have worked with the following files.
Download these to work with the “ln” command in Linux
Example 1: Create a Hard Link to a File Using the “ln” Command in Linux
To create a hard link to a file, you can use the ln command in Linux. The file I am using here to be linked is file1.txt. To do the same follow the steps below:
Steps to Follow:
➊ At first launch the Ubuntu Terminal.
➋ To view the files, Write the following command in the command prompt:
ls
➌ Now, hit the ENTER key.
❹ Type the following command in the command prompt:
ln file1.txt hardlink.txt
➎ Then, tap the ENTER button.
❻ To view the hard link file, write the following command in the command prompt:
ls
❼ Finally, press the ENTER button.
❽ To view the contents inside the file1.txt, copy the following command in the command prompt and then hit the ENTER button.
cat file1.txt
❾ To view the contents inside the hardlink.txt, type the following command in the command prompt and then strike the ENTER button.
cat hardlink.txt
Output:
In the image below, you can see the hard link to file1.txt is created using the ln command in Linux.
Similar Readings
- The “diff” Command in Linux [11 Practical Examples]
- The “cmp” Command in Linux [7 Practical Examples]
- The “comm” Command in Linux [10 Practical Examples]
Example 2: Create a Symbolic(Soft) Link to a File Using the “ln” Command in Linux
To create a symbolic link to a file, I am using the ln command in Linux. Here I want to create a link to file2.txt. To do the same follow the steps below:
Steps to Follow:
➊ At first open the Terminal in Ubuntu.
➋ To view the files, Input the following command in the command prompt::
ls
➌ Now, press the ENTER key.
❹ Type the following command in the command prompt:
ln -s file2.txt symlink.txt
➎ Then, strike the ENTER button.
❻ To display the soft link file, type the following command in the command prompt:
ls
❼ Finally, hit the ENTER button.
❽ To view the contents inside the file2.txt, copy the following command in the command prompt and then hit the ENTER button.
cat file2.txt
❾ To view the contents inside the symlink.txt, type the following command in the command prompt and then strike the ENTER button.
cat symlink.txt
Output:
In this image, you can see the soft link to file2.txt is created using the ln command in Linux.In the image below, you can see how the file of the symbolic link looks.
Example 3: Create a Symbolic(Soft) Link to a Directory Using the “ln” Command in Linux
To create a symbolic link to a directory, I am using the ln command in Linux. Here I want to create a link to newFolder. To do the same follow the steps below:
Steps to Follow:
➊ At first open the Terminal in Ubuntu.
➋ To display the files, type the following command in the command prompt::
ls
➌ Now, hit the ENTER button.
❹ Enter the following command in the command prompt:
ln -s newFolder/ SymLink
➎ Then, strike the ENTER key.
❻ To view whether the symbolic link of the directory is created or not, write the following command in the command prompt and then hit the ENTER button.
ls
❼ To view the contents of NewFolder, type the following command in the command prompt:
ls
❽ After that, press the ENTER button.
❾ To view the contents of SymLink, apply the following command in the command prompt:
ls
❿ Finally, press the ENTER key.
Output:
In the image below, you can see the symbolic link to file2.txt is created using the ln command in Linux and SymLink is containing the same contents as newFolder.In the following image, you can see how the directory of the symbolic link looks.
Example 4: Create a Hard Link to a File With the Absolute Path
To create a hard link to a file with the absolute path, you can use the ln command in Linux. Here I am creating a link using the absolute path of the source file named file1.txt & linked file named hardlink.txt.
Steps to Follow:
➊ Initially open the Ubuntu Terminal.
➋ To view the files, Type the following command in the command prompt:
ls MyFolder/
➌ Now, tap the ENTER key.
❹ Copy the following command in the command prompt:
ln /home/sylvie/MyFolder/file1.txt /home/sylvie/MyFolder/hardlink1.txt
➎ Then, hit the ENTER button.
❻ To view the hard link file, write the following command in the command prompt:
ls MyFolder/
❼ Finally, press the ENTER button.
Output:
As you can see in the image below, the hard link to file1.txt is created with the absolute path using the ln command in Linux.
Similar Readings
- The “lsof” Command in Linux [8 Practical Examples]
- The “tar” Command in Linux [12 Practical Examples]
Example 5: Unlink Symbolic Link
To unlink a symbolic link, I am using the unlink command in Linux. To do the same follow the procedure below:
Steps to Follow:
➊ To begin, open the Ubuntu Terminal.
➋ To view the files, write the following command in the command prompt:
ls
➌ Now, tap the ENTER button.
❹ Type the following command in the command prompt:
unlink symlink.txt
➎ Then, hit the ENTER key.
❻ To view whether the file is still linked or not, write the following command in the command prompt:
ls
❼ Finally, press the ENTER button.
Output:
As you can see in the following image, the symbolic link to file2.txt is unlinked using the unlink command in Linux.
Example 6: View the Version Using the “ln” Command in Linux
To view the version, I am using the ln command and option –version in Linux. You can also do the same by following the process below:
Steps to Follow:
➊ To begin, open the Ubuntu Terminal.
➋ To view the files, write the following command in the command prompt:
ln --version
➌ Now, hit the ENTER button.
Output:
In the image, it is showing the version using the ln command in Linux.
Conclusion
In this article, you got to know that the ln command has significant uses in Linux. You’ve also found the syntax, some useful options, and the practical application of this command. So, learn this command and execute it accordingly to become an expert user.
Similar Readings
- The “chgrp” Command in Linux [7 Practical Examples]
- The “file” Command in Linux [9+ Practical Examples]
- The “fsck” Command in Linux [7 Practical Examples]
- The “patch” Command in Linux [4 Practical Examples]
- The “rmdir” Command in Linux [7 Practical Examples]
- The “stat” Command in Linux [9 Practical Examples]