How to create a symlink to root

13,504

You're missing the name of the link, it should be:

cd ~
ln -s / root

Which then would create a symlink called root in your home directory. So the correct usage is:

ln -s <target> <link-name>

The error message you see is, that ./ always exists and a link can not be created with this name, best is to use the ln command2 with both parameters to prevent wrong linkage.

From man ln:

SYNOPSIS

(1st form) ln [OPTION]... [-T] TARGET LINK_NAME  
(2nd form) ln [OPTION]... TARGET  
(3rd form) ln [OPTION]... TARGET... DIRECTORY  
(4th form) ln [OPTION]... -t DIRECTORY TARGET...  

DESCRIPTION

In the 1st form, create a link to TARGET with the name LINK_NAME. In the 2nd form, create a link to TARGET in the current directory. In the 3rd and 4th forms, create links to each TARGET in DIRECTORY. Create hard links by default, symbolic links with --symbolic. By default, each destination (name of new link) should not already exist4. When creating hard links, each TARGET must exist. Symbolic links can hold arbitrary text; if later resolved, a relative link is interpreted in relation to its parent directory.

Mandatory arguments to long options are mandatory for short options too.

OPTIONS

The final parameter, <link-name>, defaults to the last part of the target. So when the target is /path/dir the link name will default to dir if not specified.1 And if you for example create ~/etc with mkdir ~/etc and then run ln -s /etc in ~ it can not create the link because the name/directory already exists.3

And you can see the link created in your home directory (here as example, of course you're free to name it whatever you like):

$ ls -l ~/root
lrwxrwxrwx 1 videonauth videonauth 1 Dez 14 00:28 root -> /

1 Thanks to @thomasrutter for pointing that out.
2 See also man link and man symlink
3 Thanks to @steeldriver for providing an example in comments.
4 Emphasised part to make text point out since it is relevant to the question.

Share:
13,504

Related videos on Youtube

Ankur S
Author by

Ankur S

Updated on September 18, 2022

Comments

  • Ankur S
    Ankur S over 1 year

    I want to create a symlink to the root (/) folder in my home directory. However if I try this, I get

    ~$ ln -s /
    ln: failed to create symbolic link './': File exists
    

    I can do this using Nautilus Ctrl+D, of course, so I know it's possible.

    How to do so using the terminal?

    • Terrance
      Terrance over 6 years
      While this is a good question, I want to know the reason why you would want do to this? Is it too hard to type in cd /? Or do you like to create a link that would keep going through a loop over and over and over again? example: cd root/home/terrance/root/home/terrance/root/home/terrance/roo‌​t . . . etc.
    • Ankur S
      Ankur S over 6 years
      not really .you could do this without needing to link to root . actually, i was just curious why you can't do it
    • Terrance
      Terrance over 6 years
      Fair enough. Thank you for answering my question. =)
  • Ankur S
    Ankur S over 6 years
    my only remaining doubt is why should a command like ln -s /etc work but not this one? Thanks for the answer,
  • Videonauth
    Videonauth over 6 years
    It is created because it has a directory name, / can be root or it can be a path component so the program can not know what now is meant. /etc is just an explicit distinguishable path.
  • steeldriver
    steeldriver over 6 years
    @AnkurSonawane because ./ will always exist - ./etc not so likely. Try mkdir etc first, and then ln -s /etc - you will also get File exists
  • thomasrutter
    thomasrutter over 6 years
    The final parameter, link-name, defaults to the last part of the target if omitted. So when the target is /some/dir the link name will default to dir if not specified. When the target is just /, however, this isn't going to work.
  • Ankur S
    Ankur S over 6 years
    I don't feel that this extremely significant but @steeldriver 's explanation sounds more reasonable given the File Exists output. Not that it matters anyway as this is the degenerate case