Module: lib-tde.file

General functions to manipulate your file and filesystems.

This module exposes an API to communicate with the filesystem.

Often you need to check what the environment is where you are running in. This often involves checking what files exist for example in /proc or /sys

Checking files is as easy as:

lib-tde.file.exists("/etc/passwd") -- returns true on TOS
lib-tde.file.exists("/this/does/not/exist") -- returns false

You can also do the same for directories

lib-tde.file.dir_exists(os.getenv("HOME") .. "/.config") -- returns true on systems with default XDG_CONFIG_DIR support
lib-tde.file.dir_exists("/this/does/not/exist/") -- returns false

You can also readout the data that is inside the a file (into a string) Imagine we have a file in the home directory called file.txt with the content abcdefg

lib-tde.file.string(os.getenv("HOME") .. "/file.txt") -- returns abcdefg

Lastly we provide a function to read the file in as a table of lines Imagine we have a file in the home directory called file.txt with the following content

line one
line two
line three

Then we can execute the following function to put it into a table of lines

lib-tde.file.lines(os.getenv("HOME") .. "/file.txt") -- returns {1: "line one", 2: "line two", 3: "line three"}

Info:

  • Copyright: 2020 Tom Meyers
  • Author: Tom Meyers

Static module functions

lib-tde.file.exists (file) -> bool Check if a file exists
lib-tde.file.basename (string) Function equivalent to basename in POSIX systems
lib-tde.file.dirname (string) Function equivalent to dirname in POSIX systems
lib-tde.file.dir_exists (dir) -> bool Check if a directory exists
lib-tde.file.dir_create (path) Recursively create a directory
lib-tde.file.overwrite (file, data) -> bool Override the entire file
lib-tde.file.write (file, data) -> bool Write data to a new file
lib-tde.file.lines (file[, match[, head]]) -> table Get all lines from a file, returns an empty table if it doesn't exist
lib-tde.file.string (file[, match[, head]]) -> string Put the content of a file into a string
lib-tde.file.list_dir (dir) -> table Return a table of filename found in a directory
lib-tde.file.list_dir_full (dir) -> table Return a table of filename found in a directory and all child directories (be careful for big directories)


Static module functions

lib-tde.file.exists (file) -> bool
Check if a file exists

Parameters:

  • file string The path to the file, can be both absolute or relative.

Returns:

    bool True if a file exists false otherwise

Usage:

    -- This true
     lib-tde.file.exists("/etc/passwd")
lib-tde.file.basename (string)
Function equivalent to basename in POSIX systems

Parameters:

  • string str the path as a string

Usage:

    -- This returns file.txt
     lib-tde.file.basename("/sys/class/power_supply/BAT0/file.txt")
lib-tde.file.dirname (string)
Function equivalent to dirname in POSIX systems

Parameters:

  • string str the path as a string

Usage:

    -- This returns /sys/class/power_supply/BAT0
     lib-tde.file.dirname("/sys/class/power_supply/BAT0/file.txt")
lib-tde.file.dir_exists (dir) -> bool
Check if a directory exists

Parameters:

  • dir string The path to the directory, can be both absolute or relative.

Returns:

    bool True if the directory exists false otherwise

Usage:

    -- This true
     lib-tde.file.dir_exists("/etc")
lib-tde.file.dir_create (path)
Recursively create a directory

Parameters:

  • path string The path to create

Usage:

    lib-tde.file.dir_create(os.getenv("HOME") .. "/.cache/tde/some_dir")
lib-tde.file.overwrite (file, data) -> bool
Override the entire file

Parameters:

  • file string The path to the file, can be both absolute or relative.
  • data string The data to put into the file

Returns:

    bool if the write was successful

Usage:

    -- This true
     lib-tde.file.overwrite("hallo.txt", "this is content in the file")
lib-tde.file.write (file, data) -> bool
Write data to a new file

Parameters:

  • file string The path to the file, can be both absolute or relative.
  • data string The data to put into the file

Returns:

    bool if the write was successful

Usage:

    -- This true
     lib-tde.file.write("hallo.txt", "this is content in the file")
lib-tde.file.lines (file[, match[, head]]) -> table
Get all lines from a file, returns an empty table if it doesn't exist

Parameters:

  • file string The path to the file, can be both absolute or relative.
  • match string A regular expression to filter out lines that should be ignored by default = ^.*$ (optional)
  • head number Acts like the head unix tool, it returns the first lines in a file (optional)

Returns:

    table The lines in a file

Usage:

    -- This gives the first 100 lines of /etc/hosts and filters it by only showing lines that start with ipv4 addresses
     lib-tde.file.lines("/etc/hosts", "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}.*", 100) -> is of type table
lib-tde.file.string (file[, match[, head]]) -> string
Put the content of a file into a string

Parameters:

  • file string The path to the file, can be both absolute or relative.
  • match string A regular expression to filter out lines that should be ignored by default = ^.*$ (optional)
  • head string Acts like the head unix tool, it returns the first lines in a file (optional)

Returns:

    string The filtered out content of a file

Usage:

    -- This gives the first 100 lines of /etc/hosts and filters it by only showing lines that start with ipv4 addresses
     lib-tde.file.string("/etc/hosts", "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}.*", 100) -> is of type string
lib-tde.file.list_dir (dir) -> table
Return a table of filename found in a directory

Parameters:

  • dir string The path to the directory, can be both absolute or relative.

Returns:

    table an iterable table containing all files in the directory

Usage:

    -- This returns a table
     lib-tde.file.list_dir("/etc") -> {1:"/etc/passwd", 2:"/etc/shadow", 3:"/etc/hosts", ...}
lib-tde.file.list_dir_full (dir) -> table
Return a table of filename found in a directory and all child directories (be careful for big directories)

Parameters:

  • dir string The path to the directory, can be both absolute or relative.

Returns:

    table an iterable table containing all files in the directory

Usage:

    -- This returns a table
     lib-tde.file.list_dir_full("/etc") -> {1:"/etc/passwd", 2:"/etc/ssh/sshd_config", 3:"/etc/pacman/pacman.conf", ...}
generated by LDoc 1.4.6 Build with 💜 By F0xedb Last updated 2021-05-21 03:30:15