17

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.

Kusalananda
  • 320,670
  • 36
  • 633
  • 936
ptkvsk
  • 273
  • 1
  • 2
  • 6

3 Answers3

18

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.

Kusalananda
  • 320,670
  • 36
  • 633
  • 936
  • 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 '19 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](https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/ln.html) 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 '19 at 17:09
8

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.

ctrl-alt-delor
  • 27,473
  • 9
  • 58
  • 102
3

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

XrXca
  • 119
  • 3