Ln will work even if file does not exist

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the unix category.

Last Updated: 2024-11-23

ln -s will still "work" (giving a broken link) even if the file you want to link to does not exist. I realized this when deploying oxnotes-docservices. I wanted to create a systemd .service file for my unicorn server and so my script ran

sudo ln -fs /home/ubuntu/oxnotes-docservices/config/deploy/unicorn.service /etc/systemd/system

However, when I looked at the /etc/systemd/system/unicorn.service link by running cat it said the file was not found. The issue was that the source dir to ln did not even exist - it was missing the current folder between oxnotes-docservices and config

I fixed in a general way by adding a test -f SOURCE && before my ln command.