How to Create and Remove soft links (symbolic links) in Linux
Files (and directories) might be located on several different ﬁle systems. You can use symbolic links to link ﬁles that are in different ﬁle systems.
There are two main reasons you might choose to use symbolic links:
- To move ﬁles to a new location – This includes moving a directory on a different disk (partition)but leaving a link so that other users do not need to know about the move.
- To provide a convenient name for a ﬁle rather than the original name, which might be complicated or unknown – When accessing a ﬂoppydisk, a user can type ls /floppy/floppy0 without having to ﬁnd out what the ﬂoppy disk is called.
A ﬁle system is a collection of certain types of ﬁles, organized for administrative convenience. The organization of these ﬁles enables you to store ﬁles that need to be shared on one machine. These shared ﬁles can be accessed by many machines by using a remote ﬁle access mechanism.
A symbolic link is a pointer that contains the pathname to another ﬁle or directory. The link makes the ﬁle or directory easier to access if it has a long pathname. A symbolic link ﬁle is identiﬁed by the letter l in the ﬁle-type ﬁeld. To view symbolic link ﬁles, perform the ’ls -l’ command.
Creating Symbolic Links
You can use the ‘ln -s’ command to create a symbolic link ﬁle. You can use either relative or absolute path names to create a symbolic link ﬁle. The ﬁle name for the symbolic link appears in the directory in which it was created. The syntax for the ln -s command is:
ln -s source_file target_file
The source_file variable refers to the ﬁle to which you create the link. The target_file variable refers to the name of the symbolic link. When creating a symbolic link, the source_file might not already exist. If the source_file does not exist, a symbolic link that points to a non-existing ﬁle is created. To create a symbolic link ﬁle, named dante_link, to thedante ﬁle, perform the ln -s command.
$ cd $ pwd /export/home/user1 $ mv dante /var/tmp $ ln -s /var/tmp/dante dante_link $ ls -F Reports/ dante_link@ dir3/ feathers_6 file1 fruit brands dir1/ dir4/ file.1 file2 fruit2 dir10/ dir5/ file.2 file3 newdir/ dante_1 dir2/ feathers file.3 file4 tutor.vi $ cat dante_link The Life and Times of Dante by Dante Pocai ....
You can display a list of ﬁles and directories, by performing the ’ls -F’ command. The output of the ls -F command shows the ﬁle dante_link with the @ symbol following it to indicate that dante_link is a symbolic link. To see the path name to which a symbolic link is pointing, perform the ls -l command with the symbolic link ﬁle name.
$ ls -l dante_link lrwxrwxrwx 1 user1 staff 5 Nov 19 14:45 dante_link -> /var/tmp/dante
Removing Symbolic Links
You can use the rm command to remove a symbolic link ﬁle in the same manner as you would remove a standard ﬁle. To remove the dante_link symbolic link ﬁle, perform the rm command.
$ cd $ pwd /export/home/user1 $ ls -l dante_link lrwxrwxrwx 1 user1 staff 5 Nov 19 14:45 dante_link -> dante $ rm dante_link $ cat dante No such file or directory $ ls dante dante_link -rw-r--r-- 1 user1 staff 5 Nov 19 14:45 dante dante_link: No such file or directory $ mv /var/tmp/dante ~/dante