127

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?

andrew.46
  • 39,359
user991710
  • 1,383

2 Answers2

158

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.

heemayl
  • 93,925
9

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

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