Scenario: I'm writing a shell script that checks to see if a directory exists; if it does, and it's a symlink, it renames the directory with .bak
appended to the directory name. I noticed, however, that if I'm moving a symlinked directory (directory A) to another symlinked directory (B), it causes A to be moved inside B, rather than overwriting B.
To illustrate:
$ mkdir foo
$ mkdir bar
$ ln -s "$PWD/foo" ~/.baz
$ ls -al ~/.baz
lrwxr-xr-x 1 ysim staff 15 Sep 11 21:45 /Users/ysim/.baz -> /Users/ysim/foo
$ mv ~/.baz ~/.bam
$ ls -al ~/.bam
lrwxr-xr-x 1 ysim staff 15 Sep 11 21:45 /Users/ysim/.bam -> /Users/ysim/foo
$ ls -al ~/.baz
ls: /Users/ysim/.baz: No such file or directory
$ ln -s "$PWD/bar" ~/.baz
$ ls -al ~/.baz
lrwxr-xr-x 1 ysim staff 15 Sep 11 21:47 /Users/ysim/.baz -> /Users/ysim/bar
$ ls -al ~/.bam
lrwxr-xr-x 1 ysim staff 15 Sep 11 21:45 /Users/ysim/.bam -> /Users/ysim/foo
Now, when I rename/move ~/.baz
to ~/.bam
:
$ mv ~/.baz ~/.bam
The target of ~/.baz
(/Users/ysim/bar
) doesn't get moved to the target of ~/.bam
, as expected:
$ ls -al ~/.bam
lrwxr-xr-x 1 ysim staff 15 Sep 11 21:45 /Users/ysim/.bam -> /Users/ysim/foo
Instead, the entire ~/.baz
symlink has been moved inside the ~/.bam
directory:
$ ls -al ~/.bam/
total 8
drwxr-xr-x 3 ysim staff 102 Sep 11 21:47 .
drwxr-xr-x+ 96 ysim staff 3264 Sep 11 21:51 ..
lrwxr-xr-x 1 ysim staff 15 Sep 11 21:47 .baz -> /Users/ysim/bar
Why is that? And how do I go about doing what I set out to do, which is to write the target of ~/.baz
to the target of ~/.bam
, so that ~/.bam
has the destination /Users/ysim/bar
?