From the coreutils ln
manual:
Normally ln does not remove existing files. Use the –force (-f) option to remove them
unconditionally, the –interactive (-i) option to remove them conditionally,
and the –backup (-b) option to rename them.
$ mkdir output
I can understand this failing:
$ ln -sT /etc/passwd output
ln: failed to create symbolic link ‘output’: File exists
But why does adding -f
also fail:
$ ln -sfT /etc/passwd output
ln: ‘output’: cannot overwrite directory
Does -f
overwrite existing files which are only symbolic links, but not files of other types (directories, regular files, …)?
Can -T
be used when the last argument, (i.e. the target file argument), is an existing directory, with the intention to overwrite the directory into a link?
Best Answer
It can remove files, but directories are not "files".
This is seen in the source:
dest_lstat_ok
boolean which starts as false becomes true, the first if statement is called sinceremove_existing_files
is true due the--force
flag, which in turn allows the second if statement to be checked. It refuses to remove directories because it's expecting a file.If you don't set the
-T
which makes ln to not treat the directory as not a directory, ln would just created a symlink under the directory with the basename of the source.