Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ln -s and overwriting a physical directory

So I basically have a directory A that always exists. I'd like to replace this directory with a symbolik link (this will be done in my deployment script).

I've tried ln -sf app/cache A but it does not work, it creates it inside A instead of overwriting it.

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

3 directories, 1 file

Is it possible with only ln or do I have to remove A beforehand?

like image 816
smarber Avatar asked Jun 29 '16 08:06

smarber


1 Answers

The ln utility may be asked to remove the destination if it already exists by adding the -f option. However, the POSIX standard says that this is done with a call to the C library routine unlink(), and about that function, the standard says

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

I have not access to a system where unlink() is documented to remove directories, or where the -f flag to ln is documented to remove directories.

Your solution is therefore to either

$ rm -rf /path/to/A

or, which would be safer,

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

before creating the symbolic link.

like image 136
Kusalananda Avatar answered Nov 01 '22 04:11

Kusalananda