How to Create a Symbolic Link in Linux using ln Command
In This Linux tutorial We are going to learn how to create a symbolic link in Linux using ln command.
What is a Symbolic Link in Linux ?
Also Called as soft links, A symbolic link in Linux is a pointer to another file or directory also what we called the target. When you access a symlink you actually accessing the original file and its content. If the original file deleted, then the symlink will be inaccessible, But if symbolic links are removed, it won’t effect to the original file. A symbolic link is just like a shortcut, very similar to shortcuts in Windows Operating System.
Linux ln command with the -s option uses to create symlink in Linux and Unix operating systems.
ln -s targe_file linkfile_name
- Do not Forget the -s option. -s option uses to make symlink with ln command in Linux/Unix.
- Target file must exist before create a symbolic link. By default link file cannot be exist unless you use -i or -f option with ln command.
Example
ln -s /etc/passwd /var/user_details
In the above example, I created a symlink called user_details inside the /var folder and the user_details point to the /etc/passwd file. Now when you access the /var/user_details file, you actually access the /etc/passwd file.
Also, if you run the ls command you will notice that user_details file is pointing to the /etc/passwd file as below screenshot shows.
Create Symbolic Link to Directory
It is also Possible to create symlinks to directories.
Example
ln -s /var/www/html /home/user/Desktop/web
Above example, create a symbolic link inside the user’s desktop pointed to the /var/www/html folder. Whenever the user wants to access /var/www/html folder user can simply click the link on the desktop.
Command Options
-f By default link name cannot be exist already when creating symlinks. What -f option does is remove the destination file and recreates if it already exist.
-i Very Similar to -f option, but ln -si will ask for the confirmation before removing the destination file.
Example
ln -si /etc/passwd /var/user_details
-b Instead of just removing, -b option will make a backup of the destination file before recreating it.