What are symbolic links?
- unix
- symbolic links
- technology
- linux
Introduction
In unix-based operating systems, symbolic links are a powerful feature that allows you to create a reference to another file or directory. Symbolic links are similar to shortcuts in Windows, but they are more powerful and flexible. In this article, I'll explain what symbolic links are, how they work, and how you can use them in order to make your life easier in all sorts of ways.
Symbolic links can be used in a variety of scenarios, such as organizing files, creating shortcuts, and managing dependencies in software projects. They provide a way to access files and directories from different locations without duplicating the actual data. This can save disk space and make it easier to manage your files. Additionally, symbolic links can be used to create virtual file systems, allowing you to customize your file structure to suit your needs. By understanding and utilizing symbolic links, you can streamline your workflow and improve your overall productivity.
Definition of a symbolic link
According to wikipedia:
In computing, a symbolic link (also symlink or soft link) is a file whose purpose is to point to a file or directory (called the "target") by specifying a path thereto.
So you end up with a file or directory that points to another corresponding file or directory like an old school pointer in C but on steroids. You can sync all types of files including images, videos, and even directories as mentioned above.
Use cases
- Linking files across directories: You can use symbolic links to link files across different directories. This can be useful if you have a file that is used in multiple locations, but you don't want to duplicate it.
- Creating shortcuts: You can use symbolic links to create shortcuts to files or directories. This can be useful if you have a file that is buried deep in a directory structure, and you want to access it quickly.
- Creating aliases: You can use symbolic links to create aliases for files or directories. This can be useful if you have a file that has a long or complex name, and you want to access it with a shorter or simpler name.
Who uses symbolic links?
Very big companies and projects use symbolic links to manage their codebase. For example, pnpm
uses symbolic links to manage dependencies in a monorepo.
This allows them to share code between different projects without having to duplicate it. You can find more information about this in the pnpm documentation.
How to create a symbolic link
We're going to focus on the ln
command which is the most common way to create symbolic links in Unix-based operating systems.
In this example, we're going to create a symbolic link to a file called file.txt
in the current directory.
ln -s /path/to/file.txt link.txt
This command creates a symbolic link called link.txt
that points to the file file.txt
. The -s
flag tells the ln
command to create a symbolic link, and not a hard link.
You can also create a symbolic link to a directory by using the same command. For example, if you want to create a symbolic link to a directory called dir
, you can use the following command:
ln -s /path/to/dir link
This command creates a symbolic link called link
that points to the directory dir
. It is very important to remember
that the first argument is the target and the second argument is the link name you want to create.
What are hard links?
Hard links are another type of link in Unix-based operating systems. Hard links are different from symbolic links in that they point directly to the inode of the file, rather than the file itself. This means that if you delete the original file, the hard link will still point to the file's data. This can be useful in some cases, but it's not as flexible as symbolic links.
Even better solution than symbolic links
If you're a JavaScript developer and you're looking to re-use as much code as possible, you might want to consider using a monorepo along with a monorepo manager like Nx. This way you can share code between different projects without having to worry about symbolic links or any other kind of linking mechanism.
Well those kind of projects do use symbolic links under the hood but you don't have to worry about them, it's
usually handled by the monorepo manager. If you ask me, I prefer pnpm
over any other monorepo manager mainly due to simplicity and speed.
Conclusion
To conclude, I believe you should experiment with symbolic links at least once in your life. They can be very useful in a variety of scenarios, and they can help you organize your files and directories in a more efficient way. If you liked this article, please share your thoughts with me by sending me an email at [email protected].
You can also see what I'm up to & give me feedback for upcoming articles on https://feedback.jimfilippou.com.