Module fs

Files, directories and file system manipulation.

Functions

basename (path) Returns the last component of the given path, removing any trailing '/' characters.
copy (source, target) Copies the contents of the file source to the file target.
dirname (path) Returns the parent directory of the pathn given.
exists (path) Returns true if the given pathname exists in the file system.
isdirectory (path) Returns true if the given path specifies a directory.
isfile (path) Returns true if the given path specifies a file.
mkdir (dir[, recursive]) Creates a new directory.
move (src, dest) Moves the item at path src to path dest.
remove (dir) Removes a file or directory.
rmdir (dir) Removes an empty directory.
workdir ([dir]) Returns or sets the current working directory.


Functions

basename (path)
Returns the last component of the given path, removing any trailing '/' characters. If the given path consists entirely of '/' characters, the string "/" is returned. If path is an empty string, the string "." is returned.

This is purely a string manipulation function and depends on no outside state.

Parameters:

  • path string The path to process.

Usage:

    print("name of script file is " .. fs.basename(arg[0]))
    assert(fs.basename("/etc/fstab") == "fstab")
copy (source, target)
Copies the contents of the file source to the file target. target will be overwritten if it already exists.

Parameters:

  • source string The source file to copy.
  • target string The destination file.

Usage:

    io.output("hello"):write("hello world")
    fs.copy("hello", "world")
    assert(io.input("world"):read("a") == "hello world")
dirname (path)
Returns the parent directory of the pathn given. Any trailing '/' characters are not counted as part of the directory name. If the given path is an empty string or contains no '/' characters, the string "." is returned, signifying the current directory.

This is purely a string manipulation function and depends on no outside state.

Parameters:

  • path string The path to process.

Usage:

    assert(fs.dirname("/usr/local/bin") == "/usr/local")
exists (path)
Returns true if the given pathname exists in the file system.

Parameters:

  • path string The path of the file to look for.

Usage:

    if fs.exists("/etc/fstab") then
    	-- ...
    end
isdirectory (path)
Returns true if the given path specifies a directory. Will return false if either the given path does not specify a directory or the path does not exist at all.

On error returns nil, an error message and a platform-dependent error code.

Parameters:

  • path string The path to check.

Usage:

    if not fs.isdirectory("/usr") then
        print("something's wrong")
    end
isfile (path)
Returns true if the given path specifies a file. Will return false if either the given path does not specify a directory or the path does not exist at all.

On error returns nil, an error message and a platform-dependent error code.

Parameters:

  • path string The path to check.

Usage:

    if not fs.isfile("/sbin/init") then
        print("something's wrong")
    end
mkdir (dir[, recursive])
Creates a new directory.

If recursive is true, creates intermediate directories (parent directories) as required; behaves as POSIX mkdir -p would.

On success, returns true. Otherwise returns nil, an error message and a platform-dependent error code.

Parameters:

  • dir string The path of the directory to create.
  • recursive boolean Whether to create intermediate directories. (optional)

Usage:

    fs.mkdir("/usr/local/bin")
move (src, dest)
Moves the item at path src to path dest. If dest exists, it is overwritten. Both src and dest must be of the same type (that is, both directories or both non-directories) and must reside on the same file system.

(If you need to move files across different file systems, consider using fs.copy instead.)

On success, returns true. Otherwise returns nil, an error message and a platform-dependent error code.

Parameters:

  • src string The pathname of the file to move.
  • dest string The destination pathname.

Usage:

    fs.move("srcfile", "destfile")
remove (dir)
Removes a file or directory.

On success, returns true. Otherwise returns nil, an error message and a platform-dependent error code.

Parameters:

  • dir string The path to remove.

Usage:

    fs.remove("path/to/file")
rmdir (dir)
Removes an empty directory.

On success, returns true. Otherwise returns nil, an error message and a platform-dependent error code.

Parameters:

  • dir string The path of the directory to remove.

Usage:

    fs.rmdir("yourdirectory")
workdir ([dir])
Returns or sets the current working directory.

If dir is nil, returns the current working directory. Otherwise, sets the current working directory to dir.

On success, returns true. Otherwise returns nil, an error message and a platform-dependent error code.

Parameters:

  • dir string The directory to set as the current working directory. (optional)

Usage:

    -- Store the current working directory in a variable:
    local wd = fs.workdir()
    -- Set the current working directory to the value of
    -- the HOME environment variable:
    fs.workdir(environ["HOME"])
generated by LDoc 1.4.6 Last updated 2023-12-27 20:06:03