org.clojars.fdiotalevi/file-kit0.2.0-SNAPSHOTdependencies
| (this space intentionally left almost blank) | ||||||
Simple file manipulation in Clojurefile-kit provides several functions to operate on files. The documentation is available at http://fdiotalevi.github.io/file-kit/ It wraps and reuses code from popular libraries like Apache Common IO and guava-libraries (everything under the terms of the Apache Softawre License) to create the best in breed file utility for Clojure. to start using it, add
to the dependencies section of your
in your clojure scripts | |||||||
(ns file-kit.core (:require [clojure.java.io :as io]) (:import [java.io File]) (:import [org.apache.commons.io FileUtils] [org.clojars.fdiotalevi.guava GuavaFiles]) (:gen-class)) | |||||||
Get information about file(s)Use one of the following functions to get information about one or more files, or one directory You can refer to a file writing its path as a string or using a File object | |||||||
Returns the canonical path of the file or directory. | (defn canonical-path [path] (.getCanonicalPath (io/file path))) | ||||||
Returns true if the | (defn file? [path] (.isFile (io/file path))) | ||||||
Returns true if the | (defn directory? [path] (.isDirectory (io/file path))) | ||||||
Returns true if | (defn exists? [path] (.exists (io/file path))) | ||||||
Returns the size in bytes of | (defn size [file] (.length (io/file file))) | ||||||
Read the content of one or multiple files | (defn cat [& files] (reduce str (map slurp files))) | ||||||
Read the content of one or multiple files and split it into lines | (defn lines [& files] (seq (.split (apply cat files) "\n"))) | ||||||
Count the lines of text contained in one or multiple files | (defn wc-l [& files] (count (apply lines files))) | ||||||
List files in a directory. | (defn ls [dir] (seq (.listFiles (io/file dir)))) | ||||||
Simple implementation of grep that returns the list of lines that
contain the specified | (defn grep [pattern file] (filter #(> (.indexOf % pattern) -1) (lines file))) | ||||||
Modify filesFollowing a set of functions to write, modify or remove files and directory | |||||||
Delete file | (defn- delete-file-recursively [f & [silently]] (let [f (io/file f)] (if (.isDirectory f) (doseq [child (.listFiles f)] (delete-file-recursively child silently))) (io/delete-file f silently))) | ||||||
Create a file or update the last modified time. | (defn touch [path] (let [file (io/file path)] (do (.createNewFile file) (.setLastModified file (System/currentTimeMillis))))) | ||||||
Create a directory. | (defn mkdir [dir] (let [file-dir (io/file dir)] (do (.mkdir file-dir) file-dir))) | ||||||
Create a directory and all parent directories if they do not exist. | (defn mkdir-p [dir] (let [file-dir (io/file dir)] (.mkdirs file-dir) file-dir)) | ||||||
Remove a file. Will throw an exception if the file cannot be deleted. | (defn rm [file] (io/delete-file file)) | ||||||
Remove a file, ignoring any errors. | (defn rm-f [file] (io/delete-file file true)) | ||||||
Remove a directory. The directory must be empty; will throw an exception if it is not or if the file cannot be deleted. | (defn rm-r [path] (delete-file-recursively path)) | ||||||
Remove a directory, ignoring any errors. | (defn rm-rf [path] (delete-file-recursively path true)) | ||||||
Change file permissions in a portable way. | (defn chmod [path & {:keys [r w x]}] (let [file (io/file path)] (do (if-not (nil? r) (.setReadable file r)) (if-not (nil? w) (.setWritable file w)) (if-not (nil? x) (.setExecutable file x))))) | ||||||
Copying and moving files | |||||||
Copy a file, preserving last modified time by default. | (defn cp [from to & {:keys [preserve] :or {preserve true}}] (let [from-file (io/file from) to-file (io/file to)] (FileUtils/copyFile from-file to-file preserve))) | ||||||
Copy a directory, preserving last modified times by default. | (defn cp-r [from to & {:keys [preserve] :or {preserve true}}] (let [from-file (io/file from) to-file (io/file to)] (cond (and (file? from-file) (file? to-file)) (FileUtils/copyFile from-file to-file preserve) (and (file? from-file) (directory? to-file)) (FileUtils/copyFileToDirectory from-file to-file preserve) :default (FileUtils/copyDirectory from-file to-file (boolean preserve))))) | ||||||
Try to rename a file, or copy and delete if on another filesystem. | (defn mv [from to] (let [from-file (io/file from) to-file (io/file to)] (cond (and (file? from-file) (or (file? to-file) (not (exists? to-file)))) (FileUtils/moveFile from-file to-file) :default (FileUtils/moveToDirectory from-file to-file true)))) | ||||||
Working with temporary files or directoryOften you'll need to create temporary files or directory in your application. The following functions help with that | |||||||
Create a temporary file with prefix and suffix (extension, like .txt) | (defn create-temp-file ([] (File/createTempFile "temp" ".tmp")) ([prefix suffix] (File/createTempFile prefix suffix))) | ||||||
Create a temporary directory | (defn create-temp-dir [] (GuavaFiles/createTempDir)) | ||||||
Useful variablesSome useful variables | |||||||
(def HOME (System/getProperty "user.home")) | |||||||