FreeBSD 上 ln 命令中的 -F 选项有什么用?

FreeBSD 上 ln 命令中的 -F 选项有什么用?

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

相关内容