12

I want to create a symlink

~/.pm2/logs -> /opt/myapp/log

When I run

ln -sFf /opt/myapp/log ~/.pm2/logs

I get a symlink

~/.pm2/logs/log -> /opt/myapp/log

which is not what I want.

I'd prefer a POSIX-compatible solution if possible.

3 Answers 3

17

You already have a directory at ~/.pm2/logs. Since that directory exists, the symbolic link is put inside it.

Would you want that ~/.pm2/logs is a symbolic link rather than a directory, then you will have to remove or rename that existing directory first.

2
  • 1
    I thought "-Ff" flags are specifically made to replace target directory so I don't have to remove it manually. Am I wrong?
    – ptkvsk
    Apr 12, 2019 at 17:02
  • 2
    @ptkvsk The -F flag does something completely different and is not a POSIX option. The -f flag would not unlink a directory. The standard specifies that if the target (the last operand) is a directory, then the link will be place inside it. The -f option does not change this behaviour.
    – Kusalananda
    Apr 12, 2019 at 17:09
7

As other answers say, there is already a directory there.

To avoid this and instead get an error-message, use the -T option, unfortunately I don't think this is Posix (it is GNU).

From the Gnu ln manual (same for cp and mv).

   ln [OPTION]... [-T] TARGET LINK_NAME   (1st form)
   ln [OPTION]... TARGET                  (2nd form)
   ln [OPTION]... TARGET... DIRECTORY     (3rd form)
   ln [OPTION]... -t DIRECTORY TARGET...  (4th form)

Note form 1 without the -T is ambiguous with form 3 (both have two arguments).

In Posix you can force this non-ambiguity by putting a / at the end of a directory name, in form 3, but I don't think there is any thing you can do the other way around. This is why Gnu added the -T option.

2
  • Thank you! I'm on Mac so can't use -T, but still good to know.
    – ptkvsk
    Apr 15, 2019 at 12:55
  • Yes you can, you just need Gnu ln from Gnu binutils. Apr 15, 2019 at 20:23
2

Remove the ~/.pm2/logs directory first, because your target is an existing directory, the link is created inside it.

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.