Create a symbolic link in Linux to a file or a directory path

You wish you didn’t have to copy files and folders twice on your Linux web server, and consistently have to come up with a way to keep the duplicate files or directories synchronized?

A symbolic link in Linux or Unix is a virtual pointer to another directory or file. It acts in all ways as if it were the original file or folder. A symlink will appear in any directory listing (ls -al), and will indicate the original directory or file. All child directories of a symlink directory will inherit the symbolic link’s directory path when accessed through the symlink. Any changes made to the symbolically linked file or directory, will occur in the original actual file or directory, as the symbolic link just points straight to it.

The major difference between a symbolic link and a hard link is that a symbolic link is necessary when linking from one file system mount point to another file system mount point, and a symbolic link can be used with in the file system as well.

Lay the foundation:

To create a symbolic link, the syntax of the command is similar to a copy or move command: existing file first, destination file second. For example, to link the directory /export/space/common/archive to /archive for easy access, use:

The syntax to use for implementing a symbolic link is much like the move or copy command. The original file or directory, the destination file or directory. In the below example, this command would create a symbolic link of /webdocs which would be a virtual link to the /var/www/html directory.

Symbolic Link for directories in Unix

ln -s /var/www/html /webdocs

You may replace the directories as needed when running this command.

Symbolic Link in Unix for files

Just like the command for a directory, the same will work for a specific file. This example would make it so we could reference the virtually linked file banned_hosts.txt from our root directory, rather than having to go to /etc/sbin/apf/deny_hosts.conf

ln -s /etc/sbin/apf/deny_hosts.conf /banned_hosts.txt

Well now what are we gonna’ do?

Implementation of this great feature is really unlimited. Here are just a few examples, but you don’t have to take my word for it!

Potential uses include but are not limited to:

  • Share web applications from multiple virtually hosted domains.
  • Share content from one user’s home directory to another, so as to permit limited access to a specific directory or file
  • Hide the original directory path, or in essence use it for ‘permalinks’ aka pretty links, hiding the not so appealing actual directory path.
  • Organize your Samba SMB shares over the network.
  • Create a customized ftp home directory.

How have you utilized symbolic links?