Finding Files

  • Find all the path names matching a specified pattern according to the rules used by the Unix shells. Home directory (tilde, ~) gets expanded. Globstar (**) can be included as part of the pattern to represent all intermediary directories, making the search recursive, similar¹ to pattern expansion on Zsh, Fish and Bash 4+. This is implemented with libc².

    ¹ globstar expansion has many quirks and differences among different implementations. Pathos use the following algorithm:

    • find the leftmost occurance of ** (ignore the rest of occurances).
    • split the pattern by middle of the globstar. So a/**/c/*.swift, for example, would become a/* and */c/*.swift.
    • perform a non-recursive, system glob with the first half (a/* from previous example).
    • for each result from previous glob that is a directory, recursively find all of its children.
    • perform an fnmatch (per POSIX/libc) with the full pattern with each child, return the ones that match.

    ² due to libc implementation, there are subtle behaviral differences among platforms. Specifically, broken symbolic links will not be found on Linux, but it will be included on Darwin.

    Throws

    A SystemError if any sub-directory cannot be opened or there’s not enough memory to hold all data for results from listing child paths.

    See also

    To work with Path or PathRepresentable, use PathRepresentable.glob(_:).

    Declaration

    Swift

    public func glob(_ pattern: String) throws -> [String]

    Parameters

    pattern

    a pattern according to rules of Unix shells.

    Return Value

    paths in current working directory that matches the pattern.

  • Find paths to directories and files of all types in path.

    Throws

    A SystemError if path cannot be opened as a directory or there’s not enough memory to hold all data for the results.

    Declaration

    Swift

    public func children(inPath path: String, recursive: Bool = false) throws -> [(String, FileType)]

    Parameters

    path

    the path whose children will be returned.

    recursive

    set to true to include results from child directories in addition to that from path. Defaults to false.

    Return Value

    path to directories and files of all types in path, and their types, in pairs.

  • Find paths to directories or files of a type in path.

    Throws

    A SystemError if path cannot be opened as a directory or there’s not enough memory to hold all data for the results.

    Declaration

    Swift

    public func children(inPath path: String, ofType type: FileType, recursive: Bool = false) throws -> [String]

    Parameters

    path

    the path whose children will be returned.

    type

    the type of file to find. See FileType.

    recursive

    set to true to include results from child directories in addition to that from path. Defaults to false.

    Return Value

    path to files of the given type in path.