I have two folders, /test/opt and /test2/opt.

I want to create a symbolic link so that when application will write something to /test/opt it will go to /test2/opt.

I tried creating a symlink using ln -s /test2/opt /test/opt. But the thing happening is: it is creating the opt directory inside /test/opt and it's getting linked to /test2/opt.

Don't know what's happening here. test has other directory as well.
Tried adding / on both sides one side of source / target but it's not happening.

  • If /test/opt already exists, ln won't be able to put a symlink to /test2/opt at that location without removing it. Sep 27 '19 at 14:41
  • You want to say vice versa? So what should I do? My /test/ is separate mount where now no space available so I want to redirect new files from /test/opt to this new folder (/test2/opt) which is under different mount where I have space .
    – anant
    Sep 27 '19 at 15:00
  • yeah, point is, destination of a symlink can't already exist. Sep 27 '19 at 15:02
  • Ok so it will delete /test2 & /test2/opt and then will run script.
    – anant
    Sep 27 '19 at 15:05
  • but just as ln won't remove an exising directory by default, it also won't recursively create them. Just like if you wanted to create any other type of file in /test2, if you want to create a symlink in /test2, that directory must exist first. Sep 27 '19 at 15:29

What you are seeing is the expected behavior. ln -s behaves similarly to mv, cp, and ln. If the second argument refers to an existing directory, mv file dir, moves the file into dir, cp file dir copies the file into dir, and ln file dir (with or without -s) creates the link into dir.


It's not working as /test/opt already exists, remove it then run:

ln -s /test2/opt /test/opt
  • So I am confused here /test/opt is my source and /test2/opt is my target so when I will do ls -lrt test, I should see test -->/test2/opt.Is there any option where if I don't want to delete /test/opt before running script.
    – anant
    Sep 27 '19 at 16:02
  • /test/opt can't be both a symlink and a directory. Sep 27 '19 at 16:07
  • 1
    Ok now I got it, I will delete source i.e /test/opt and will run script ln -s /test2/opt /test/opt so it will create symlink inside /test which will point to /test2/opt. So when I will do ls - lrt /test it will show opt -->/test2/opt
    – anant
    Sep 27 '19 at 16:12
  • Hi Now it worked , I have one more folder where it is mount , e.g. /tmp is separate mount and I want to create symlink for this /tmp so when I will do ls -lrt / , it wshould show me tmp --> /test2/tmp
    – anant
    Sep 30 '19 at 6:08

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.