Skip to content
Go back

Linking files using symlink and stow

A symlink is a file that points to another file or directory. You can think of it like a shortcut — it lets you make files and folders available in different places on your system without duplicating them.

The command is simple:

ln -s <path_origin> <path_target>

For example: ln -s ~/.dotfiles/zsh/.zshrc/ ~/.zshrc will create a symlink .zshrc in your home directory pointing to ~/.dotfiles/zsh/.zshrc/.

Using the same command you can create symlinks to folders. You are free to give any name to your symlink:

# Creating symlink random_name pointing to folder_name
ln -s /path/of/the/real/folder_name /some/other/path/random_name

Symlinks create one-off links, which can be tedious if you need to create many of them. You can use stow as a wrapper — it uses ln under the hood — to create many symlinks automatically. A practical example of using stow to manage your .dotfiles:

# Given the following dotfiles folder (repo)
~/.dotfiles/
    |__ zsh
    |__ fzf
    |__ tmux
    |__ nvim
    
# Symlink content of folders above in the parent (user home) directory
stow zsh fzf tmux nvim

The key thing to understand is that stow mirrors the directory structure of each package you give it. For every file in that package, it creates a corresponding symlink in the target folder.

If the content of the tmux folder above is:

~/.dotfiles/tmux
    |__ .config
        |__ tmux
            |__ tmux.conf

Running stow tmux will create the following symlink:

# ~/.config/tmux/tmux.conf  --> ~/.dotfiles/tmux/.config/tmux/tmux.conf
~/.config/tmux/tmux.conf

By default, stow sets the parent directory as the target, as shown above. You can override this and specify the target directory for the symlinks:

stow -t /some_path tmux

# /some_path/.config/tmux/tmux.conf --> ~/.dotfiles/tmux/.config/tmux/tmux.conf
/some_path/.config/tmux/tmux.conf

Practical use cases


 

Next Post
Obsidian PDF Export Styles