Reputation: 1589
Let me show my problem through screen shots.
This is my first terminal. I changed the link from another terminal say terminal2. My problem is highlighted in this screen shots. Here the ls
returns content of test1 folder after I changed the link into test2.
Upvotes: 0
Views: 4323
Reputation: 126213
The thing that is confusing you is that the pwd
builtin doesn't actually tell you the current working directory -- it tells you the path that you used to get to the current working directory.
If you want the actual current working directory, you need the -P
flag:
$ pwd -P
which will give you something like /home/darkknight/test1
.
Upvotes: 2
Reputation: 37437
What you describe is the expected behavior.
$ mkdir test1
$ mkdir test2
$ touch test1/t1
$ touch test2/t2
$ ln -s test1 test
$ cd test
$ ls
t1
$ rm ../test
$ ln -s test2 ../test
$ readlink ../test
test2
$ ls
t1
$
Simply imagine that when you cd test
you actually enter test1
. You can then change the symbolic link test
to whatever you want -and even delete it!- you are still in test1
.
$ pwd
test
$ readlink `pwd`
test2
$
For sure, readlink
returns test2
, as it goes and read the current test
link that has been changed to test2
. However, at the time you cd
into test
, the link was to test1
. And you are still in the test1
directory.
Of course, if now you change to the test directory, you'll be in test2
.
$ cd ../test
$ ls
t2
$
One last thing, to clarify. Directory test
does not exist. You cannot enter nor be in this directory. Whenever you cd test
, you enter the directory the test
symbolic link currently points to (here test1
).
After you've entered the test1
directory, you can change the test
symbolic link to whatever you want, you'll still be in test1
.
Upvotes: 2
Reputation: 2601
That second call to readlink
doesn't do what you think
darkknight@localhost:~/test$ readlink \`pwd`/home/darkknight/test2`
The description for readlink
in the man page is
Display value of a symbolic link on standard output
readlink
is a read-only operation. You will also find the value of $?
to be non-zero after this, meaning this has failed because test2
is not a symlink.
Replace it with another call to ln -s -f
(where -f stands for force) ...
darkknight@localhost:~$ ln -s -f test2 test
Not all distros honor -f
so you may have to remove the symlink first:
darkknight@localhost:~$ rm test; ln -s test1 test
Upvotes: 2
Reputation: 1290
Now I changing the link to test2 from another terminal : the command should be
rm -fr test;ln -s test2 test; cd test
Upvotes: 1