What are Symbolic Links (Soft Links) and how to create them under Linux

What is a Soft Link

– Symbolic links link files and directories located across different file systems.
– A symbolic link is a pointer that contains the path name to another file or directory.
– The link makes the file or directory easier to access if it has a long path name.
– A symbolic link file is identified by the letter l in the file-type field as shown below. To view symbolic link files, use the ‘ls -l’ command.

$ ls -l
lrwxr-xr-x  1 geek  wheel  11 Dec 14 07:26 target_file -> source_file

Creating Symbolic Links

You can use the ln -s command to create a symbolic link file. The syntax to create soft link is as follows.

$ ln -s source_file target_file

In the syntax displayed above, the source_file variable refers to the file to which you create the link. The target_file variable refers to the name of the symbolic link. When creating a symbolic link, if the source_file does not exist, a symbolic link that points to a non-existing file is created.

The file name for the symbolic link appears in the directory in which it was created. You can use either relative or absolute path names to create a symbolic link file.

For example, use the ln –s command to create a symbolic link file named file1_link to the file1 file.

ln -s /tmp/file1 /var/tmp/file1_link

Use the ‘ls –F‘ command to display a list of files and directories.

$ ls -Fl /var/tmp
lrwxr-xr-x  1 geek  wheel   10 Dec 14 07:32 file1_link@ -> /tmp/file1

The @ symbol that follows the file name indicates that the file is a symbolic link. The output of the ls -F command above lists the file file1_link as a symbolic link.

Removing Soft Links

You can use rm command to remove the soft link file, just as you would remove a standard file. For example to remove the symbolic link file1_link, use the command as shown below.

$ ls -Fl /var/tmp
lrwxr-xr-x  1 geek  wheel   10 Dec 14 07:32 file1_link@ -> /tmp/file1
$ rm file1_link
Related Post