ln -s and overwriting a physical directory
Asked Answered
E

1

8

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?

Endotoxin answered 29/6, 2016 at 8:41 Comment(2)
what are the values you provided for MY_TARGET and path_to_A . For example to create softlink for /webroot/home/httpd/test.com/index.php as /home/vivek/index.php, enter the following command: ln -s /webroot/home/httpd/test.com/index.php /home/vivek/index.php ls -lPedicure
Thx for your answer, updated my post @PedicureEndotoxin
G
9

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.

Gape answered 29/6, 2016 at 9:45 Comment(1)
I'm a little surprised this is the only wayMilicent

© 2022 - 2024 — McMap. All rights reserved.