2

How do I test whether a pathname exists?

Previously I was using:

realpath -e /some/path 

Which works except if /some/path is a broken symlink, in which case it reports that it does not exist.

[ -e /some/path ] 

Produces a similar outcome to realpath

1 Answer 1

4

To check whether /some/path exists, either as something that can be completely resolved, or as a broken symbolic link:

if [ -e /some/path ] || [ -h /some/path ]; then echo '/some/path exists, possibly as a broken symbolic link' fi 

The -h test is true for a symbolic link, no matter if it's broken or not. There is also a -L test which is identical to the -h test (historical reasons).

The -e tests fails if the given pathname, with all its symbolic links resolved, can not be found. This is why we may have to use -h in a separate test if we expect that the filename component of our pathname is a broken symbolic link.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.