122

I have two folders, one of which is my webserver root. I want to link it to my project folder so that I can keep things up-to-date with Git.

If I try to do this:

ln -s /home/user/project  /var/www/html

The system creates a folder called project within the html folder, and links that. Is there any way to link it so that the directory 'html' points to the contents of project? Do they have to be the same name?

Note: I also need to link several hidden files. Can I do this using ln?

2 Answers 2

153

From man ln:

By default, each destination (name of new link) should not already exist.

As you already have a directory named html, the link will be created inside /var/www/html having the name of the target i.e. project.

If you want to have a symlink /var/www/html pointing to /home/user/project then you should not have the directory html present beforehand. So, you should only have /var/www and then running the following will create the desired symlink (don't do this unless you are sure):

ln -s /home/user/project /var/www/html

Note: if you use a trailing slash (like in ln -s /home/user/project /var/www/html/) you will get an error: ln: target '/var/www/html' is not a directory: No such file or directory.

Here is an example:

$ pwd
/home/user/test/askubuntu

$ ls -l
total 4
drwxrwxr-x 2 user user 4096 Mar 25 00:16 foo

$ ln -s /home/user/test/bar /home/user/test/askubuntu/foo

$ ls -l
total 4
drwxrwxr-x 2 user user 4096 Mar 25 00:17 foo

$ cd foo/
$ ls -l
total 0
lrwxrwxrwx 1 user user 25 Mar 25 00:17 bar -> /home/user/test/bar

$ cd ..
$ rm -r foo/

$ ls -l
total 0

$ ln -s /home/user/test/bar /home/user/test/askubuntu/foo

$ ls -l
total 0
lrwxrwxrwx 1 user user 25 Mar 25 00:18 foo -> /home/user/test/bar

And yes, you can create symlinks of hidden files.

5
  • 17
    I tried this but the resulting symlink was a file and not a directory
    – Alex
    Sep 27, 2017 at 23:04
  • 2
    @Alex a softlink appears like a file but the result is that it behaves like the folder it was pointing at.
    – Chanoch
    May 17, 2018 at 14:06
  • 10
    Also note that the source directory must not be relative path must be absolute so ln -s ./ /var/www/html will not work Jun 14, 2018 at 13:44
  • ln -s $PWD/path/to/real/dir ./relative/path/to/symlink the second arg can be relative (at least on darwin/BSD)
    – yurisich
    Jul 22, 2020 at 15:11
  • @DiegoAndrésDíazEspinoza THAT IS IMPORTANT, was wondering why it wasn't working! Sep 1, 2022 at 10:36
8

Make sure html directory is not created under /var/www/.

The command then is: ln -s -T /home/user/project /var/www/html

You must log in to answer this question.

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