2

When I run the following command, where symlinked-dir is just that - a symlink to another directory, cat complains that there is no such file. There is, indeed, no such file - it's created in the parent directory of the real path instead, but it contains the the path through the symlink outputted by pwd, so at least cd and pwd know where they are, link-wise. I want the redirect to know it too.

cd symlinked-dir && echo $(pwd) >> ../tmp && cd .. && cat tmp

So, what modifications should I make to get the above command work?

Edit: the shell is Bash on either Debian or Gentoo (both work the same way).

6

2 Answers 2

2

Just:

(cd symlinked-dir && pwd) >> tmp && cat tmp

Where we run the cd in a subshell so it only affect that subshell and perform the redirection outside the subshell (and before the subshell is even created).

Replace pwd with pwd -P if you want the canonical (symlink-free) absolute path of the symlinked-dir. Though if you have a realpath command, then you can do it simply with realpath symlinked-dir > tmp.

0

As pointed out, reading .. of the directory will not help us, so we must solve this at the symbolic path level. One of the ways to do so is listed below:

cd symlinked-dir && echo $(pwd) >> $(realpath -s $(pwd)/../tmp) && cd .. && cat tmp

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .