DEV Community

Symlinks

๐Ÿงฉ Intro

In my previous blog post on how to back up and sync your terminal configuration with a GitHub dotfiles repo, we had to manually copy configuration files into the ~/dotfiles directory every time we wanted to push updates to GitHub:

cp ~/.bashrc ~/dotfiles/bashrc
cp ~/.bash_aliases ~/dotfiles/bash_aliases
Enter fullscreen mode Exit fullscreen mode

If you changed ~/.bashrc or ~/.bash_aliases, you had to remember to copy them back into ~/dotfiles before committing.

Thatโ€™s tedious and easy to forget. What if the files stayed in sync automatically? Thatโ€™s where symlinks come in handy.

๐Ÿช„ What is a symlink?

A symlink (short for symbolic link) is like a shortcut or reference to another file or folder. It points to the original location and behaves like the real file, meaning you can open, read, or edit it just like the original. If youโ€™ve ever created shortcuts on Windows, itโ€™s the same concept.

Think of it like this:

  • ๐Ÿ“ Original file: ~/.bashrc
  • ๐Ÿ“Ž Symlink: ~/dotfiles/bashrc โ†’ points to that original file

When a program or shell tries to read ~/dotfiles/bashrc, the system redirects it to the real file ~/.bashrc.

โš™๏ธ Syntax

ln -s <original> <link>
Enter fullscreen mode Exit fullscreen mode

Where:

  • <original>: the original file or directory
  • <link>: the new symlink (the name/path of the shortcut you want to create)
  • The -s flag stands for symbolic

Example:

ln -s ~/.bashrc ~/dotfiles/bashrc
Enter fullscreen mode Exit fullscreen mode

โœ… Result:

  • This creates ~/dotfiles/bashrc as a shortcut to ~/.bashrc.
  • When you edit ~/.bashrc, the change is immediately reflected in ~/dotfiles/bashrc.
  • When you edit ~/dotfiles/bashrc, the change is immediately reflected in ~/.bashrc.
  • You can commit and push ~/dotfiles/bashrc to GitHub without manual copying.

๐Ÿ“Œ Quick symlink cheatsheet

Command Meaning
ln -s <original> <link> Create a symbolic link
ls -l Show links to the original
rm <link> Delete only the symlink (not the original file)
readlink <link> Show where the symlink points

๐Ÿ’ก Things to keep in mind

  • Name collisions: If ~/dotfiles/bashrc already exists, the ln -s command will fail. Delete or rename the existing file first via
rm ~/dotfiles/bashrc
Enter fullscreen mode Exit fullscreen mode
  • Deleting the symlink does not delete the original file; it just removes the shortcut.
  • Deleting or moving the original file will break the symlink.
  • If the original file has been deleted, editing the symlink may either create a new file at the path it points to or throw an error (depending on your editor). So, as long as the original exists, both stay perfectly in sync.
  • File permissions and metadata: A symlink has its own minimal permissions (lrwxrwxrwx), which donโ€™t affect the original. Running ls -l shows the linkโ€™s metadata, not the originalโ€™s. Use ls -lL to view the originalโ€™s details.
  • Relative vs. absolute paths: A symlink stores the path string to its target, either absolute (e.g., /home/user/.bashrc) or relative (e.g., ../.bashrc).
    • Absolute symlinks contain the full path. They work as long as the original stays in the same place, but will break if the file is moved.
    • Relative symlinks use a path relative to the linkโ€™s own location. These are more portable. If you move the whole directory structure (e.g., a Git repo or project folder), the links still work because the relative relationship remains the same. You can create a relative symlink like this:
  ln -s ../real_folder shortcut
Enter fullscreen mode Exit fullscreen mode
  • Symlinks can point to directories as well as files.

โœจ Final Thoughts

Symlinks are a simple yet powerful way to automate your dotfiles workflow.
They remove the need for manual copies and keep your configurations perfectly in sync across devices.

Note: I use ChatGPT to help me gather information and shape these posts, but I always test the commands myself and add my own insights to make them easier to understand. This blog is my personal record of what I learn as I go deeper into Linux. If it helps someone else too, even better!

Top comments (0)