Why is my symbolic link creating a file and not a folder?
Asked Answered
C

5

14

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!

Cavill answered 27/7, 2013 at 3:59 Comment(5)
What do you see when you do $ ls -l somefolderMetaphrase
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.Longevous
Thank you both. That seems right.. but if I do a 'cd somefolder' I get 'no such file or folder someFolder"Cavill
lowercase somefolder?Muggins
just a typo.. I cannot cd into the symlinkCavill
M
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.

Muggins answered 27/7, 2013 at 4:12 Comment(9)
I do. But I cannot cd into the dir. "somefolder: no file or folder found"Cavill
@Nick, try writing absolute path. Starting from root directory.Muggins
You mean for the target?Cavill
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.Muggins
Not sure I follow. Can you give me a small example? I'll double check the source pathCavill
Looks like you do not have "/Users/me/somefolder". Try writing by pressing on Tab button for autocompletion. This way you will enter valid directory.Muggins
Somehow I managed to mangle the source path (as you alluded). I guess I expected the link creation to fail that case. Thanks a lot for your time!Cavill
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.Mighty
@Muggins That was my problem! On ubuntu 18 not using root either.Conscription
M
20

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
Mosenthal answered 28/6, 2018 at 19:3 Comment(3)
this doesn't account for going backwards using ../ when creating symlink. @Ines de Santiago 's answer below works well in that caseBoffin
there's no backwards for an absolute path. What you mentioned is not an absolute pathMosenthal
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 pathBoffin
M
2

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

For instance:

cd /Users/me
ln -s somefolder somefolderNewName

Muttonhead answered 8/5, 2019 at 10:1 Comment(0)
M
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.

Muggins answered 27/7, 2013 at 4:12 Comment(9)
I do. But I cannot cd into the dir. "somefolder: no file or folder found"Cavill
@Nick, try writing absolute path. Starting from root directory.Muggins
You mean for the target?Cavill
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.Muggins
Not sure I follow. Can you give me a small example? I'll double check the source pathCavill
Looks like you do not have "/Users/me/somefolder". Try writing by pressing on Tab button for autocompletion. This way you will enter valid directory.Muggins
Somehow I managed to mangle the source path (as you alluded). I guess I expected the link creation to fail that case. Thanks a lot for your time!Cavill
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.Mighty
@Muggins That was my problem! On ubuntu 18 not using root either.Conscription
Z
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.

Zulema answered 23/5, 2020 at 4:26 Comment(0)
T
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
Tippler answered 27/7, 2013 at 4:13 Comment(2)
This weird. I can ls -i and see the link. But I cannot cd into it.Cavill
Perhaps you're not being consistent in your capitalization? Maybe try it again with a different name for sanity sake?Tippler

© 2022 - 2024 — McMap. All rights reserved.