Skip to main content
109 votes

Tips for remembering the order of parameters for ln?

I go by "ln is like cp. The 'source' needs to come first."
Hermann's user avatar
  • 6,352
48 votes
Accepted

Tips for remembering the order of parameters for ln?

I use the following: ln has a one-argument form (2nd form listed in the manpage) in which only the target is required (because how could ln work at all without knowing the target) and ln creates the ...
gary's user avatar
  • 734
30 votes
Accepted

How to force creation of a symbolic link?

In GNU's ln, there is ln -n, which would allow re-pointing a symlink: $ mkdir dir1 dir2 $ ln -s dir1 sym # dir1/ # dir2/ # sym -> dir1/ $ ln -nsf dir2 sym # dir1/ # dir2/ # sym -> dir2/ BSD ...
Kenneth B. Jensen's user avatar
23 votes

Too many levels of symbolic links

Symlinks are relative to the parent directory of the link, not of the current directory of the ln process. When you do: cd /top/dir ln -s test/src test/firefox (where test/firefox is a directory), ...
Stéphane Chazelas's user avatar
23 votes
Accepted

Why I can't create a hard link from device file in other than /dev directory?

But I could only create a hard link in the /dev directory and it was not possible in other directories. As shown by the error message, it is not possible to create a hard link across different ...
dr_'s user avatar
  • 30.4k
20 votes

Create a symbolic link relative to the current directory

I was having the same problem. Google led to this answer but the simplest solution is not documented here: ln -sT -T does the trick man ln: -T, --no-target-directory treat LINK_NAME as a ...
tiegs's user avatar
  • 209
18 votes
Accepted

How to create a folder symlink that has a different name?

You already have a directory at ~/.pm2/logs. Since that directory exists, the symbolic link is put inside it. Would you want that ~/.pm2/logs is a symbolic link rather than a directory, then you ...
Kusalananda's user avatar
  • 341k
17 votes

Create a symbolic link relative to the current directory

ln's behavior with relative paths is unintuitive. To restore sanity, use the -r flag. cd /run/media/name/exhdd ln -sr Data/ ~/Data Explanation: -r, --relative create symbolic links ...
Aleksandr Dubinsky's user avatar
16 votes
Accepted

What is the difference between the link and ln commands?

link used solely for hard links, calls the link() system function and doesn't perform error checking when attempting to create the link ln has error checking and can create hard and soft links
aavloni's user avatar
  • 309
16 votes
Accepted

What is `ln --no-dereference` supposed to do?

Without -n, both your ln commands would create links inside dir2: if LINK_NAME exists and is a directory or a symlink to a directory, the link is created inside the directory (if possible). That’s ...
Stephen Kitt's user avatar
14 votes

Too many levels of symbolic links

Use the absolute path instead of the relative path, then it will work. Eg: ln -s /home/user/test/src /home/user/test/firefox
JISHNU T U's user avatar
14 votes

Tips for remembering the order of parameters for ln?

Most Unices document the ln command as ln source target (I'm omitting options etc. here) Examples: The POSIX standard ln [-fs] [-L|-P] source_file target_file OpenBSD: ln [-fhLnPs] source [...
Kusalananda's user avatar
  • 341k
12 votes

Why I can't create a hard link from device file in other than /dev directory?

A hard link cannot be used to achieve what you want, because hard links do not work between file systems. However, you can achieve what you want with the mknod command. Run ls -l /dev/devicefile. ...
wizzwizz4's user avatar
  • 527
12 votes

Tips for remembering the order of parameters for ln?

I recently heard a great way to remember this particular thing: a rhyme Something old, something new, something borrowed, something blue, and a sixpence in her shoe. The first verse is what the ...
loa_in_'s user avatar
  • 408
11 votes
Accepted

Why can't I create a `hardlink` to a file from a "mount --bind" directory on the same filesystem?

There's a disappointing lack of comments in the code. It's as if no-one ever thought it useful, since the time bind mounts were implemented in v2.4. Surely all you'd need to do is substitute .mnt-&...
sourcejedi's user avatar
  • 51.4k
11 votes
Accepted

Is there a way to create a symlink to a non-existent target?

When you run ln -s nonexistenttarget link ln doesn’t check whether nonexistenttarget exists, it creates the link, unless link already exists. -f works around the last part by deleting link if ...
Stephen Kitt's user avatar
10 votes

Does `ln -sf` overwrite existing files which are only symbolic links

In UNIX, directories are special (I feel myself channeling The Church Lady from SNL). Directories contain other files, so deleting them requires a different operation. Even when a directory is empty ...
MAP's user avatar
  • 595
10 votes
Accepted

ln: create symlink using another symlink

-L only works with hard links; as specified in POSIX: If the -s option is specified, the -L and -P options shall be silently ignored. If you have readlink you can use that: ln -s -- "$(readlink ...
Stephen Kitt's user avatar
9 votes

What is the difference between the link and ln commands?

The link FILENAME LINKNAME command creates a hard link to a file. According to the manual page, it is the same as: ln --directory --no-target-directory FILENAME LINKNAME See man link or info ...
NZD's user avatar
  • 1,452
9 votes

Does `ln -sf` overwrite existing files which are only symbolic links

It can remove files, but directories are not "files". ➜ lab touch file ➜ lab mkdir dir ➜ lab ln -sfT /home file ➜ lab ln -sfT /home dir ln: dir: cannot overwrite directory This is seen ...
Braiam's user avatar
  • 36.4k
9 votes
Accepted

Why change the current directory to the parent directory of a relative symlink before creating it

There are two things to remember: ln -s (without -r) stores the target name literally as you pass it to it if you pass a relative target, it resolves relatively to the link name, not your current ...
Petr Skocik's user avatar
  • 29.2k
9 votes
Accepted

Size of symlink

Sort of, but note that the size of a file is not well-defined at that level of precision. A symbolic link involves four parts: The name of the link, which is stored in the directory where it is an ...
Gilles 'SO- stop being evil''s user avatar
8 votes

Why change the current directory to the parent directory of a relative symlink before creating it

If you did mkdir /tmp/foo ln -s ../../etc/passwd /tmp/foo Then access /tmp/foo/passwd would convert to /tmp/foo/../../etc/passwd - ie /etc/passwd If you did mkdir /var/tmp/foo ln -s ../../etc/...
Stephen Harris's user avatar
8 votes

Understanding the usage of ln

It means that if you to ln /path/to/files/* /path/to/some/directory/ or ln -t /path/to/some/directory/ /path/to/files/*, a link to each of the files matching /path/to/files/* will be created in /path/...
DopeGhoti's user avatar
  • 77.4k
8 votes

Rm hardlink without removing file/directory itself

$ echo 'hello' >name $ ln name othername $ ls -l total 4 -rw-r--r-- 2 kk wheel 6 Jun 25 09:45 name -rw-r--r-- 2 kk wheel 6 Jun 25 09:45 othername $ cat othername hello $ rm name $ ls -l ...
Kusalananda's user avatar
  • 341k
8 votes
Accepted

What is the meaning of 'ln -sT' in Linux?

ln’s synopsis is as follows: ln [OPTION]... [-T] TARGET LINK_NAME (1st form) ln [OPTION]... TARGET (2nd form) ln [OPTION]... TARGET... DIRECTORY (3rd form) ln [OPTION]... -t ...
Stephen Kitt's user avatar
8 votes

How to create a folder symlink that has a different name?

As other answers say, there is already a directory there. To avoid this and instead get an error-message, use the -T option, unfortunately I don't think this is Posix (it is GNU). From the Gnu ln ...
ctrl-alt-delor's user avatar
7 votes

Making a link using ln -s

A shell will typically break a ln -s /Backup files/ /link 1 command line into these words: ln -s /Backup files/ /link 1 From the first word, it will derive the command to execute (something like /...
Stéphane Chazelas's user avatar
7 votes

How to create a link (ln -s) with an absolute path?

Okay, I found one way to make it work easily: ln -s `realpath ../dir2/file` link #not perfect It's not perfect - $( ... ) is preferred to `...` for command substitution, and there's a ...
Peter Kay's user avatar
  • 131
7 votes

Tips for remembering the order of parameters for ln?

In case this helps anyone: I've gotten used to thinking of it as "ln what where", which helps me remember that the first argument ("what") is the existing file, the second ("where") is the place to ...
David Z's user avatar
  • 1,017

Only top scored, non community-wiki answers of a minimum length are eligible