Skip to content

Latest commit

 

History

History
1098 lines (795 loc) · 46.9 KB

API.md

File metadata and controls

1098 lines (795 loc) · 46.9 KB

Table of contents

  • babashka.fs
    • absolute? - Returns true if f represents an absolute path.
    • absolutize - Converts f into an absolute path via Path#toAbsolutePath.
    • canonicalize - Returns the canonical path via java.io.File#getCanonicalPath.
    • components - Returns a seq of all components of f as paths, i.e.
    • copy - Copies src file to dest dir or file.
    • copy-tree - Copies entire file tree from src to dest.
    • create-dir - Creates dir using Files#createDirectory.
    • create-dirs - Creates directories using Files#createDirectories.
    • create-file - Creates empty file using Files#createFile.
    • create-link - Create a hard link from path to target.
    • create-sym-link - Create a soft link from path to target.
    • create-temp-dir - Creates a temporary directory using Files#createDirectories.
    • create-temp-file - Creates an empty temporary file using Files#createTempFile.
    • creation-time - Returns creation time as FileTime.
    • cwd - Returns current working directory as path.
    • delete - Deletes f.
    • delete-if-exists - Deletes f if it exists.
    • delete-on-exit - Requests delete on exit via File#deleteOnExit.
    • delete-tree - Deletes a file tree using walk-file-tree.
    • directory? - Returns true if f is a directory, using Files/isDirectory.
    • ends-with? - Returns true if path this ends with path other.
    • exec-paths - Returns executable paths (using the PATH environment variable).
    • executable? - Returns true if f is executable.
    • exists? - Returns true if f exists.
    • expand-home - If path begins with a tilde (~), expand the tilde to the value of the user.home system property.
    • extension - Returns the extension of a file via split-ext.
    • file - Coerces f into a File.
    • file-name - Returns the name of the file or directory.
    • file-separator
    • file-time->instant - Converts a java.nio.file.attribute.FileTime to a java.time.Instant.
    • file-time->millis - Converts a java.nio.file.attribute.FileTime to epoch millis (long).
    • get-attribute
    • glob - Given a file and glob pattern, returns matches as vector of paths.
    • gunzip - Extracts gz-file to dest directory (default ".").
    • gzip - Gzips source-file and writes the output to dir/out-file.
    • hidden? - Returns true if f is hidden.
    • home - With no arguments, returns the current value of the user.home system property.
    • instant->file-time - Converts a java.time.Instant to a java.nio.file.attribute.FileTime.
    • last-modified-time - Returns last modified time as a java.nio.file.attribute.FileTime.
    • list-dir - Returns all paths in dir as vector.
    • list-dirs - Similar to list-dir but accepts multiple roots and returns the concatenated results.
    • match - Given a file and match pattern, returns matches as vector of paths.
    • millis->file-time - Converts epoch millis (long) to a java.nio.file.attribute.FileTime.
    • modified-since - Returns seq of regular files (non-directories, non-symlinks) from file-set that were modified since the anchor path.
    • move - Move or rename a file to a target dir or file via Files/move.
    • normalize - Normalizes f via Path#normalize.
    • owner - Returns the owner of a file.
    • parent - Returns parent of f, is it exists.
    • path - Coerces f into a Path.
    • path-separator
    • posix->str - Converts a set of PosixFilePermission to a string.
    • posix-file-permissions - Gets f's posix file permissions.
    • read-all-bytes - Returns contents of file as byte array.
    • read-all-lines - Read all lines from a file.
    • read-attributes - Same as read-attributes* but turns attributes into a map and keywordizes keys.
    • read-attributes* - Reads attributes via Files/readAttributes.
    • readable? - Returns true if f is readable.
    • real-path - Converts f into real path via Path#toRealPath.
    • regular-file? - Returns true if f is a regular file, using Files/isRegularFile.
    • relative? - Returns true if f represents a relative path.
    • relativize - Returns relative path by comparing this with other.
    • same-file? - Returns true if this is the same file as other.
    • set-attribute
    • set-creation-time - Sets creation time of f to time (millis, java.time.Instant or java.nio.file.attribute.FileTime).
    • set-last-modified-time - Sets last modified time of f to time (millis, java.time.Instant or java.nio.file.attribute.FileTime).
    • set-posix-file-permissions - Sets posix file permissions on f.
    • size - Returns the size of a file (in bytes).
    • split-ext - Splits path on extension If provided, a specific extension ext, the extension (without dot), will be used for splitting.
    • split-paths - Splits a path list given as a string joined by the OS-specific path-separator into a vec of paths.
    • starts-with? - Returns true if path this starts with path other.
    • str->posix - Converts a string to a set of PosixFilePermission.
    • strip-ext - Strips extension via split-ext.
    • sym-link? - Determines if f is a symbolic link via java.nio.file.Files/isSymbolicLink.
    • temp-dir - Returns java.io.tmpdir property as path.
    • unixify - Returns path as string with Unix-style file separators (/).
    • unzip - Unzips zip-file to dest directory (default ".").
    • update-file - Updates the contents of text file path using f applied to old contents and xs.
    • walk-file-tree - Walks f using Files/walkFileTree.
    • which - Returns Path to first executable program found in :paths opt, similar to the which Unix command.
    • which-all - Returns every Path to program found in (exec-paths).
    • windows? - Returns true if OS is Windows.
    • with-temp-dir - Evaluate body with binding-name bound to a temporary directory.
    • writable? - Returns true if f is writable.
    • write-bytes - Writes bytes to path via java.nio.file.Files/write.
    • write-lines - Writes lines, a seqable of strings to path via java.nio.file.Files/write.
    • xdg-cache-home - Path representing the base directory relative to which user-specific non-essential data files should be stored as described in the XDG Base Directory Specification.
    • xdg-config-home - Path representing the base directory relative to which user-specific configuration files should be stored as described in the XDG Base Directory Specification.
    • xdg-data-home - Path representing the base directory relative to which user-specific data files should be stored as described in the XDG Base Directory Specification.
    • xdg-state-home - Path representing the base directory relative to which user-specific state files should be stored as described in the XDG Base Directory Specification.
    • zip - Zips entry or entries into zip-file.

(absolute? f)

Returns true if f represents an absolute path.

Source

(absolutize f)

Converts f into an absolute path via Path#toAbsolutePath.

Source

(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.

Source

(components f)

Returns a seq of all components of f as paths, i.e. split on the file separator.

Source

(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).

Source

(copy-tree src dest)
(copy-tree src dest {:keys [:replace-existing :copy-attributes :nofollow-links], :as opts})

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.

Source

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

Creates dir using Files#createDirectory. Does not create parents.

Source

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

Creates directories using Files#createDirectories. Also creates parents if needed. Doesn't throw an exception if the dirs exist already. Similar to mkdir -p

Source

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

Creates empty file using Files#createFile.

Source

(create-link path target)

Create a hard link from path to target.

Source

(create-sym-link path target)

Create a soft link from path to target.

Source

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

Creates a temporary directory using Files#createDirectories.

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

create temp dir in dir 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------".

Source

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

Creates an empty temporary file using Files#createTempFile.

  • (create-temp-file): creates temp file with random prefix and suffix.
  • (create-temp-dir {:keys [:prefix :suffix :path :posix-file-permissions]}): create temp file in path with prefix. If prefix and suffix are not provided, random ones are generated. The :posix-file-permissions option is a string like "rwx------".

Source

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

Returns creation time as FileTime.

Source

(cwd)

Returns current working directory as path

Source

(delete f)

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

Source

(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.

Source

(delete-on-exit f)

Requests delete on exit via File#deleteOnExit. Returns f.

Source

(delete-tree root)

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

Source

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

Returns true if f is a directory, using Files/isDirectory.

Source

(ends-with? this other)

Returns true if path this ends with path other.

Source

(exec-paths)

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

Source

(executable? f)

Returns true if f is executable.

Source

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

Returns true if f exists.

Source

(expand-home f)

If [path](#babashka.fs/path) begins with a tilde (~), expand the tilde to the value of the user.home system property. If the [path](#babashka.fs/path) begins with a tilde immediately followed by some characters, they are assumed to be a username. This is expanded to the path to that user's home directory. This is (naively) assumed to be a directory with the same name as the user relative to the parent of the current value of user.home.

Source

(extension path)

Returns the extension of a file via split-ext.

Source

(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.

Source

(file-name x)

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

Source

Source

(file-time->instant ft)

Converts a java.nio.file.attribute.FileTime to a java.time.Instant.

Source

(file-time->millis ft)

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

Source

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

Source

(glob root pattern)
(glob root pattern opts)

Given a file and glob pattern, returns matches as vector of paths. Patterns containing ** or / will cause a recursive walk over path, unless overriden with :recursive. 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 paths. Note: on Windows files starting with a dot are not hidden, unless their hidden attribute is set.
  • :follow-links: follow symlinks.
  • :recursive: force recursive search.

Examples: (fs/glob "." "**.clj")

Source

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

Extracts gz-file to dest directory (default ".").

Options:

  • :replace-existing - true / false: overwrite existing files

Source

(gzip source-file)
(gzip source-file {:keys [dir out-file], :or {dir "."}})

Gzips source-file and writes the output to dir/out-file. If out-file is not provided, the source-file name with .gz appended is used. If dir is not provided, the current directory is used. Returns the created gzip file.

Source

(hidden? f)

Returns true if f is hidden.

Source

(home)
(home user)

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)

Converts a java.time.Instant to a java.nio.file.attribute.FileTime.

Source

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

Returns last modified time as a java.nio.file.attribute.FileTime.

Source

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

Returns all paths in dir as vector. 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)

Similar to list-dir but accepts multiple roots 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 pattern)
(match root pattern {:keys [hidden follow-links max-depth recursive]})

Given a file and match pattern, returns matches as vector of paths. Pattern 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 paths - note: on Windows paths starting with a dot are not hidden, unless their hidden attribute is set.
  • :follow-links: - follow symlinks
  • :recursive: - match recursively.
  • :max-depth: - max depth to descend into directory structure.

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

Source

(millis->file-time millis)

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

Source

(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.

Source

(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.

Source

(normalize f)

Normalizes f via Path#normalize.

Source

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

Returns the owner of a file. Call str on it to get the owner name as a string.

Source

(parent f)

Returns parent of f, is it exists. Akin to dirname in bash.

Source

(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.

Source

Source

(posix->str p)

Converts a set of PosixFilePermission to a string.

Source

(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.

Source

(read-all-bytes f)

Returns contents of file as byte array.

Source

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

Read all lines from a file.

Source

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

Same as read-attributes* but turns attributes into a map and keywordizes keys. Keywordizing can be changed by passing a :key-fn in the options map.

Source

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

Reads attributes via Files/readAttributes.

Source

(readable? f)

Returns true if f is readable

Source

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

Converts f into real path via Path#toRealPath.

Source

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

Returns true if f is a regular file, using Files/isRegularFile.

Source

(relative? f)

Returns true if f represents a relative path.

Source

(relativize this other)

Returns relative path by comparing this with other.

Source

(same-file? this other)

Returns true if this is the same file as other.

Source

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

Source

(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).

Source

(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).

Source

(set-posix-file-permissions f posix-file-permissions)

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

Source

(size f)

Returns the size of a file (in bytes).

Source

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

Splits path on extension If provided, a specific extension ext, the extension (without dot), will be used for splitting. Directories are not processed.

Source

(split-paths joined-paths)

Splits a path list given as a string joined by the OS-specific path-separator into a vec of paths. On UNIX systems, the separator is ':', on Microsoft Windows systems it is ';'.

Source

(starts-with? this other)

Returns true if path this starts with path other.

Source

(str->posix s)

Converts a string to a set of PosixFilePermission.

Source

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

Strips extension via split-ext.

Source

(sym-link? f)

Determines if f is a symbolic link via java.nio.file.Files/isSymbolicLink.

Source

(temp-dir)

Returns java.io.tmpdir property as path.

Source

(unixify f)

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

Source

(unzip zip-file)
(unzip zip-file dest)
(unzip zip-file dest {:keys [replace-existing]})

Unzips zip-file to dest directory (default ".").

Options:

  • :replace-existing - true / false: overwrite existing files

Source

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

Updates the contents of text file path using f applied to old contents and xs. Returns the new contents.

Options:

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

Source

(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.

Source

(which program)
(which program opts)

Returns Path to first executable program found in :paths opt, similar to the which Unix command. Default for :paths is (exec-paths).

On Windows, searches for program with filename extensions specified in :win-exts opt. Default is ["com" "exe" "bat" "cmd"]. If program already includes an extension from :win-exts, it will be searched as-is first.

When program is a relative or absolute path, :paths 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.

Source

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

Returns every Path to program found in (exec-paths). See which.

Source

(windows?)

Returns true if OS is Windows.

Source

(with-temp-dir [binding-name options] & body)

Function.

Evaluate body with binding-name bound to a temporary directory.

The directory is created by passing options to create-temp-dir, and will be removed with delete-tree on exit from the scope.

options is a map with the keys as for create-temp-dir.

Source

(writable? f)

Returns true if f is writable

Source

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

Writes bytes to path via java.nio.file.Files/write. Supported 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 path lines)
(write-lines path lines {:keys [charset], :or {charset "utf-8"}, :as opts})

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

Supported options:

  • :charset (default "utf-8")

Supported 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)

Path representing the base directory relative to which user-specific non-essential data files should be stored as described in the XDG Base Directory Specification.

Returns path based on the value of env-var XDG_CACHE_HOME (if set and representing an absolute path), else (fs/path (fs/home) ".cache"). When provided, appends app to the path.

Source

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

Path representing the base directory relative to which user-specific configuration files should be stored as described in the XDG Base Directory Specification.

Returns path based on the value of env-var XDG_CONFIG_HOME (if set and representing an absolute path), else (fs/path (fs/home) ".config"). When provided, appends app to the path.

Source

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

Path representing the base directory relative to which user-specific data files should be stored as described in the XDG Base Directory Specification.

Returns path based on the value of 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 path.

Source

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

Path representing the base directory relative to which user-specific state files should be stored as described in the XDG Base Directory Specification.

Returns path based on the value of 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 path.

Source

(zip zip-file entries)
(zip zip-file entries opts)

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

Options:

  • :root: directory which will be elided in zip. E.g.: (fs/zip ["src"] {:root "src"})
  • :path-fn: a single-arg function from file system path to zip entry path.

Source