12

I want a create a symbolic link to a folder. The follow command will create a file with the link name but I'm trying to link to the source folder. What am I doing wrong?

ln -s /Users/me/somefolder somefolder

This creates a file "somefolder" in my current directory. How do I create a symbolic link to the folder and it's contents?

Thanks!

5
  • 1
    What do you see when you do $ ls -l somefolder
    – Fred
    Jul 27, 2013 at 4:03
  • Yes, it will create a file: one that is the symlink to your folder. It will be treated as a folder whenever it's accessed. Don't worry.
    – user529758
    Jul 27, 2013 at 4:10
  • Thank you both. That seems right.. but if I do a 'cd somefolder' I get 'no such file or folder someFolder"
    – Nick
    Jul 27, 2013 at 4:12
  • lowercase somefolder?
    – sgun
    Jul 27, 2013 at 4:13
  • just a typo.. I cannot cd into the symlink
    – Nick
    Jul 27, 2013 at 4:19

5 Answers 5

17

You need to use absolute path names to create the links. For example, I'm now at

$ pwd
/home/alex/my_folder

And I'm creating a symbolic link to the folder "directoryA" in a sub-directory under my pwd (present working directory):

 $ ln -s $PWD/directoryA $PWD/temp/link_to_directoryA

In this case variable $PWD holds absolute path to my working directory. You can surely use your absolute path without any variables like this:

 $ ln -s /home/alex/my_folder/directoryA /home/alex/my_folder/temp/link_to_directoryA
3
  • this doesn't account for going backwards using ../ when creating symlink. @Ines de Santiago 's answer below works well in that case
    – itaintme
    Jun 9, 2022 at 6:02
  • there's no backwards for an absolute path. What you mentioned is not an absolute path
    – molexi
    Jun 9, 2022 at 21:14
  • I agree. The extra work here is to find the absolute path for both the directories when they could be in different places. With the other answer it takes the step of finding the absolute path away. Both answers are correct. One is easier for relative path
    – itaintme
    Jun 10, 2022 at 5:48
2

You need to be inside the same directory where you create the symbolic link

For instance:

cd /Users/me
ln -s somefolder somefolderNewName

1

Not creating a directory is an expected behavior.

When you do

  ls -ali

It should show something beginning with;

  lrwxrwxrwx

In which "l" represents symlink and allows you to traverse using cd.

NOTICE: ln command will not complain when you provide an invalid source path. And this will result with an error message when you try cd in to that.

9
  • I do. But I cannot cd into the dir. "somefolder: no file or folder found"
    – Nick
    Jul 27, 2013 at 4:14
  • 3
    @Nick, try writing absolute path. Starting from root directory.
    – sgun
    Jul 27, 2013 at 4:20
  • You mean for the target?
    – Nick
    Jul 27, 2013 at 4:23
  • I guess I got the problem. When you symlink to a non-existent path or target file it doesnt complain. And it shows "no directory" when you want to cd in.
    – sgun
    Jul 27, 2013 at 4:25
  • 1
    This is an old thread but just in case it saves anyone else some hassle, it's worth noting that the path to the folder you're linking can't contain a symlink itself. Totally obvious in retrospect but I was so used to typing cd www/folder/... that I'd totally forgotten www is just a symlink to public_html. So anytime I tried to create a shortcut to a folder in the website I was using www and getting the same error as the OP.
    – wunderdojo
    Feb 27, 2014 at 20:30
1

Late for the party.. This is what worked for me..

if you want to create a symbolic link from sourceFolder to destinationFolder you should be inside the parent of the destinationFolder "parentOfDestinationFolder" while doing so.

0

I think you have what you want, you just don't know it. A link has an entry in the directory, just like data files or directories do. You can see this most clearly if you run ls -l in the directory where you're creating the link.

You can use your link as if it were a directory, e.g.:

$ cd somefolder

You might also like to know that if you change directory this way, the parent of somefolder will be the directory that contains the link. If you don't want that, use:

$ cd -P somefolder
2
  • 1
    This weird. I can ls -i and see the link. But I cannot cd into it.
    – Nick
    Jul 27, 2013 at 4:22
  • Perhaps you're not being consistent in your capitalization? Maybe try it again with a different name for sanity sake? Jul 27, 2013 at 4:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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