coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: ../.. resolution of ls


From: Dominique Martinet
Subject: Re: ../.. resolution of ls
Date: Mon, 8 Jun 2020 09:57:58 +0200

Peng Yu wrote on Sun, Jun 07, 2020 at 12:23:06PM -0500:
> It seems that ../../ can not be resolved symbolically by ls. See the
> following example. I'd like `ls ..` to print both a and b.
> Unfortunately, it only print b because it thinks it is in /tmp/i/a/b
> instead of /tmp/i/b. Is there a way to use symbolic pwd instead of abs
> pwd? Thanks.
> 
> /tmp/i$ tree
> .
> ├── a
> │   └── b
> └── b -> a/b/
> 
> /tmp/i$ cd b
> /tmp/i/b$ ls -H ../
> b
> /tmp/i/b$ ls ../
> b

An external command (external to the shell) can't do that.
It only knows what the current directory actually is, and opening
.. really is /tmp/i/a/ so will only list b - it has no way of knowing
HOW you got into b.


The only thing that actually knows the "symbolic path" as you call it is
your shell, so you can actually work around this a bit for example this
simple function does what you'd like (but is definitely lacking so
please do not use it as it is, e.g. it would not give the expected
return on an absolute path)

$ cd /tmp/i/b
$ pwdls() {
    local arg="$1";
    local dir="$PWD";
    while [[ "$arg" = "../"* ]]; do
       arg=${arg#../};
       dir=${dir%/*};
    done;
    ls "/$dir/$arg"
}
$ pwdls ../
a/ b@


I assume there is something similar in how shell completion works path
completions, so that is why sometimes you can see something through
completion for ../whatever but the command you run never finds it...

Ah, here it is, for bash you can use compgen, which can do this because it
is a shell builtin:
$ compgen -f ../
../a
../b


-- 
Dominique



reply via email to

[Prev in Thread] Current Thread [Next in Thread]