How to: Linux / UNIX create soft link with ln command

ADVERTISEMENTS



Two types of links

What is a soft (“symbolic”) link? How do I create a soft link (symbolic link) under UNIX or Linux operating system?To make links between files you need to use ln command. A symbolic link (also known as a soft link or symlink) consists of a special type of file that serves as a reference to another file or directory. Unix/Linux like operating systems often uses symbolic links. This guide explains how to use the ln command to create symbolic/soft links.

There are two types of links

symbolic links (also known as “soft links” or “symlinks”): Refer to a symbolic path indicating the abstract location of another file.

(also known as “soft links” or “symlinks”): Refer to a symbolic path indicating the abstract location of another file. hard links : Refer to the specific location of physical data.

How do I create soft link / symbolic link under Unix and Linux?

Soft links are created with the ln command. For example, the following would create a soft link named link1 to a file named file1, both in the current directory

$ ln -s file1 link1

To verify new soft link run:

$ ls -l file1 link1

Sample outputs:

-rw-r--r-- 1 veryv wheel 0 Mar 7 22:01 file1 lrwxr-xr-x 1 veryv wheel 5 Mar 7 22:01 link1 -> file1

From the above outputs it is clear that a symbolic link named ‘link1’ contains the name of the file named ‘file1’ to which it is linked.

How to use the ln command

So the syntax is as follows to create a symbolic link in Unix or Linux, at the shell prompt:

$ ln -s {source-filename} {symbolic-filename}



For example create a softlink for /webroot/home/httpd/test.com/index.php as /home/vivek/index.php, enter the following command:

$ ln -s /webroot/home/httpd/test.com/index.php /home/vivek/index.php

$ ls -l

Sample outputs:

lrwxrwxrwx 1 vivek vivek 16 2007-09-25 22:53 index.php -> /webroot/home/httpd/test.com/index.php

You can now edit the soft link named /home/vivek/index.php and /webroot/home/httpd/test.com/index.php will get updated:

$ vi /home/vivek/index.php

Your actual file /webroot/home/httpd/test.com/index.php remains on disk even if you deleted the soft link /home/vivek/index.php using the rm command:

$ rm /home/vivek/index.php ## <--- link gone ##

## But original/actual file remains as it is ##

$ ls -l /webroot/home/httpd/test.com/index.php

Creating Symlink to a directory

The syntax remains same:

$ ln -s {source-dir-name} {symbolic-dir-name}

For example, create a symbolic link from the /home/lighttpd/http/users/vivek/php/app/ directory to the /app/ directory you would run:

$ ln -s /home/lighttpd/http/users/vivek/php/app/ /app/

Now I can edit files using /app/

$ cd /app/

$ ls -l

$ vi config.php

How to overwrite symlinks/Soft link

Pass the -f to the ln command to overwrite links:

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

How to delete or remove symlinks/soft links

Use the rm command to delete a file including symlinks:

rm my-link-name

unlink /app/

rm /home/vivek/index.php

Getting help about the ln command

Type the following ln command:

$ man ln

$ ln --help

ln command option Description --backup make a backup of each existing destination file -b like --backup but does not accept an argument -d allow the superuser to attempt to hard link directories (note: will probably fail due to system restrictions, even for the superuser) -f remove existing destination files -i prompt whether to remove destinations -L dereference TARGETs that are symbolic links -n treat LINK_NAME as a normal file if it is a symbolic link to a directory -P make hard links directly to symbolic links -r create symbolic links relative to link location -s make symbolic links instead of hard links -S override the usual backup suffix -t specify the DIRECTORY in which to create the links -T treat LINK_NAME as a normal file always -v print name of each linked file --help display this help and exit --version output version information and exit

Conclusion

You learned how to create a symbolic link in Linux using the ln command by passing the -s option. See ln command man page here for more information.