Use stat to check for a path rather than ls

This is more consistent and less error prone.