Path

public struct Path : Pathish
extension Path: Codable
extension Path: CustomStringConvertible
extension Path: CustomDebugStringConvertible

A Path represents an absolute path on a filesystem.

All functions on Path are chainable and short to facilitate doing sequences of file operations in a concise manner.

Path supports Codable, and can be configured to encode paths relatively.

Sorting a Sequence of paths will return the locale-aware sort order, which will give you the same order as Finder.

Converting from a String is a common first step, here are the recommended ways to do that:

let p1 = Path.root/pathString
let p2 = Path.root/url.path
let p3 = Path.cwd/relativePathString
let p4 = Path(userInput) ?? Path.cwd/userInput

If you are constructing paths from static-strings we provide support for dynamic members:

let p1 = Path.root.usr.bin.ls  // => /usr/bin/ls

However we only provide this support off of the static members like root due to the anti-pattern where Path.swift suddenly feels like Javascript otherwise.

Note

A Path does not necessarily represent an actual filesystem entry.

See also

Pathish for most methods you will use on Path instances.
  • The normalized string representation of the underlying filesystem path

    Declaration

    Swift

    public let string: String
  • Creates a new absolute, standardized path.

    Note

    Resolves any .. or . components.

    Note

    Removes multiple subsequent and trailing occurences of /.

    Note

    Does not resolve any symlinks.

    Note

    On macOS, removes an initial component of “/private/var/automount”, “/var/automount”, or “/private” from the path, if the result still indicates an existing file or directory (checked by consulting the file system).

    Declaration

    Swift

    public init?<S>(_ description: S) where S : StringProtocol

    Return Value

    The path or nil if fed a relative path or a ~foo string where there is no user foo.

  • Creates a new absolute, standardized path from the provided file-scheme URL.

    Note

    If the URL is not a file URL, returns nil.

    Declaration

    Swift

    public init?(url: URL)
  • Creates a new absolute, standardized path from the provided file-scheme URL.

    Note

    If the URL is not a file URL, returns nil.

    Note

    If the URL is a file reference URL, converts it to a POSIX path first.

    Declaration

    Swift

    public init?(url: NSURL)
  • Converts anything that is Pathish to a Path

    Declaration

    Swift

    public init<P>(_ path: P) where P : Pathish
  • A filesystem entry’s kind, file, directory, symlink etc.

    See more

    Declaration

    Swift

    enum EntryType : CaseIterable

Common Directories

  • cwd

    Returns a Path containing FileManager.default.currentDirectoryPath.

    Declaration

    Swift

    public static var cwd: DynamicPath { get }
  • Returns a Path representing the root path.

    Declaration

    Swift

    public static var root: DynamicPath { get }
  • Undocumented

    Declaration

    Swift

    public static func source(for filePath: String = #file) -> (file: DynamicPath, directory: DynamicPath)
  • Returns a Path representing the user’s home directory

    Declaration

    Swift

    public static var home: DynamicPath { get }
  • The root for user documents.

    Note

    There is no standard location for documents on Linux, thus we return ~/Documents.

    Note

    You should create a subdirectory before creating any files.

    Declaration

    Swift

    public static var documents: DynamicPath { get }
  • The root for cache files.

    Note

    On Linux this is XDG_CACHE_HOME.

    Note

    You should create a subdirectory before creating any files.

    Declaration

    Swift

    public static var caches: DynamicPath { get }
  • For data that supports your running application.

    Note

    On Linux is XDG_DATA_HOME.

    Note

    You should create a subdirectory before creating any files.

    Declaration

    Swift

    public static var applicationSupport: DynamicPath { get }
  • Returns Path.string

    Declaration

    Swift

    public var description: String { get }
  • Returns eg. Path(string: "/foo")

    Declaration

    Swift

    public var debugDescription: String { get }
  • The builder for Path.find()

    See more

    Declaration

    Swift

    class Finder
    extension Path.Finder: Sequence, IteratorProtocol