babashka.fs

absolute?

(absolute? f)
Returns true if f represents an absolute path.

absolutize

(absolutize f)
Converts f into an absolute path via Path#toAbsolutePath.

canonicalize

(canonicalize f)(canonicalize f {:keys [:nofollow-links]})
Returns the canonical path via
java.io.File#getCanonicalPath. If :nofollow-links is set, then it
will fall back on absolutize + normalize. This function can be used
as an alternative to real-path which requires files to exist.

components

(components f)
Returns a seq of all components of f.

copy

(copy src dest)(copy src dest {:keys [:replace-existing :copy-attributes :nofollow-links]})
Copies src file to dest dir or file.
Options:
- :replace-existing
- :copy-attributes
- :nofollow-links (used to determine to copy symbolic link itself or not).

copy-tree

(copy-tree src dest)(copy-tree src dest {:keys [:replace-existing :copy-attributes :nofollow-links]})
Copies entire file tree from src to dest. Creates dest if needed
using create-dirs, passing it the :posix-file-permissions
option. Supports same options as copy.

create-dir

(create-dir path)(create-dir path {:keys [:posix-file-permissions]})
Creates dir using Files#createDirectory. Does not create parents.

create-dirs

(create-dirs path)(create-dirs path {:keys [:posix-file-permissions]})
Creates directories using Files#createDirectories. Also creates parents if needed.

create-file

(create-file path)(create-file path {:keys [:posix-file-permissions]})
Creates empty file using Files#createFile.

create-temp-dir

(create-temp-dir)(create-temp-dir {:keys [:prefix :path :posix-file-permissions]})
Creates a temporary directory using Files#createDirectories.

(create-temp-dir): creates temp dir with random prefix.
(create-temp-dir {:keys [:prefix :path :posix-file-permissions]}):

create temp dir in path with prefix. If prefix is not provided, a random one
is generated. If path is not provided, the directory is created as if called with (create-temp-dir). The :posix-file-permissions option is a string like "rwx------".

creation-time

(creation-time f)(creation-time f {:keys [nofollow-links], :as opts})
Returns creation time as FileTime.

delete

(delete f)
Deletes f. Returns nil if the delete was successful,
throws otherwise. Does not follow symlinks.

delete-if-exists

(delete-if-exists f)
Deletes f if it exists. Returns true if the delete was successful,
false if f didn't exist. Does not follow symlinks.

delete-on-exit

(delete-on-exit f)
Requests delete on exit via File#deleteOnExit. Returns f.

delete-tree

(delete-tree root)
Deletes a file tree using walk-file-tree. Similar to rm -rf. Does not follow symlinks.

directory?

(directory? f)(directory? f {:keys [:nofollow-links]})
Returns true if f is a directory, using Files/isDirectory.

ends-with?

(ends-with? this other)
Returns true if path this ends with path other.

exec-paths

(exec-paths)
Returns executable paths (using the PATH environment variable). Same
as (split-paths (System/getenv "PATH")).

executable?

(executable? f)
Returns true if f is executable.

exists?

(exists? f)(exists? f {:keys [:nofollow-links]})
Returns true if f exists.

extension

(extension path)
Returns the extension of a file

file

(file f)(file f & fs)
Coerces f into a File. Multiple-arg versions treat the first argument
as parent and subsequent args as children relative to the parent.

file-name

(file-name x)
Returns the name of the file or directory. E.g. (file-name "foo/bar/baz") returns "baz".

file-separator

file-time->instant

(file-time->instant ft)
Converts a java.nio.file.attribute.FileTime to a java.time.Instant.

file-time->millis

(file-time->millis ft)
Converts a java.nio.file.attribute.FileTime to epoch millis (long).

get-attribute

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

glob

(glob root pattern)(glob root pattern {:keys [hidden follow-links max-depth]})
Given a file and glob pattern, returns matches as vector of
files. Patterns containing ** or / will cause a recursive walk over
path. Glob interpretation is done using the rules described in
https://docs.oracle.com/javase/7/docs/api/java/nio/file/FileSystem.html#getPathMatcher(java.lang.String).

Options:

- :hidden: match hidden files. Note: on Windows files starting with
a dot are not hidden, unless their hidden attribute is set.
- :follow-links: follow symlinks.

hidden?

(hidden? f)
Returns true if f is hidden.

instant->file-time

(instant->file-time instant)
Converts a java.time.Instant to a java.nio.file.attribute.FileTime.

last-modified-time

(last-modified-time f)(last-modified-time f {:keys [nofollow-links], :as opts})
Returns last modified time as a java.nio.file.attribute.FileTime.

list-dir

(list-dir dir)(list-dir dir glob-or-accept)
Returns all paths in dir as vector. For descending into subdirectories use glob.

list-dirs

(list-dirs dirs glob-or-accept)
Similar to list-dir but accepts multiple roots and returns the concatenated results.

millis->file-time

(millis->file-time millis)
Converts epoch millis (long) to a java.nio.file.attribute.FileTime.

modified-since

(modified-since anchor file-set)
Returns seq of regular files (non-directories, non-symlinks) from file-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 file-set may be a regular file, directory or
collection of files (e.g. returned by glob). Directories are
searched recursively.

move

(move source target)(move source target {:keys [:replace-existing :atomic-move :nofollow-links]})
Move or rename a file to a target dir or file via Files/move.

normalize

(normalize f)
Normalizes f via Path#normalize.

parent

(parent f)
Returns parent of f, is it exists.

path

(path f)(path parent child)(path parent child & more)
Coerces f into a Path. Multiple-arg versions treat the first argument as
parent and subsequent args as children relative to the parent.

path-separator

posix->str

(posix->str p)
Converts a set of PosixFilePermission to a string.

posix-file-permissions

(posix-file-permissions f)(posix-file-permissions f {:keys [:nofollow-links]})
Gets f's posix file permissions. Use posix->str to view as a string.

read-all-bytes

(read-all-bytes f)
Returns contents of file as byte array.

read-all-lines

(read-all-lines f)

read-attributes

(read-attributes path attributes)(read-attributes path attributes {:keys [:nofollow-links :key-fn]})
Reads attributes via Files/readAttributes. Keywordizes string keys into keywords. This can be changed by passing a :key-fn in the options map.

readable?

(readable? f)
Returns true if f is readable

real-path

(real-path f)(real-path f {:keys [:nofollow-links]})
Converts f into real path via Path#toRealPath.

regular-file?

(regular-file? f)(regular-file? f {:keys [:nofollow-links]})
Returns true if f is a regular file, using Files/isRegularFile.

relative?

(relative? f)
Returns true if f represents a relative path.

relativize

(relativize this other)
Returns relative path by comparing this with other.

same-file?

(same-file? this other)
Returns true if this is the same file as other.

set-attribute

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

set-creation-time

(set-creation-time f time)(set-creation-time f time {:keys [nofollow-links], :as opts})
Sets creation time of f to time (millis, java.time.Instant or java.nio.file.attribute.FileTime).

set-last-modified-time

(set-last-modified-time f time)(set-last-modified-time f time {:keys [nofollow-links], :as opts})
Sets last modified time of f to time (millis, java.time.Instant or java.nio.file.attribute.FileTime).

set-posix-file-permissions

(set-posix-file-permissions f posix-file-permissions)
Sets posix file permissions on f. Accepts a string like "rwx------" or a set of PosixFilePermission.

size

(size f)

split-ext

(split-ext path)
Splits a path into a vec of [path-without-ext ext]. Works with strings, files, or paths.

split-paths

(split-paths joined-paths)
Splits a string joined by the OS-specific path-seperator into a vec of paths.

starts-with?

(starts-with? this other)
Returns true if path this starts with path other.

str->posix

(str->posix s)
Converts a string to a set of PosixFilePermission.

strip-ext

(strip-ext path)(strip-ext path {:keys [ext]})
Returns the path with the extension removed. If provided, a specific extension will be removed.

sym-link?

(sym-link? f)

temp-dir

(temp-dir)
Returns java.io.tmpdir property as path.

unzip

(unzip zip-file)(unzip zip-file dest)(unzip zip-file dest {:keys [replace-existing]})
zip-file: zip archive to unzip (required)
dest: destination directory (defaults to ".")
Options:
  :replace-existing true/false: overwrite existing files

walk-file-tree

(walk-file-tree f {:keys [:pre-visit-dir :post-visit-dir :visit-file :visit-file-failed :follow-links :max-depth]})
Walks f using Files/walkFileTree. Visitor functions: :pre-visit-dir,
:post-visit-dir, :visit-file, :visit-file-failed. All visitor functions
default to (constantly :continue). Supported return
values: :continue, :skip-subtree, :skip-siblings, :terminate. A
different return value will throw.

which

(which program)(which program {:keys [:all]})
Locates a program in (exec-paths) similar to the which Unix command.
When using the :all option, a vec of results instead of a single result is returned.

writable?

(writable? f)
Returns true if f is readable