Creating Symbolic Link in Linux

Creating Symbolic Link in Linux
Photo by Martin Wettstein / Unsplash

Problem:

I have directory like this /var/www/html/my-back-end/public and my Apache web server has virtual domain example.com that points DocumentRoot to /var/www/html/my-back-end/public. Now we have assets directory at /data/example.com/assets, this directory contains images that uploaded by users or admins.

I type https://example.com/assets/images/image.png and it 404 (a.k.a not found). How to resolve this?

Solution:

This can be solved using symbolic links. The error happen because there is no directory called assets inside the /var/www/html/my-back-end/public directory.

Let's go create the symbolic link.

Type this.

ln -s /data/example.com/assets /var/www/html/my-back-end/public/assets

Now on your /var/www/html/my-back-end/public there is a directory called assets and actually pointing to /data/example.com/assets directory.

A symbolic link can be thought of as an alias in many ways. Just like an alias provides a shorthand or a reference to something else, a symbolic link (or symlink) provides a reference to another file or directory in the filesystem.

Here’s a brief comparison to help clarify:

  • Alias: In the context of command-line interfaces or graphical user interfaces, an alias is a shortcut or an alternate name that refers to a command or file. For example, in a shell, you might create an alias to shorten a long command.
  • Symbolic Link: In filesystems, a symbolic link is a file that acts as a pointer or reference to another file or directory. When you access the symbolic link, you are actually accessing the target file or directory it points to. It can be used to simplify access, create shortcuts, or organize files more flexibly.

Both symbolic links and aliases essentially allow you to access or reference something in a more convenient or organized manner.

The command ln -s /data/example.com/assets /var/www/html/my-back-end/public/assets creates a symbolic link in a Unix-like operating system. Here's a breakdown of what each part of the command does:

  • ln: This is the command used to create links in Unix-like systems.
  • -s: This flag tells the ln command to create a symbolic link (also known as a symlink). Without the -s flag, ln would create a hard link instead.
  • /data/example.com/assets: This is the target of the symbolic link. It is the actual file or directory that you want the symlink to point to. In this case, it points to the directory /data/example.com/assets.
  • /var/www/html/my-back-end/public/assets: This is the location where the symbolic link will be created. It is the path where the symlink will reside. The symlink will be named assets and will be located in the /var/www/html/my-back-end/public/ directory.

What This Command Does

  1. Creates a Symlink: It creates a symbolic link named assets in the /var/www/html/my-back-end/public/ directory.
  2. Points to a Target: The symlink points to the /data/example.com/assets directory.
  3. Behavior: When you access the assets symlink in /var/www/html/my-back-end/public/, it will show the contents of the /data/example.com/assets directory. Essentially, it acts as a shortcut to the target directory.

Hope it helps.

Support Us

Subscribe to Buka Corner

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe