Skip to main content

Remove the last path component (file.path.remove_last_component)

Declaration​

new_path, err = file.path.remove_last_component(path)

Parameters​

  • path
    String. The original path string.

Returns​

  • new_path
    String or nil. Returns the path string with its last component removed on success, or nil if the path is invalid.
  • err
    String. Returns "Invalid path" if the path is invalid.

Description​

Remove the last path component and return the new path.
For /usr, return /; for the root directory /, return / unchanged.
This function only processes the path string; it does not delete any actual file or directory.
This function is available only in versions released after 20250914.

Example​

local parent = file.path.remove_last_component("/usr/local/bin") -- Returns "/usr/local"
local root = file.path.remove_last_component("/usr") -- Returns "/"
local unchanged = file.path.remove_last_component("/") -- Returns "/"