Radpath

Elixir library for path operations inspired by pathlib from Python.

Summary

dirs(path, regex_dir \\ ".+")

Returns all of the directories in the given path

ensure(path, is_file \\ false)

Ensures that a directory/file is created. If is_file is set to true then file is created

files(path)

Radpath.files(path) will list down all files in the path without filtering

files(path, ext)

Returns all of the files in the given path, and if ext is given will filter by that extname

islink?(path)

Returns true if path is a symbolic link and false if otherwise:

md5sum(path)

Returns the md5sum of a file. Only works for files. Folders passed will return :error:

mktempdir()

To create a temp dir without arguments:

mktempdir(path)

To create a temp dir at a specific location:

mktempfile()

To create a temp file and write to it without arguments:

mktempfile(path)

To create a temp file at a certain location without extension and write to it: ## Arguments

mktempfile(ext, path)

To create a temp file with arguments (ext and path) and write to it. Default value for extension is '.tmp':

mv(source, destination)
parent_path(path)

To get parent_path of a given path. The path supplied should be a bit string:

relative_path(base, file)

Gives you back the relative path:

rename(source, destination)

To rename a file / directory:

sha1sum(path)

Returns the sha1sum of a file. Only works for files. Folders passed will return :error:

start(type, args)

Callback implementation of :application.start/2

symlink(source, destination)

To create symlink:

unzip(zip_file, unzip_dir \\ File.cwd!())

zip_file is the zip archive. Will only unzip if zip_file exists

zip(dirs, archive_name)

To create a zip archive:

Functions

dirs(path, regex_dir \\ ".+")

Specs:

  • dirs(bitstring, bitstring) :: list

Returns all of the directories in the given path

Arguments

  • path path to show list of directories in bitstring.
  • regex_dir String regex of directory pattern to show in final output.

Usage

iex(4)> Radpath.dirs("/home/lowks/src/elixir/radpath/lib", regex_dir)

iex(3)> Radpath.dirs(["/home/lowks/src/elixir/radpath/lib", "/home/lowks/src/elixir/radpath/_build"], regex_dir)
ensure(path, is_file \\ false)

Specs:

  • ensure(bitstring, bitstring) :: none

Ensures that a directory/file is created. If is_file is set to true then file is created.

Arguments

  • path - Path that is to be created

  • is_file - Boolean, indicating if path is a file, if true then ensure will create file. Default false (directory).

Usage

iex(1)> Radpath.ensure(path)
  :ok

iex(2)> Radpath.ensure(path, true)
  :ok
files(path)

Radpath.files(path) will list down all files in the path without filtering

Arguments

  • path location to list down all files in bitstring

Usage

Listing down all files in the "ci" folder without filtering:

 Radpath.files("ci")
 ["/Users/lowks/Projects/src/elixir/Radpath/ci/script/circleci/prepare.sh"]
files(path, ext)

Specs:

  • files(bitstring, bitstring) :: list

Returns all of the files in the given path, and if ext is given will filter by that extname.

Arguments

  • path Path of file to filter in bitstring
  • ext Extensions to show in filter in bitstring

Usage

Listing all of the contents of the folder /home/lowks/Documents

Radpath.files("/home/lowks/Documents")

Appling a filter 'doc' on that search:

Radpath.files("/home/lowks/Documents", "doc")

Appling a list filter of 'doc' and 'pdf' on that search:

Radpath.files("/home/lowks/Documents", ["doc", "pdf"])

Listing for list of directories is also supported:

Radpath.files(["/home/lowks/Documents", "/tmp"], ["doc"])

or Radpath.files(["/home/lowks/Documents", "/tmp"]) or Radpath.files(['/home/lowks/Documents', '/tmp'], ['doc', 'pdf'])

Paths that do not exists returns an empty list:

iex(2)> Radpath.files("/heck/i/do/not/exist")
[]
islink?(path)

Specs:

  • islink?(bitstring) :: boolean

Returns true if path is a symbolic link and false if otherwise:

Arguments

  • path Path to be checked in bitstring

Usage

 iex(1)> Radpath.islink?("test3")
 true
 iex(2)> Radpath.islink?("/home")
 false
md5sum(path)

Specs:

  • md5sum(bitstring) :: bitstring

Returns the md5sum of a file. Only works for files. Folders passed will return :error:

Arguments

  • path Path to file to generate md5sum in bitstring

Usage

iex(2)> Radpath.md5sum("mix.exs")
"e7794b67112458774fe9f23d1b9c4913"
iex(3)> Radpath.md5sum("test")   
:error
mktempdir()

Specs:

  • mktempdir :: none

To create a temp dir without arguments:

Radpath.mktempdir
mktempdir(path)

Specs:

  • mktempdir(bitstring) :: none

To create a temp dir at a specific location:

Arguments

  • path location to create tempdir in bitstring

Usage

Radpath.mktempdir("/home/lowks/Downloads")
mktempfile()

Specs:

  • mktempfile :: none

To create a temp file and write to it without arguments:

Radpath.mktempfile |> File.write("test123")
mktempfile(path)

Specs:

  • mktempfile(bitstring) :: bitstring

To create a temp file at a certain location without extension and write to it: ## Arguments

  • path path where to create tempfile in bitstring

Usage

Radpath.mktempfile("/home/lowks/Downloads") |> File.write("abcdef")
mktempfile(ext, path)

Specs:

  • mktempfile(bitstring, bitstring) :: bitstring

To create a temp file with arguments (ext and path) and write to it. Default value for extension is '.tmp':

Arguments

  • path path where to create tempfile in bitstring
  • ext extension of newly created tempfile in bitstring

Usage

Radpath.mktempfile(".log", "/home/lowks/Downloads") |> File.write("abcdef")
mv(source, destination)
parent_path(path)

Specs:

  • parent_path(bitstring) :: bitstring
  • parent_path(list) :: none

To get parent_path of a given path. The path supplied should be a bit string:

Arguments

  • path - The path which you want parent_path returned.

Usage

 Radpath.parent_path(path).
relative_path(base, file)

Specs:

  • relative_path(bitstring, bitstring) :: none

Gives you back the relative path:

Arguments

  • file Bitstring
  • base Bitstring

Usage

iex(1)> Radpath.relative_path("/tmp/lowks/", "/tmp/lowks/iam.txt")
"iam.txt"
iex(2)> Radpath.relative_path("/tmp/lowks/", "/tmp/lowks/hoho/iam.txt")
"hoho/iam.txt"
rename(source, destination)

Specs:

  • rename(bitstring, bitstring) :: none

To rename a file / directory:

Arguments

  • source - Original name of directory / file
  • destination - New name of directory / file

Usage

Radpath.rename(source, destination)

or

Radpath.mv(source, destination)
sha1sum(path)

Specs:

  • sha1sum(bitstring) :: bitstring

Returns the sha1sum of a file. Only works for files. Folders passed will return :error:

Arguments

  • path Path to file to generate sha1sum in bitstring

Usage

iex(1)> Radpath.sha1sum("mix.exs")
"48edfd81ee32efc0f9aea7dbebd0798fb2adf226"
iex(2)> Radpath.sha1sum("test")   
:error
start(type, args)

Callback implementation of :application.start/2.

symlink(source, destination)

Specs:

  • symlink(bitstring, bitstring) :: none

To create symlink:

Arguments

  • source of file or directory in bitstring
  • destination of file or directory in bitstring

Usage

Radpath.symlink(source, destination). Source must exist.
unzip(zip_file, unzip_dir \\ File.cwd!())

Specs:

  • unzip(bitstring, bitstring) :: none

zip_file is the zip archive. Will only unzip if zip_file exists

Arguments

  • zip_file string value of file that is to be uncompressed.
  • unzip_dir string value of output directory of where to unzip.

Usage

To create a zip archive:

Radpath.unzip(zip_file, unzip_dir)
zip(dirs, archive_name)

Specs:

  • zip(bitstring, bitstring) :: none

To create a zip archive:

Arguments

  • dir: List containing all the directories to be zipped
  • archive_name: String which is the name of the archive to be created

Usage

Radpath.zip(dir1, archive_name)