88

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?

116

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

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.

  • 13
    I tried this but the resulting symlink was a file and not a directory – Alex Sep 27 '17 at 23:04
  • 1
    @Alex a softlink appears like a file but the result is that it behaves like the folder it was pointing at. – Chanoch May 17 '18 at 14:06
  • 6
    Also note that the source directory must not be relative path must be absolute so ln -s ./ /var/www/html will not work – Diego Andrés Díaz Espinoza Jun 14 '18 at 13:44
0

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

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

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.