Skip to content

Latest commit

 

History

History
1401 lines (1102 loc) · 73.1 KB

File metadata and controls

1401 lines (1102 loc) · 73.1 KB

Table of contents


(absolute? path)

Function.

Returns true if path is absolute via Path#isAbsolute.

Source

(absolutize path)

Function.

Converts path into an absolute path via Path#toAbsolutePath.

Source

(canonicalize path)
(canonicalize path {:keys [:nofollow-links]})

Function.

Returns the canonical path for path via File#getCanonicalPath.

Options:

This function can be used as an alternative to real-path which requires files to exist.

Source

(components path)

Function.

Returns a seq of paths for all components of path. i.e.: split on the file-separator.

Source

(copy source-file target-path)
(copy source-file target-path {:keys [replace-existing copy-attributes nofollow-links]})

Function.

Copies source-file to target-path dir or file via Files/copy.

Options:

  • :replace-existing
  • :copy-attributes
  • :nofollow-links - used to determine to copy symbolic link itself or not.

Returns target-path.

Source

(copy-tree source-dir target-dir)
(copy-tree source-dir target-dir {:keys [:replace-existing :copy-attributes :nofollow-links], :as opts})

Function.

Copies entire file tree from source-dir to target-dir. Creates target-dir if needed.

Options:

  • same as copy
  • :posix-file-permissions - string format unix-like system permissions passed to create-dirs when creating target-dir.

Source

(create-dir dir)
(create-dir dir {:keys [:posix-file-permissions]})

Function.

Creates dir via Files/createDirectory. Does not create parents.

Returns dir.

Options:

  • :posix-file-permissions - string format for unix-like system permissions for dir, as described in str->posix. Affected by umask.

Source

(create-dirs dir)
(create-dirs dir {:keys [:posix-file-permissions]})

Function.

Creates dir via Files/createDirectories. Also creates parents if needed. Does not throw an exception if the dirs exist already. Similar to mkdir -p shell command.

Returns dir.

Options:

  • :posix-file-permissions - string format for unix-like system permissions for dir, as described in str->posix. Affected by umask.

Source

(create-file file)
(create-file file {:keys [:posix-file-permissions]})

Function.

Creates empty file via Files/createFile.

Returns file.

Options:

  • :posix-file-permissions - string format for unix-like system permissions for file, as described in str->posix. Affected by umask.

Source

(create-link link existing-file)

Function.

Creates a new hard link (directory entry) for an existing-file via Files/createLink.

Returns link.

Source

(create-sym-link link target-path)

Function.

Creates a symbolic link to target-path via Files/createSymbolicLink.

Returns link.

As of this writing, JDKs do not recognize empty-string target-path "" as the cwd. Consider instead using a target-path of "." to link to the cwd.

Source

(create-temp-dir)
(create-temp-dir {:keys [:dir :prefix :posix-file-permissions], :as opts})

Function.

Returns path to directory created via Files/createTempDirectory.

This function does not set up any automatic deletion of the directories it creates. See with-temp-dir for that functionality.

Options:

  • :dir - directory in which to create the new directory. Defaults to default system temp dir (e.g. /tmp); see temp-dir. Must already exist.
  • :prefix - provided as a hint to the process that generates the name of the new directory. In most cases, this will be the beginning of the new directory name. Defaults to a random (v4) UUID.
  • :posix-file-permissions - string format unix-like system permissions as described in str->posix for new directory. If not specified, uses the file system default permissions for new directories. Affected by umask.
  • ⚠️ :path - [DEPRECATED] previous name for :dir, kept for backwards compatibility. If both :path and :dir are given (don't do that!), :dir is used.

Examples:

  • (create-temp-dir)
  • (create-temp-dir {:posix-file-permissions "rwx------"})
  • (create-temp-dir {:dir (path (cwd) "_workdir") :prefix "process-1-"})

Source

(create-temp-file)
(create-temp-file {:keys [:dir :prefix :suffix :posix-file-permissions], :as opts})

Function.

Returns path to empty file created via Files/createTempFile.

This function does not set up any automatic deletion of the files it creates. Create the file in a with-temp-dir for that functionality.

Options:

  • :dir - directory in which to create the new file. Defaults to default system temp dir (e.g. /tmp); see temp-dir. Must already exist.
  • :prefix - provided as a hint to the process that generates the name of the new file. In most cases, this will be the beginning of the new file name. Defaults to a random (v4) UUID.
  • :suffix - provided as a hint to the process that generates the name of the new file. In most cases, this will be the end of the new file name. Defaults to a random (v4) UUID.
  • :posix-file-permissions - string format unix-like system permissions for new file, as described in str->posix. If not specified, uses the file system default permissions for new files. Affected by umask.
  • ⚠️ :path - [DEPRECATED] Previous name for :dir, kept for backwards compatibility. If both :path and :dir are given (don't do that!), :dir is used.

Examples:

  • (create-temp-file)
  • (create-temp-file {:posix-file-permissions "rw-------"})
  • (create-temp-file {:dir (path (cwd) "_workdir") :prefix "process-1-" :suffix "-queue"})

Source

(creation-time path)
(creation-time path {:keys [nofollow-links], :as opts})

Function.

Returns creation time of path as FileTime.

See README notes for some details on behaviour.

See also: set-creation-time, last-modified-time, file-time->instant, file-time->millis

Source

(cwd)

Function.

Returns current working directory path.

Source

(delete path)

Function.

Deletes path via Files/delete. Returns nil if the delete was successful, throws otherwise. Does not follow symlinks.

Source

(delete-if-exists path)

Function.

Deletes path if it exists via Files/deleteIfExists. Returns true if the delete was successful, false if path didn't exist. Does not follow symlinks.

Source

(delete-on-exit path)

Function.

Requests delete of path on exit via File#deleteOnExit. Returns path.

Source

(delete-tree root-path)
(delete-tree root-path {:keys [force]})

Function.

Deletes the file tree at root-path using walk-file-tree. Similar to rm -rf shell command. Does not follow symlinks.

Options:

  • :force - if true forces deletion of read-only files/directories. Similar to chmod -R +wx + rm -rf shell commands.

Source

(directory? path)
(directory? path {:keys [:nofollow-links]})

Function.

Returns true if path is a directory via Files/isDirectory.

Options:

Source

(ends-with? this-path other-path)

Function.

Returns true if this-path ends with other-path via Path#endsWith.

See also: starts-with?

Source

(exec-paths)

Function.

Returns a vector of command search paths (from the PATH environment variable). Same as (split-paths (System/getenv "PATH")).

Source

(executable? path)

Function.

Returns true if path is executable via Files/isExecutable.

Source

(exists? path)
(exists? path {:keys [:nofollow-links]})

Function.

Returns true if path exists via Files/exists.

Options:

Source

(expand-home path)

Function.

Returns path replacing ~ (tilde) with home dir.

If path:

  • does not start with ~, returns path.
  • starts with ~ then file-separator, ~ is replaced with (home). e.g., ~/foo -> /home/myuser/foo
  • starts with ~ then some other chars, those other chars are assumed to be a username, then naively expanded to (home username). e.g., ~someuser/foo -> /home/someuser/foo

See also: home

Source

(extension path)

Function.

Returns the extension of path via split-ext.

Source

(file path)
(file path & paths)

Function.

Coerces path(s) into a File, combining multiple paths into one. Multiple-arg versions treat the first argument as parent and subsequent args as children relative to the parent.

Source

(file-name path)

Function.

Returns the name of the file or directory for path via File#getName. E.g. (file-name "foo/bar/baz") returns "baz".

Source

The system-dependent default path component separator character (as string) via File/separator.

Source

(file-time->instant ft)

Function.

Converts ft FileTime to an Instant.

Source

(file-time->millis ft)

Function.

Converts ft FileTime to epoch milliseconds (long).

Source

(get-attribute path attribute)
(get-attribute path attribute {:keys [:nofollow-links]})

Function.

Returns value of attribute for path via Files/getAttribute.

Options:

Source

(glob root-dir pattern)
(glob root-dir pattern opts)

Function.

Returns a vector of paths matching glob pattern (on path and filename) relative to root-dir. Patterns containing ** or / will cause a recursive walk under root-dir, unless overriden with :recursive false. Similarly, :hidden will be automatically enabled when pattern starts with a dot. Glob interpretation is done using the rules described in FileSystem#getPathMatcher

Options:

  • :hidden - match hidden paths. Implied true when pattern starts with a dot; otherwise, defaults to false. Note: on Windows files starting with a dot are not hidden, unless their hidden attribute is set.
  • :follow-links - follow symlinks. Defaults to false.
  • :recursive - implied true when pattern contains ** or /; otherwise, defaults to false.
    • true - pattern is matched against all descendant files and directories under root-dir
    • false - pattern is matched only against immediate children under root-dir
  • :max-depth - max depth to descend into directory structure, when recursing. Defaults to Integer/MAX_VALUE.

Examples:

  • (fs/glob "." "**.clj") - finds .clj files and dirs under . dir and its subdirs
  • (fs/glob "." "**.clj" {:recursive false}) - finds .clj files and dirs immediately under . dir only
  • (fs/glob "." "*.clj" {:recursive true}) - finds .clj files and dirs immediately under . only (pattern lacks directory wildcards)

If on macOS, see note on glob

See also: match

Source

(gunzip gz-file)
(gunzip gz-file target-dir)
(gunzip gz-file target-dir {:keys [replace-existing]})

Function.

Extracts gz-file to target-dir.

If target-dir not specified (or nil) defaults to gz-file dir.

File is extracted to target-dir with gz-file file-name without .gz extension.

Creates target-dir dir(s) if necessary. The gz-file is not deleted.

Options:

  • :replace-existing - when true overwrites existing file

See also: gzip

Source

(gzip source-file)
(gzip source-file {:keys [dir out-file]})

Function.

Gzips source-file to :dir/:out-file.

Does not store the source-file name in the .gz file. The source-file is not deleted.

Options:

  • :dir(s) - created if necessary. If not specified, defaults to source-file dir.
  • :out-file - if not specified, defaults to source-file file-name with .gz extension.

Returns the created gzip file.

See also: gunzip

Source

(hidden? path)

Function.

Returns true if path is hidden via Files/isHidden.

TIP: some older JDKs can throw on empty-string path (hidden ""). Consider instead checking cwd via (hidden ".").

Source

(home)
(home user)

Function.

Returns home dir path.

With no arguments, returns the current value of the user.home system property. If a user is passed, returns that user's home directory as found in the parent of home with no args.

Source

(instant->file-time instant)

Function.

Converts instant Instant to a FileTime.

Source

(last-modified-time path)
(last-modified-time path {:keys [nofollow-links], :as opts})

Function.

Returns last modified time of path as FileTime.

See also: set-last-modified-time, creation-time, file-time->instant, file-time->millis

Source

(list-dir dir)
(list-dir dir glob-or-accept)

Function.

Returns a vector of all paths in dir. For descending into subdirectories use glob.

  • glob-or-accept - a glob string such as "*.edn" or a (fn accept [^java.nio.file.Path p]) -> truthy

Source

(list-dirs dirs glob-or-accept)

Function.

Similar to list-dir but accepts multiple roots in dirs and returns the concatenated results.

  • glob-or-accept - a glob string such as "*.edn" or a (fn accept [^java.nio.file.Path p]) -> truthy

Source

(match root-dir pattern)
(match root-dir pattern {:keys [hidden follow-links max-depth recursive]})

Function.

Returns a vector of paths matching pattern (on path and filename) relative to root-dir. Pattern interpretation is done using the rules described in FileSystem#getPathMatcher

Options:

  • :hidden - match hidden paths - note: on Windows paths starting with a dot are not hidden, unless their hidden attribute is set. Defaults to false, i.e. skip hidden files and folders.
  • :follow-links - follow symlinks. Defaults to false.
  • :recursive
    • true - pattern is matched against all descendant files and directories under root-dir
    • false (default) - pattern is matched only against immediate children under root-dir
  • :max-depth - max depth to descend into directory structure, when matching recursively. Defaults to Integer/MAX_VALUE.

Examples:

  • (fs/match "." "regex:.*\\.clj" {:recursive true})

See also: glob

Source

(millis->file-time millis)

Function.

Converts epoch milliseconds (long) to a FileTime.

Source

(modified-since anchor-path path-set)

Function.

Returns seq of regular files (non-directories, non-symlinks) from path-set that were modified since the anchor-path. The anchor-path can be a regular file or directory, in which case the recursive max last modified time stamp is used as the timestamp to compare with. The path-set may be a regular file, directory or collection of paths (e.g. as returned by glob). Directories are searched recursively.

Source

(move source-path target-path)
(move source-path target-path {:keys [:replace-existing :atomic-move]})

Function.

Moves or renames dir or file at source-path to target-path dir or file via Files/move. If target-path is a directory, moves source-path under target-path. Never follows symbolic links.

Returns target-path.

Options:

  • replace-existing - overwrite existing target-path, default false
  • atomic-move - watchers will only see complete target-path file, default false

Source

(normalize path)

Function.

Returns normalized path for path via Path#normalize.

Source

(owner path)
(owner path {:keys [:nofollow-links]})

Function.

Returns the owner of path via Files/getOwner. Call str on return value to get the owner name as a string.

Options:

Source

(parent path)

Function.

Returns parent path of path via Path#getParent. Akin to dirname in bash.

Source

(path path)
(path parent child)
(path parent child & more)

Function.

Coerces path(s) into a Path, combining multiple paths into one. Multiple-arg versions treat the first argument as parent and subsequent args as children relative to the parent.

Source

The system-dependent path-separator character (as string) via File/pathSeparator.

Source

(posix->str p)

Function.

Converts a set of PosixFilePermission p to a string, like "rwx------" via PosixFilePermissions/toString.

See also: str->posix

Source

(posix-file-permissions path)
(posix-file-permissions path {:keys [:nofollow-links]})

Function.

Returns a set of PosixFilePermission for path via Files/getPosixFilePermissions. Use posix->str to convert to a string.

Options:

See also: set-posix-file-permissions

Source

(read-all-bytes file)

Function.

Returns contents of file as byte array via Files/readAllBytes.

Source

(read-all-lines file)
(read-all-lines file {:keys [charset], :or {charset "utf-8"}})

Function.

Returns contents of file as a vector of lines via Files/readAllLines.

Options:

  • :charset - defaults to "utf-8"

Source

(read-attributes path attributes)
(read-attributes path attributes {:keys [:nofollow-links :key-fn], :as opts})

Function.

Same as read-attributes* but returns requested attributes for path as a map with keywordized attribute keys.

Options:

  • :key-fn - optionally override keywordizing function with your own.
  • :nofollow-links

Source

(read-attributes* path attributes)
(read-attributes* path attributes {:keys [:nofollow-links]})

Function.

Returns requested attributes for path via Files/readAttributes.

Options:

Source

(read-link sym-link-path)

Function.

Returns the immediate target of sym-link-path via Files/readSymbolicLink. The target need not exist.

Source

(readable? path)

Function.

Returns true if path is readable via Files/isReadable

Source

(real-path path)
(real-path path {:keys [:nofollow-links]})

Function.

Converts path into real path via Path#toRealPath.

Options:

Source

(regular-file? path)
(regular-file? path {:keys [:nofollow-links]})

Function.

Returns true if path is a regular file via Files/isRegularFile.

Options:

Source

(relative? path)

Function.

Returns true if path is relative (in other words, is not absolute?).

Source

(relativize base-path other-path)

Function.

Returns other-path relative to base-path via Path#relativize.

Examples:

  • (fs/relativize "a/b" "a/b/c/d") => c/d
  • (fs/relativize "a/b/c/d" "a/b") => ../..

Source

(root path)

Function.

Returns root path for path, or nil, via Path#getRoot.

The return value depends upon the runtime platform.

On Windows, returns Windows specific roots, ex: (replace forward slash with backslash):

  • C:/ for C:/foo/bar
  • C: for C:foo/bar
  • //server/share for //server/share/foo/bar

On Linux and macOS, returns the leading / for anything that looks like an absolute path.

Source

(same-file? this-path other-path)

Function.

Returns true if this-path is the same file as other-path via Files/isSamefile.

Source

(set-attribute path attribute value)
(set-attribute path attribute value {:keys [:nofollow-links]})

Function.

Sets attribute for path to value via Files/setAttribute.

Source

(set-creation-time path time)
(set-creation-time path time {:keys [nofollow-links], :as opts})

Function.

Sets creation time of path. time can be epoch milliseconds (long), FileTime, or Instant.

Options:

See README notes for some details on behaviour.

See also: creation-time

Source

(set-last-modified-time path time)
(set-last-modified-time path time {:keys [nofollow-links], :as opts})

Function.

Sets last modified time of path. time can be epoch milliseconds (long), FileTime, or Instant.

See also: last-modified-time

Source

(set-posix-file-permissions path posix-file-permissions)

Function.

Sets posix-file-permissions on path via Files/setPosixFilePermissions. Accepts a string like "rwx------" or a set of PosixFilePermission.

See also: posix-file-permissions

Source

(size path)

Function.

Returns the size of path in bytes.

Source

(split-ext path)
(split-ext path {:keys [ext]})

Function.

Splits path on extension. Returns [name ext]. Leading directories in path are not processed.

Options:

  • :ext - split on specified extension (do not include a leading dot)

Examples:

  • (fs/split-ext "foo.bar.baz") => ["foo.bar" "baz"]
  • (fs/split-ext "foo.bar.baz" {:ext "bar.baz"}) => ["foo" "bar.baz"]
  • (fs/split-ext "foo.bar.baz" {:ext "png"}) => ["foo.bar.baz" nil]

Source

(split-paths joined-paths)

Function.

Splits joined-paths string into a vector of paths by OS-specific path-separator.

Source

(starts-with? this-path other-path)

Function.

Returns true if this-path starts with other-path via Path#startsWith.

See also: ends-with?

Source

(str->posix s)

Function.

Converts string s to a set of PosixFilePermission via PosixFilePermissions/fromString.

s is a string like "rwx------".

See also: posix->str

Source

(strip-ext path)
(strip-ext path {:keys [ext], :as opts})

Function.

Strips extension from path via split-ext.

Source

(sym-link? path)

Function.

Returns true if path is a symbolic link via Files/isSymbolicLink.

Source

(temp-dir)

Function.

Returns java.io.tmpdir property as path.

Source

(touch path)
(touch path {:keys [time nofollow-links], :as opts})

Function.

Updates last modified time of path to :time, creating path as a file if it does not exist.

If path is deleted by some other process/thread before :time is set, a NoSuchFileException will be thrown. Callers can, if their use case requires it, implement their own retry loop.

Options:

Source

(unixify path)

Function.

Returns path as string with Unix-style file separators (/).

Source

(unzip zip-file)
(unzip zip-file target-dir)
(unzip zip-file target-dir {:keys [replace-existing extract-fn]})

Function.

Unzips zip-file to target-dir (default ".").

Options:

  • :replace-existing - true / false: overwrite existing files
  • :extract-fn - function that decides if the current ZipEntry should be extracted. Extraction only occurs if a truthy value is returned (i.e. not nil/false). The function is only called for files (not directories) with a single map arg:
    • :entry - the current ZipEntry
    • :name - the name of the ZipEntry (result of calling getName)

See also: zip.

Source

(update-file file f & xs)
(update-file file opts f & xs)

Function.

Updates the contents of text file with result of applying function f with old contents and args xs. Returns the new contents.

Options:

  • :charset - charset of file, default to "utf-8"

Source

(walk-file-tree path {:keys [:pre-visit-dir :post-visit-dir :visit-file :visit-file-failed :follow-links :max-depth]})

Function.

Walks path via Files/walkFileTree.

Options:

  • :follow-links
  • :max-depth - maximum directory depth to walk, defaults is unlimited
  • Override default visitor functions via:
    • :pre-visit-dir - args [dir attrs]
    • :post-visit-dir - args [dir ex]
    • :visit-file - args [file attrs]
    • :visit-file-failed - args [file ex]

All visitor functions must return one of :continue, :skip-subtree, :skip-siblings or :terminate. A different return value will throw. When not supplied, visitor functions default to (constantly :continue).

Returns path.

Source

(which program)
(which program opts)

Function.

Returns path to first executable program found in :paths, similar to the which Unix command.

When program is a relative or absolute path, :paths option is not consulted. On Windows, the :win-exts variants are still searched. On other OSes, the path for program will be returned if executable, else nil.

Options:

  • :paths - paths to search, default is return of (exec-paths)
  • :win-exts - active on Windows only. Searches for program with filename extensions specified in :win-exts option. If program already includes an extension from :win-exts, it will be searched as-is first. Default is ["com" "exe" "bat" "cmd"].

Source

(which-all program)
(which-all program opts)

Function.

Returns a vector of every path to program found in (exec-paths). See which.

Source

(windows?)

Function.

Returns true if OS is Windows.

Source

(with-temp-dir [temp-dir] & body)
(with-temp-dir [temp-dir opts] & body)

Macro.

Evaluates body with temp-dir bound to the result of (create-temp-dir opts).

By default, the temp-dir will be removed with delete-tree on exit from the scope.

Options:

  • see create-temp-dir for options that control directory creation
  • :keep - if true does not delete the directory on exit from macro scope.

Example:

(with-temp-dir [d]
  (let [t (path d "extract")
    (create-dir t)
    (gunzip path-to-zip t)
    (copy (path t "the-one-file-I-wanted.txt") (path permanent-dir "file-I-extracted.txt"))))
;; d no longer exists here

Source

(writable? path)

Function.

Returns true if path is writable via Files/isWritable

Source

(write-bytes file bytes)
(write-bytes file bytes {:keys [append create truncate-existing write], :as opts})

Function.

Writes bytes to file via Files/write.

Options:

  • :create - (default true)
  • :truncate-existing - (default true)
  • :write - (default true)
  • :append - (default false)
  • or any java.nio.file.StandardOption.

Examples:

(fs/write-bytes f (.getBytes (String. "foo"))) ;; overwrites + truncates or creates new file
(fs/write-bytes f (.getBytes (String. "foo")) {:append true})

Source

(write-lines file lines)
(write-lines file lines {:keys [charset], :or {charset "utf-8"}, :as opts})

Function.

Writes lines, a seqable of strings, to file via Files/write.

Options:

  • :charset - (default "utf-8")

Open options:

  • :create - (default true)
  • :truncate-existing - (default true)
  • :write - (default true)
  • :append - (default false)
  • or any java.nio.file.StandardOption.

Source

(xdg-cache-home)
(xdg-cache-home app)

Function.

Returns path to user-specific non-essential data as described in the XDG Base Directory Specification.

Uses env-var XDG_CACHE_HOME (if set and representing an absolute path), else (fs/path (fs/home) ".cache"). When provided, appends app to the returned path.

Source

(xdg-config-home)
(xdg-config-home app)

Function.

Returns path to user-specific configuration files as described in the XDG Base Directory Specification.

Uses env-var XDG_CONFIG_HOME (if set and representing an absolute path), else (fs/path (fs/home) ".config"). When provided, appends app to the returned path.

Source

(xdg-data-home)
(xdg-data-home app)

Function.

Returns path to user-specific data files as described in the XDG Base Directory Specification.

Uses env-var XDG_DATA_HOME (if set and representing an absolute path), else (fs/path (fs/home) ".local" "share"). When provided, appends app to the returned path.

Source

(xdg-state-home)
(xdg-state-home app)

Function.

Returns path to user-specific state files as described in the XDG Base Directory Specification.

Uses env-var XDG_STATE_HOME (if set and representing an absolute path), else (fs/path (fs/home) ".local" "state"). When provided, appends app to the returned path.

Source

(zip zip-file path-or-paths)
(zip zip-file path-or-paths opts)

Function.

Zips path-or-paths into zip-file. A path may be a file or directory. Directories are included recursively and their names are preserved in the zip file. Currently only accepts relative paths.

Options:

  • :root - optional directory to be elided in zip-file entries. E.g.: (fs/zip ["src"] {:root "src"})
  • :path-fn - an optional custom path conversion function. A single-arg function called for each file system path returning the path to be used for the corresponding zip entry.

See also: unzip.

Source