How to Create and Remove soft links (symbolic links) in Linux

Files (and directories) might be located on several different file systems. You can use symbolic links to link files that are in different file systems.

There are two main reasons you might choose to use symbolic links:

  • To move files 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 file rather than the original name, which might be complicated or unknown – When accessing a floppydisk, a user can type ls /floppy/floppy0 without having to find out what the floppy disk is called.

A file system is a collection of certain types of files, organized for administrative convenience. The organization of these files enables you to store files that need to be shared on one machine. These shared files can be accessed by many machines by using a remote file access mechanism.

A symbolic link is a pointer that contains the pathname to another file or directory. The link makes the file or directory easier to access if it has a long pathname. A symbolic link file is identified by the letter l in the file-type field. To view symbolic link files, perform the ’ls -l’ command.

You can use the ‘ln -s’ command to create a symbolic link file. You can use either relative or absolute path names to create a symbolic link file. The file 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 file 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 file is created. To create a symbolic link file, named dante_link, to thedante file, 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 files and directories, by performing the ’ls -F’ command. The output of the ls -F command shows the file 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 file name.

$ ls -l dante_link
lrwxrwxrwx   1 user1    staff          5 Nov 19 14:45 dante_link ->
/var/tmp/dante

You can use the rm command to remove a symbolic link file in the same manner as you would remove a standard file. To remove the dante_link symbolic link file, 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