Is there a way to create a symlink whose target does not exist using shell scripts?

From reading man 1 ln, I do not see an option to do so; and even -f checks if the target exists.

Is there a way to achieve what I'm looking for?


When you run

ln -s nonexistenttarget link

ln doesn’t check whether nonexistenttarget exists, it creates the link, unless link already exists. -f works around the last part by deleting link if necessary.

The impact of a non-existent target is only felt when a program tries to dereference the link, e.g. by opening it:

$ ls -l link
lrwxrwxrwx 1 steve steve 17 May 22 08:44 link -> nonexistenttarget

$ cat link
cat: link: No such file or directory
  • I once saw a script here somewhere (can't find it now) that created symbolic links that contained the text of a shell script, by mistake (wrong variable used in ln -s command). – Kusalananda May 22 '19 at 7:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.