Imagine I have a path that doesn't exist:
$ ls /foo/bar/baz/hello/world
ls: cannot access /foo/bar/baz/hello/world: No such file or directory
But let's say /foo/bar
does exist. Is there a quick way for me to determine that baz
is the breaking point in the path?
I'm using Bash.
Best Answer
Given a canonical pathname, such as yours, this will work:
That prints through the last fully existing/accessible component of
$pathname
, and puts each of those separately into the arg array. The first nonexistent component is not printed, but it is saved in$p
.You might approach it oppositely:
That will either return appropriately or will pare down
$path
as needed. It declines to attempt a change to/
, but if successful will print both your current working directory and the directory to which it changes to stdout. Your current$PWD
will be put in$OLDPWD
as well.