简体   繁体   中英

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

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!

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

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.

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

For instance:

cd /Users/me
ln -s somefolder somefolderNewName

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.

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, eg:

$ 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM