![FreeBSD 上 ln 命令中的 -F 选项有什么用?](https://linux22.com/image/184796/FreeBSD%20%E4%B8%8A%20ln%20%E5%91%BD%E4%BB%A4%E4%B8%AD%E7%9A%84%20-F%20%E9%80%89%E9%A1%B9%E6%9C%89%E4%BB%80%E4%B9%88%E7%94%A8%EF%BC%9F.png)
在https://www.freebsd.org/cgi/man.cgi?ln, 它说
-F If the target file already exists and is a directory, then remove
it so that the link may occur. The -F option should be used with
either -f or -i options. If neither -f nor -i is specified, -f is
implied. The -F option is a no-op unless -s is specified.
那么如何使用这个选项呢?如果将文件链接到目录,则会在该目录中创建同名链接。
答案1
正如它所说,如果目标文件已经存在并且是一个目录,那么ln
将删除它以便可以发生链接。
mkdir dir1
echo hello >file
ln -s file dir1 # Creates a broken symlink at dir1/file pointing to itself
cat dir1/file
cat: dir1/file: Too many levels of symbolic links
mkdir dir2
echo hello >file
ln -Fs file dir2 # Removes dir1 and creates a symlink of dir1 pointing to file
cat dir2/file
cat: dir2/file: Not a directory
cat dir2
hello