Search code examples
java-8nio

Java 8 Files.walk not returning all Paths


Here is the method

public List<Path> listAllPaths() throws IOException {
         Files.walk(Paths.get("/tmp")).forEach(e -> System.out.println(e.toString()));
        return Files.walk(Paths.get(filePath)).collect(Collectors.toList());
    }

when I execute this method, I see only /tmp printed and returned I was hoping to see all subdirectories and files inside

Javadocs says

"Return a Stream that is lazily populated with Path by walking the file tree rooted at a given starting file. The file tree is traversed depth-first, the elements in the stream are Path objects that are obtained as if by resolving the relative path against start."


Solution

  • That's a symbolic link, not a real path:

    Path p = Paths.get("/tmp");
    System.out.println(Files.isSymbolicLink(p)); // returns true
    

    The real path is /private/tmp

    you can do an

    ls -l 
    

    and see that some of the folders are owned by _mbsetupuser user.

    Also you can walk the symbolic links as suggest in the comments via:

     Files.walk(Paths.get("/tmp"), FileVisitOption.FOLLOW_LINKS)