Skip to content

Instantly share code, notes, and snippets.

@EuRoXy
Last active November 13, 2020 13:24
Show Gist options
  • Save EuRoXy/20149822c9301cb165661409eb81b174 to your computer and use it in GitHub Desktop.
Save EuRoXy/20149822c9301cb165661409eb81b174 to your computer and use it in GitHub Desktop.
sys funcs in julia

I/O

open("xy.txt", "w") do io
    for line in eachline(io)
        write(io, "EuRoXy")
        print(io,
            """
            Eu
            Ro
            Xy
            """)
    end
end
print(read("xy.txt", String))
show(readlines("xy.txt"))
open(f -> read(f, String), "xy.txt")
seek(f, 0) # return to beginning of file
show(read(f))
close(f)

File Sys

rm(f) # recursive=true to rm dir, #force=true => a non-existing path is not treated as error
cd()
mkdir()
mkpath()
symlink()
chown()
chmod() # arg in octal e.g. 0o644
mv()
touch()

for (file, target) in zip(srcfiles, dest)
    cp(file, target)
end
currdir = pwd()
xy = joinpath(currdir, "xy.txt")

splitdir(currdir)
splitpath(currdir)
splitext("xy.txt")
isdir(xy)
isfile(xy)

islink()
isfifo()
walkdir()

[path for path in readdir() if occursin("txt", path)]
filter(p -> !startswith(p, "."), readdir())

Process

function hardlink(oldpath, newpath)
    # calling:  int link(char *oldpath, char *newpath)
    ret_code = ccall(:link, Int32, (Cstring, Cstring), oldpath, newpath)
    # handle system errors from libc:
    systemerror("linking $oldpath -> $newpath", ret_code != 0)
end

fnam = "xy.txt"
run(Cmd(`ls -lh $fnam`, ignorestatus=true))

words = ["Eu", "Ro", "Xy"]
nums = 1:3
`$words$nums`
readlines(`ls`)

# regex
[fn for fn in filenames if occursin(r".git/objects/.*[abcde]{4}", fn)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment