I have something like

folder A and when I do ln -s A a it creates the sym linked folder a now if I repeat the command ln -s A a I get the dead link a/A

Is there a way that I can have ln fail if the link exists other then wrapping everything in if exists statements ?

2 Answers 2


If all you're looking for is a single conditional test for a single command, you don't need an if statement—just use a list.

According to LESS=+/Lists man bash:

   A  list  is a sequence of one or more pipelines separated by one of the
   operators ;, &, &&, or ||, and optionally terminated by one of ;, &, or
   An AND list has the form

         command1 && command2

   command2  is  executed if, and only if, command1 returns an exit status
   of zero.

Let's say the folder you expect to be there is mydir and you want to create a link mylink2dir only if the dir exists. You could use:

[ -d mydir ] && ln -s mydir mylink2dir

Or equivalently:

test -d mydir && ln -s mydir mylink2dir
  • Thats true at least its compact . You dont have any insight as to why ln functions like that ? It reminds me of OSX s super annoying cp -R place/ Apr 20, 2016 at 3:10
  • @Prospero, it can be extremely useful to create a link to a file that doesn't exist yet. As to why it was designed that way, it's part of the Unix Philosophy: "Do one thing and do it well." ln exists to create links, not to test for the existence of files. It's not necessary for a file to exist to have a link made to it, so ln doesn't test that. The test utility (also known as [), on the other hand, has only the purpose of testing conditions. So you can string them together any way you like. Hope that helps.
    – Wildcard
    Apr 20, 2016 at 3:13
  • its really the fact that ln will do two different things with the same command when one of the outcomes seems to have no application ; unless I guess if your some kind of crazy robot super hero and are recursively ln ing into the unknown ha Apr 20, 2016 at 4:18
  • @Prospero, regarding "ln will do two different things with the same command" —not really. Whether the actual file is there or not, all ln does is create a new path that points to the same location as the specified path. The specified path could be a directory, a FIFO, a socket, a block device, a regular file, or even another symlink—or it could not exist. You're just manipulating paths, not files. How would it be beneficial to have separate commands for each of the cases I've just listed? It would just make it harder to learn.
    – Wildcard
    Apr 20, 2016 at 4:28
  • Im not communicating well enough what my confusion is , I dont mean to use up your time ! Its the fact that I can use ln to create a link into a folder then the same command creates a link into that link instead of failing because the folder exists . Apr 20, 2016 at 5:32

Use the -n option to avoid creating a directory. This will overwrite an existing symbolic link, but not create a link inside a directory that's pointed to by an existing symlink.

ln -sn A a

ln -n isn't POSIX but exists on GNU coreutils, BusyBox and *BSD (including OSX).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .