ln -s 并覆盖一个物理目录

ln -s and overwriting a physical directory

所以我基本上有一个一直存在的目录A。我想用符号 link 替换此目录(这将在我的部署脚本中完成)。

我试过 ln -sf app/cache A 但它不起作用,它在 A 中创建它而不是覆盖它。

$ tree 
.
├── A
│   └── cache -> app/cache
└── app
    └── cache

3 directories, 1 file

是否可以仅使用 ln 还是我必须事先删除 A?

如果目标已经存在,则可能会要求 ln 实用程序通过添加 -f 选项来删除该目标。但是,POSIX 标准说这是通过调用 C 库例程 unlink() 完成的,关于 that 函数,标准说

The path argument shall not name a directory unless the process has appropriate privileges and the implementation supports using unlink() on directories.

我无法访问记录了 unlink() 以删除目录的系统,或者记录了 ln-f 标志以删除目录的系统。

因此,您的解决方案是

$ rm -rf /path/to/A

或者,哪个更安全,

$ mv -f /path/to/A /path/to/A.orig

在创建符号 link 之前。