I am starting to learn Babashka in order to learn (supercedes Clojure Shell)
the way we have (supercedes Clojure [Javascript Python SQL CSS HTML])
. The examples show cool stuff with some advanced needs, but to get started I just want to replace my every-day shell script. In particular I’m looking for replacements to mv
, rm
, and cp
. I don’t want to do this with clojure.java.shell
; in that case, I would just write the bash script. Can I do it in an OS-agnostic way in Babashka?
To replace cp, I worked out this one-liner, which seems to be ok for text files, but it fails with non-text files, as well as likely efficiency problems.
bb '(->> "whatev.txt" slurp (spit "whatev2.txt"))'
You can use clojure.java.io
for this, although java.nio
might have better functions for it nowadays.
$ echo "babashka" > /tmp/a
$ bb '(io/copy (io/file "/tmp/a") (io/file "/tmp/b"))'
$ cat /tmp/b
babashka
Awesome! I can adjust a recursive version (cp -r
) too. What about bash mv
? Is there a way without doing the copy and adding a delete?
I actually had to look that one up, but it seems you can do it this way:
bb '(.renameTo (io/file "/tmp/a") (io/file "/tmp/moved"))'
See:
Babashka also supports java.nio
:
$ bb '(java.nio.file.Files/move (.toPath (io/file "/tmp/moved")) (.toPath (io/file "/tmp/a")) (into-array [java.nio.file.StandardCopyOption/REPLACE_EXISTING]))'
Thank you, and extra plus for demonstrating how to include java.nio methods
With the tragic oversight I’ve come to expect from Java, there doesn’t appear to be a one-liner for recursive copy to match the recursive move.
I wouldn’t go as far as to call it a tragic oversight. “Recursive move” is not recursive!
I should have called it “deep move,” with no similar “deep copy”
I wrote a thing but never quite finished it, you might find it helpful for a lot of these things:
So here’s copying recursively:
Here’s move:
and rename:
Awesome, @nate ! Great suggestions. These might deserve reference on Babashka documentation!
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.