Creating a Symbolic Link with ln -s What Comes First?
One thing I've had to google more times than I'd like to admit is the path argument order for the
ln command. What comes first in the
ln -s command on linux or Mac? So I thought I'd write a little blog entry for future me to find.
Here's an example:
ln -s /real/path /linked/path
To answer my own question, you put the existing path first (source file), then the link path (target file) you want to create second.
A good way to try to remember this is that it is the same order as the
cpcommand if you were to copy a file from one path to a new path, you always put the existing file path first. The same goes for
ln, but unless you create symbolic links every day you'll probably forget this by the next time you need to create one.
It doesn't matter if you are running Mac, Linux, Ubuntu, RedHat the syntax will always be ln -s source target
ln: No such file or directory Error
Recently when attempting to create a symbolic link from one folder to a new linked folder, I received this error:
ln: /linked/path/: No such file or directory
That seamed like a rather odd error since that linked path was the link I was attempting to create, of course it doesn't exist. This led me to immediately think I mixed up the order of the target and source paths, but I did have them in the correct order. The cause of that error for me was simply that I put a trailing slash on the linked path. Removing that trailing slash fixed the problem for me.
Like this? Follow me ↯Tweet Follow @pfreitag
Creating a Symbolic Link with ln -s What Comes First? was first published on July 13, 2020.
If you like reading about linux, mac, bash, ln, ubuntu, or redhat then you might also like:
- Recursively Counting files by Extension on Mac or Linux
- The 15 Essential UNIX commands
- Linux and OS X Shell Commands
- Bash Script to log file modifications with osquery
- Bash Loop To Wait for Server to Start
- Searching for files by file name on Mac or Linux
- Counting IP Addresses in a Log File
- Passing Environment Variables to Sudo Command