344

What command is used to create a symbolic link/soft link?

533
┌── ln(1) link, ln -- make links
│   ┌── Create a symbolic link.
│   │                         ┌── the optional path to the intended symlink
│   │                         │   if omitted, symlink is in . named as destination
│   │                         │   can use . or ~ or other relative paths
│   │                   ┌─────┴────────┐
ln -s /path/to/original /path/to/symlink
      └───────┬───────┘
              └── the path to the original file/folder
                  can use . or ~ or other relative paths
$ echo content > original
$ ln -s original symlink
$ ls -la original symlink
-rw-r--r--  1 grgarside  staff    8 28 Jan 18:44 original
lrwxr-xr-x  1 grgarside  staff    8 28 Jan 18:44 symlink -> original
$ cat symlink
content

For more information about ln(1) see the man page.

The path to the symlink is optional; if omitted, ln defaults to making a link with the same name as the destination, in the current directory:

$ cd ~/Documents
$ ln -s ../Pictures
$ ls -l Pictures
lrwxr-xr-x  1 user  staff  11 Feb  1 17:05 Pictures -> ../Pictures

To create a symlink to replace a system directory (e.g. if you want to have /Users pointing to another disk drive), you need to disable System Integrity Protection. You can re-enable it after the symlink is set up.

  • 62
    If only man pages were as clear as your answer! – Adrian Lynch Jan 27 '16 at 7:00
  • permission denied :S can you give me a hint? trying to create symlink for Users so I can use it on other hard disk and ssd has just symlink. thanks – Ewoks Mar 13 '16 at 21:36
  • 2
    @patrix I've edited my answer to clarify; feel free to edit further. – grg Mar 14 '16 at 7:40
  • 3
    Fun fact: original doesn't need to actually exist. The command ln -s "This directory is no longer in use" README would be perfectly legitimate, and then anybody executing ls -l would see the message. – Edward Falk May 11 '16 at 14:33
  • 1
    For future reference: You don't need to move /Users on macOS to save space. You can change users' home directory paths individually in the System Preferences under Users & Groups by right-clicking the user list entries. – Peter W. May 12 '17 at 22:09
71

The command is called ln. If used with the option -s it will create a symbolic link in the current directory:

ln -s /any/file/on/the/disk linked-file
  • 1
    Does the linked-file have to exist first? I get a file not found error on the target. – AlxVallejo Oct 26 '16 at 14:26
  • 2
    The file not (you get an error message if it does), but all directories in any path. If you are stuck, feel free to ask a new question using the Ask Question button at the top right. Include a link to this question to provide context. – nohillside Oct 26 '16 at 17:06
  • Symbolic link should not exist before you run this command. But a valid path is required as far as I know. If the output file's folder not exists before you run this command, you will get No such file or directoryerror as well. – Deniz Kaplan Dec 19 '17 at 11:23
34

I know this question is explicitly asking about the Terminal, but if you're in GUI Land and don't want to enter Terminal Land, you can use SymbolicLinker. This puts a "Make Symbolic Link" option in your Services menu in Finder.

A context menu for a folder, showing a "Services" submenu, with "Make Symbolic Link" hilighted

A context menu for a symbolic link, with "Make Symbolic Link" hilighted

  • 3
    I'd love to know why this was downvoted so I can make higher-quality answers from now on :) – Ben Leggiero Apr 27 '16 at 22:43
  • 10
    probably because the question was "How can I create a symbolic link in Terminal?" But I am not downvoting you :) – vedrano Apr 29 '16 at 14:55
  • 1
    @EdwardFalk I think in El Capitan, you can hold Command+Option while dragging a file... will update the answer later – Ben Leggiero Nov 9 '16 at 14:27
  • 1
    @BenLeggiero That makes an alias. – Andy Stewart Dec 29 '16 at 15:11
  • 2
    @BenLeggerio, The difference is explained here: apple.stackexchange.com/questions/2991/… – MiB Feb 25 '17 at 3:25
28

It's just ln -s <source> <destination>.

5

ln -s /some/dir/ ~/Desktop/dir

You can also create a symlink for directory using the same command

ln -s "$(pwd)" ~/Desktop/dir

To create symlink to current directory you are in.

  • 2
    A simple . (dot) will give the pwd. No fancy arguments needed :) – mylogon Apr 20 '18 at 19:37
  • @mylogon hahaha i like to overthink sometimes. simplified my answer! – Gerald Jun 17 '18 at 8:23
  • @mylogon i just realised that . doesnt work on macOS. didnt try on linux yet. using ./ resulted in this foo -> ./foo which points to itself. – Gerald Jul 12 '18 at 6:10
  • What is the full command you typed out? – mylogon Jul 12 '18 at 6:20

You must log in to answer this question.

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