HomeA-Z Commands12 Practical Examples of In Command in Linux

12 Practical Examples of In Command in Linux

Using Linux is fun with the CLI mode in the terminal section. There are tons of commands that can make your Linux journey smoother if you just know the right command. There are many commands in Linux that require to be associated with the url or path of the file. While writing a command, remembering both the command and path is really havoc, and it slows down the efficiency. With the ln command in Linux, you can easily generate, connect, and assign soft and hard links inside the command shell. The full form of the ln is the link command; it helps us to generate links to files and directories on a Linux system.


So you might wonder what is the difference between the link and the ln command in Linux. The main difference between the link and the ln command is when you run it, by default, the link command creates a hard link on the system wherein the ln command creates a soft link.

Here you can see a practical example of the ln and the link command below. In Linux, the ln command is probably the most used command for its ability to be assigned as a path in both soft links and hard links.

Here is another example of the difference between the ln command and the link command; the ln command directly makes the links between files, wherein the link command calls the link function and then generates the link between the files.

ln file1.txt file2.txt
link file1.txt file3.txt

The difference between ln and link

ln Synopsis and Syntaxes


Now I am going to add some of the most important synopsis and syntaxes and flags that you might need to know about the ln command in Linux. Knowing these below-mentioned syntaxes will help you to write your code efficiently.

  • –backup[=CONTROL]: Backup the each and every existing destination of the soft or hard links
  • -b: This one is for back up the path
  • -f, –force: This one can force to overwrite the linking
  • -i, –interactive: The i flag prompts a window before you execute a command in the shell
  • -L, –logical: The i defines the logical operation that can establish a link between your path and another directory
  • -s, –symbolic: The s is for the symbolic link or soft links
  • -S, –suffix=  The capital S is used to define the suffix, which is usually effective to use rather than using the “~” the sign
  • -t, –target-: Use -t for the target location
  • -v, –verbose: The small v defines the command as a verbose or verbal description of the command
  • –help: The help is for printing the help and manual
  • –version: The version is to check the current version of the ln command in Linux

Examples of ln Command in Linux


The ln command has a lot of syntaxes, arguments, and symbols that can help a Linux system admin assign the command with various tasks. The ln is an important command because, in Linux, the use of the ln command has already been established inside the OS code; now, you will need to figure out how you can use it on your shell. The ln command creates a target directory with a soft or hard link.

In Linux, the server-level administration jobs require remembering the server address path and location. If you are a system admin, then obviously, you need to remember, or you need to take notes of that very lengthy path or URL.

With the ln command in Linux, you can create symbolic links to make your path shorter so that you can remember it easily. Another example of using the ln command for the symbolic link is if you need to copy your files in multiple locations, you don’t need to copy the physical file; you can just use the symbolic link through the ln command.

Using the hard links through the ln command is also easy if you need to store your file as the actual file with all the metadata and in the actual sizes.


The below-mentioned ln command will allow us to create a soft link to the actual file. The very first syntax with the -s flag denotes that we’re going to make a soft link, and later we have put the file path and then the name of the soft link.

$ ln -s {source-filename} {symbolic-filename}
$ ln -s /webroot/home/httpd/test.com/index.php /home/ubuntupit/index.php
$ ls -l

Create a soft link with ln command from webroot


If you edit the symbolic link, that won’t affect the original file. But, if you use the symbolic url to connect another command, changing the existing soft link will also require updating the other links created with this symbolic link.

Say, for instance, if we have a symbolic link for the directory /home/ubuntupit/index.php linked with the /webroot/home/httpd/test.com/index.php, the path is required to be updated when we change the symbolic link.

We can use any notepad or text editor to edit the link and make the changes inside the script.

$ sudo nano /home/ubuntupit/index.php

Another advantage of the ln command is if we create a symbolic link with the ln command and remove the symbolic link with the rm command, it does not affect the original file in the disc.

$ rm /home/ubuntupit/index.php ## <--- link gone ##
$ ls -l /webroot/home/httpd/test.com/index.php

We can also create the symbolic link to a directory in a Linux System with the ln command. For instance, the below-mentioned ln command will create a symbolic link to the home directory with the symbolic name app.

$ ln -s {source-dir-name} {symbolic-dir-name}
$ ln -s /home/lighttpd/http/users/ubuntupit/php/app/ /app/

If we run the command and edit the path app it will be editing the original path.

$ cd /app/
$ ls -l
$ nano config.php

By using an -f flag with the ln command, we can overwrite the symbolic link in the Linux system. Here the below-mentioned ln command will forcefully overwrite the data of the soft link.

ln -f -s /path/to/my-cool-file.txt link.txt

Removing the symbolic links created with the ln command is very easy on Linux systems. You can execute the following commands below to remove your soft links from the system. Please note removing the soft links will not remove the actual file on the system

rm my-link-name
unlink /app/
rm /home/ubuntupit/index.php

Another use of the ln command is if you create a hard link for a file. You can execute the below-mentioned ln command on your terminal shell to make a hard link. In the below-mentioned command, the first part is the reference (source) file, and the second one is the hard link that we generate through the ln command on Linux.

$ ln test_file.txt link_file.txt

If you have two different directories in the Linux system, then you can create a soft link with two different directories through the ln command.

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

Creating a soft link in another directory with ln command on Linux

8. Get More Control Over the Linking 


If you want to create a soft link with more controlling power on your Linux system through the ln command, you can use the -v flag in your command. This verbose command will display the name of the files that you have linked through the soft link. This might help you hyperlink or control your links if you have numerous files in your system.

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

Through the ln command on Linux, you can also use a -t flag to assign the target folder to create a soft link.

ln Invoice* | xargs ln -s -t /home/ubuntupit/Desktop/

This one would be very helpful for those who need to perform soft linking a lot on the Linux system. With the help of the ln command, you can assign multiple directories to create a soft link.

ln <Option> <Linkobject1> <Linkobject2> <Target directory with soft links for link objects>
ln -s ~/videos/video.mp4 ~/videos/test/ ~/Desktop/Soft_link_folder

In Linux, the ln command also allows you to backup your file if that already exists in the destination directory where you want to create the soft links. To create a backup of the soft links, you can use the -b flag with the ln command in the Linux terminal shell.

ln -s -b /home/ubuntupit/videos/movie.mp4 /home/ubuntupit/Desktop/soft_link_movie_file

Creating soft links with backup

And if you do not need to backup your file in the destination directory, you can use the -f flag to forcefully overwrite your file to create a normal soft link.

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

If you want to share your directory with a new user through the ln command, there are options for sharing your directory through a soft link. To generate shareable soft links for a new user, you will need to perform the ln command with the right arguments with sudo access.

At first, you need to create a file under /etc/sudoers/ directory. Here, I am giving the format and the commands.

Create file /etc/sudoers.d/any-descriptive-name-here with such content:
user ALL = NOPASSWD: /bin/ln -s /source/location /destination/location

12. ln Help Page 


The commands below will help you to get the details about the syntax and how to use the commands in the appropriate format. To check the manual page and the help page of the ln command, you can directly execute the below-mentioned commands in the terminal shell.

$ man ln
$ ln --help

ln --help

Issues You Might Face While Using ln Command


So far, we have seen a few examples of the ln command in Linux or Unix systems. If you are a newbie to Linux, you might face some issues while executing the ln command.

1. ln Works in CLI But Not in The Script


One of the most discussed issues regarding the ln command is that it works perfectly in your terminal shell, but it does not execute properly when you put the same command in the script.

To solve this issue, make sure that the script you’re writing is perfectly well written to be synced with the ln command, and there are not so many loops in the script. If you run too many loops inside the script and in the ln command, your functions might get stuck in the loop, and you might not get accurate results.

For instance, the below-mentioned ln command might work perfectly in the terminal shell, but it might not function well when you put it in a complicated script.

ln ../files/file_01_am.dat

Here is a complicated and faulty script line that will show you an error. As you can see, there is a for loop in the script, and your ln function will get lost inside this loop.

for file in `ls -1 ../files/file_??_??.dat` ; do echo "$file" ; ln "$file" ; done

You might need to add the below-mentioned replace line in the script to fix this issue.

find /media/file_??_??.dat -exec ln {} \;

While you use the ln command in Linux to create a symbolic link, please be mentioned that you will need to assign an absolute directory to create a simple URL. Many of us use the relative path while creating a soft link. Later, they might get a broken soft link error when they execute the symbolic command with the absolute path.

While executing the ln command, please make sure that you assign the same path you created with the ln command.

Insights!


In the entire post, we have seen how to use the ln command in Linux, what are the ln syntaxes and what they mean. We have also seen a few important examples of ln commands that you must need to know to make your Linux experiences smoother.

Knowing the ln command is important to becoming a successful Linux administrator. If you gather a list of 100 commands that you need to know, the ln command must be placed at the very top rows on the list.

If you find this article has been useful for you, please do not forget to share this post with your friends and the Linux community. We also encourage you to write down your opinions in the comment section regarding this article.

Mehedi Hasan
Mehedi Hasan
Mehedi Hasan is a passionate enthusiast for technology. He admires all things tech and loves to help others understand the fundamentals of Linux, servers, networking, and computer security in an understandable way without overwhelming beginners. His articles are carefully crafted with this goal in mind - making complex topics more accessible.
  1. You keep using the terms “hard link” and “soft link” without once defining them. I know I have trouble remembering; my way is to ignore hard links. But I would have appreciated a definition here. It makes me wonder whether you know the difference.

LEAVE A REPLY

Please enter your comment!
Please enter your name here


You May Like It!

Trending Now