This reference is based on the cppreference.com filesystem documentation. Cppreference provides a more detailed explanation regarding the implementation and edge cases of functions that are made available to Lua by this module.
The names of the functions and objects used by the API of this Lua modules are equivalent to the functions, enums and objects as specified for the C++ std::filesystem
library.
absolute
canonical
copy
copy_file
copy_symlink
copy_options (enum)
create_directory
create_directories
create_directory_symlink
create_symlink
current_path
directory (none std::filesystem)
directory_entry (constructor)
directory_entry:assign
directory_entry:replace_filename
directory_entry:refresh
directory_entry:path
directory_entry:exists
directory_entry:is_block_file
directory_entry:is_character_file
directory_entry:is_directory
directory_entry:is_fifo
directory_entry:is_other
directory_entry:is_regular_file
directory_entry:is_socket
directory_entry:is_symlink
directory_entry:file_size
directory_entry:hard_link_count
directory_entry:last_write_time
directory_entry:status
directory_entry:symlink_status
directory_options (enum)
exists
equivalent
file_size
file_time (object)
file_time_duration (object)
file_time_duration:seconds
file_time_now (none std::filesystem)
file_type (enum)
hard_link_count
is_block_file
is_character_file
is_directory
is_empty
is_fifo
is_other
is_regular_file
is_socket
is_symlink
last_write_time
permissions
perms (enum)
perm_options (enum)
path (constructor)
path:append
path:clear
path:compare
path:concat
path:elements
path:empty
path:extension
path:filename
path:has_extension
path:has_filename
path:has_parent_path
path:has_relative_path
path:has_root_directory
path:has_root_name
path:has_root_path
path:has_stem
path:is_absolute
path:is_relative
path:lexically_normal
path:lexically_proximate
path:lexically_relative
path:make_preferred
path:parent_path
path:relative_path
path:remove_filename
path:root_directory
path:root_name
path:root_path
path:replace_extension
path:replace_filename
path:stem
proximate
read_symlink
recursive_directory (none std::filesystem)
recursive_directory_iterator_state (object, none std::filesystem)
recursive_directory_iterator_state:depth
recursive_directory_iterator_state:disable_recursion_pending
recursive_directory_iterator_state:options
recursive_directory_iterator_state:recursion_pending
recursive_directory_iterator_state:pop
relative
remove
remove_all
rename
resize_file
space
status
status_known
symlink_status
temp_directory_path
weakly_canonical
Returns a path object with a absolute reference to the same file system location as p
.
Converts path p
to a canonical absolute path, i.e. an absolute path that has no dot, dot-dot elements or symbolic links in its generic format representation.
If p
is not an absolute path, the function behaves as if it is first made absolute by absolute
function.
The path p
must exist.
Copies the file or directory from
to file or directory to
, using the options
indicated by copy_options
.
Copies a single file from from
to to
, using the options
indicated by copy_options
.
Copies a symlink to another location.
copy_options
is an enumeration with constants which are used to control the behavior of the copy
and copy_file
functions.
Its members support binary operators to combine, mask or check the options.
You can combine only one option from each option group below.
For example the result a copy with the options skip_existing
and overwrite_existing
combined is undefined but overwrite_existing
and skip_symlinks
is valid.
Options controlling copy_file
when the file already exists
Option | Meaning |
---|---|
none |
Report an error (default behavior) |
skip_existing |
Keep the existing file, without reporting an error |
overwrite_existing |
Replace the existing file |
update_existing |
Replace the existing file only if it is older than the file being copied |
Options controlling the effects of copy
on subdirectories
Option | Meaning |
---|---|
none |
Skip subdirectories (default behavior) |
recursive |
Recursively copy subdirectories and their content |
Options controlling the effects of copy
on symbolic links
Option | Meaning |
---|---|
none |
Follow symlinks (default behavior) |
copy_symlinks |
Copy symlinks as symlinks, not as the files they point to |
skip_symlinks |
Ignore symlinks |
Options controlling the kind of copying copy
does
Option | Meaning |
---|---|
none |
Copy file content (default behavior) |
directories_only |
Copy the directory structure, but do not copy any non-directory files |
create_symlinks |
Instead of creating copies of files, create symlinks pointing to the originals. Note: the source path must be an absolute path unless the destination path is in the current directory. |
create_hard_links |
Instead of creating copies of files, create hardlinks that resolve to the same files as the originals |
Creates a directory p
.
The attributes of the new directory are copied from optional existing
directory which must be a valid path.
The new directory will have the perms.all
attributes set when existing
is omitted.
Returns a boolean to indicate if a directory was created.
Creates a directories for every element p
that does not already esist.
Returns a boolean to indicate if directories were created.
Creates a symbolic link link
with its target set to target
as if by POSIX symlink(): the pathname target may be invalid or non-existing.
Some operating systems require symlink creation to identify that the link is to a directory.
Portable code should use this function to create directory symlinks rather than create_symlink.
Creates a symbolic link link
with its target set to target
as if by POSIX symlink()
: the pathname target may be invalid or non-existing.
Returns the current path when called without p
.
When called with p
, p
is set as the current path.
Enables iteration over entries in a directory by using a generic for-loop.
The default for directory_options
is fs.directory_options.none
.
local fs = require( filesystem )
for entry in fs.directory( "my_directory" ) do
print( entry )
end
entry
is a directory_entry
object.
See also the recursive_directory
function.
Creates a new directory_entry
object from p
.
A directory_entry
hold (cached) information of an entry in the filesystem.
A default empty directory_entry object is created when called without parameters.
Assigns p
as the contents to the directory_entry
object.
Replaces the filename with extention by p
of the directory_entry
object and calls refresh to update the cached attributes.
Update the cached status of the filesystem entry to which the directory_entry
is referring to.
Returns a path object to the entry it refers to.
Tests if the directory_entry
refers an existing entry on the filesystem.
Tests if directory_entry
refers to block device.
Tests if directory_entry
refers to a character device.
Tests if directory_entry
refers to a directory.
Tests if directory_entry
refers to a named pipe.
Tests if directory_entry
refers to an other file.
Tests if directory_entry
refers to a regular file.
Tests if directory_entry
refers to a named IPC socket.
Tests if directory_entry
refers to a symbolic link.
Returns the file size of the file to which directory_entry
refers.
Returns the number of hard links referring to the file to which directory_entry
refers.
Returns the (cached) time of the last data modification of the file to which directory_entry
refers.
Returns a permissions
and a file type
of the file to which directory_entry
refers.
Returns a permissions
and a file type
of the symbolic link to which directory_entry
refers.
Enables iteration over the entries in a directory by using a generic for-loop.
The default for directory_options
is fs.directory_options.none
.
p
can be a path object or a string.
local fs = require( filesystem )
for entry in fs.directory( "my_directory", fs.directory_options.skip_permission_denied ) do
print( entry:path():filename() )
end
In this example entry
is a directory_entry
object.
directory_options
has members that are constants which are used to control the behavior of the directory
and recursive_directory
functions.
Its members support binary operators to combine, mask or check the options.
Option | Meaning |
---|---|
none |
Skip directory symlinks, permission denied is error |
follow_directory_symlink |
Follow rather than skip directory symlinks |
skip_permission_denied |
Skip directories that would otherwise result in permission denied errors |
Checks if path p
corresponds to an existing file or directory.
Checks if the paths p1
and p2
resolve to the same file system entity.
Returns the size in bytes of the file p
(symlinks are followed).
The size of a directory (as well as any other file that is not a regular file or a symlink) is implementation-defined.
An object that holds the time modified when returned by last_write_time
or directory_entry:last_write_time
.
When returned by file_time_now
it holds the time at the moment of the function call.
The file_time
object supports limited arithmatic such as subtracting two file_time
objects to get a time difference (which is a regular file_time_duration
object).
You can also add or subtract an offset (in seconds) or a file_time_duration
to a file_time
object.
local fs = require( "filesystem" )
local p = fs.path( "my_file.txt" )
local ft = fs.last_write_time( p )
local now = fs.file_time_now()
-- calculate the duration since the file was modified
local duration = now - ft
-- set new modified time one second ago
fs.last_write_time( p, now - 1 )
An object that is created when subtracting one file_time
objects from another.
You can do limited arithmatic with file_time_duration objects
; only adding and subtracting numbers, integeral values (both in seconds) and file_time_duration
objects is supported.
A file_time_duration
can also be added to or subtracted from a file_time
.
Returns a Lua number that represents the duration in seconds.
Returns a file_time
object with the current time.
file_type
is an enummeration with constants which are used to identify the type of the entry on the filesystem.
The supported file types are;
Type | Meaning |
---|---|
none |
Indicates that the file status has not been evaluated yet, or an error occurred when evaluating it |
not_found |
Indicates that the file was not found (this is not considered an error) |
regular |
A regular file |
directory |
A directory |
symlink |
A symbolic link |
block |
A block special file |
character |
A character special file |
fifo |
A FIFO (also known as pipe) file |
socket |
A socket file |
unknown |
The file exists but its type could not be determined |
Depending on the implementation and platform the file_type
returned by functions may hold a value that is not listed.
Returns the number of hard links for p
.
Tests if p
refers to block device.
Tests if p
refers to a character device.
Tests if p
refers to a directory.
Tests if p
refers to an empty file or directory.
Tests if p
refers to a named pipe.
Tests if p
refers to an other file.
Tests if p
refers to a regular file.
Tests if p
refers to a named IPC socket.
Tests if p
refers to a symbolic link.
Sets the the time of the last modification to new_time
for p
.
Returns the time of the last modification of p
when called without new_time
.
Changes the permissions of the entry p
refers to.
perms
are the permissions that are applied as specified by perm_options
.
By default perms
will replace the current permissions when perm_options
is not provided.
perms
is an enummeration with constants which represents file access permissions.
Its members support binary operators to combine, mask or check permissions.
Permission | Meaning |
---|---|
none |
No permission bits are set |
owner_read |
File owner has read permission |
owner_write |
File owner has write permission |
owner_exec |
File owner has execute/search permission |
owner_all |
File owner has read, write, and execute/search permissions. Equivalent to owner_read | owner_write | owner_exec . |
group_read |
The file's user group has read permission |
group_write |
The file's user group has write permission |
group_exec |
The file's user group has execute/search permission |
group_all |
The file's user group has read, write, and execute/search permissions. Equivalent to group_read | group_write | group_exec . |
others_read |
Other users have read permission |
others_write |
Other users have write permission |
others_exec |
Other users have execute/search permission |
others_all |
Other users have read, write, and execute/search permissions. Equivalent to others_read | others_write | others_exec . |
all |
Users have read, write, and execute/search permissions. Equivalent to owner_all | group_all | others_all . |
set_uid |
Set user ID to file owner user ID on execution |
set_gid |
Set group ID to file's user group ID on execution |
sticky_bit |
Implementation-defined meaning, but POSIX XSI specifies that when set on a directory, only file owners may delete files even if the directory is writeable to others (used with /tmp) |
mask |
All valid permission bits. Equivalent to all | set_uid | set_gid | sticky_bit . |
perm_options
is an enummeration with constants that control the behavior of the function permissions
.
The options support binary operators to combine, mask or check options.
Option | Meaning |
---|---|
replace |
Permissions will be completely replaced by the argument to permissions() (default behavior) |
add |
Permissions will be replaced by the bitwise OR of the argument and the current permissions |
remove |
Permissions will be replaced by the bitwise AND of the negated argument and current permissions |
nofollow |
Permissions will be changed on the symlink itself, rather than on the file it resolves to |
You can only combine replace
, add
or remove
with nofollow
or else the behavior of the permissions function is undefined.
local fs = require( "filesystem" )
local undefined_1 = fs.perm_options.replace | fs.perm_options.add
local undefined_2 = fs.perm_options.add | fs.perm_options.remove
local okay_1 = fs.perm_options.replace | fs.perm_options.nofollow
local okay_2 = fs.perm_options.remove | fs.perm_options.nofollow
Creates a new path object from p
.
A default empty path object is created when called without parameters.
p
can be another path object or a string.
Appends another path object or string p
to path
with a directory separator.
Returns the path
object itself.
Erases the contents of path
.
Compares the lexical representations of path
and p
lexicographically.
Concatenates another path object or string p
to path
.
Returns the path
object itself.
Returns a function and a state to iterate through the elements of path
Typically you will use this function with a generic for-loop, for example;
for element in your_path:elements() do
print( element )
end
Check if path
is empty.
Returns a path object with the file extension component of path
.
Returns a path object with the filename component of path
.
Checks if path:root_path
will return an emty result.
Checks if path:has_filename
will return an emty result.
Checks if path:parent_path
will return an emty result.
Checks if path:relative_path
will return an emty result.
Checks if path:root_directory
will return an emty result.
Checks if path:root_name
will return an emty result.
Checks if path:root_path
will return an emty result.
Checks if path:stem
will return an emty result.
Returns true when path
is an absolute path.
Returns true when path
is a relative path.
Retuns the normal form of path
.
Retuns the proximate form of path
.
This function calls path:lexically_relative
and if this call results in an empty path then the value path
is returned.
Returns a path that is made relative to base
.
Converts directory separators of path
to preferred directory separator.
Returns the path
object itself.
Returns a path object with the parent path of path
.
Returns a path object with a path relative to the root path.
Removes the filename component from path
.
Returns the path
object itself.
Returns a path object with the root directory of path
or a new empty path object in case path
does not include a root directory.
Returns a path object with the root name of path
or a new empty path object in case path
does not include a root name.
Returns a path object with the root path of path
or a new empty path object in case path
does not include a root name.
Replaces the extension with ext
or removes the extension when ext
is not provided.
ext
can be a path object or a string.
Returns the path
object itself.
Replaces the filename component of path
by repl
.
repl
can be a path object or a string.
Returns the path
object itself.
Returns the filename identified by the generic-format path stripped of its extension.
Returns a path which is p
that is relative to base.
Tries to resolve symlinks and normalizes with weakly_canonical
and path:lexically_proximate
for p
and base
before other processing.
Default for base
when it's not provided is the result of current_path
.
Returns a path object which refers to the target of a symbolic link at p
.
Enables recusive iteration over entries in a directory and its subdirectories by using a generic for-loop.
The default for directory_options
is fs.directory_options.none
.
local fs = require( filesystem )
for state, entry in fs.recursive_directory( "my_directory" ) do
print( state:depth() )
print( entry )
end
In this example state
is a recursive_directory_iterator_state
object that let you control the recursion.
entry
is a directory_entry
object.
See also the directory
function.
An object that controls the recursive direcotry iteration
Returns the number of directories from the starting directory to the currently iterated directory, i.e. the current depth of the directory hierarchy. The starting directory has depth of 0, its subdirectories have depth 1, etc.
Disables recursion to the currently referred subdirectory, if any.
Returns the options set at start of the directory iteration that affect the iteration.
Returns true if the next iteration will cause the directory that is currently referred will be iterated into.
Moves one level up in the directory hierarchy.
Returns a path which is p
that is relative to base.
Resolves symlinks and normalizes both p
and base
before other processing.
Default for base
when it's not provided is the result of current_path
.
Removes entity refered by p
.
Returns a boolean to indicate if the entity was deleted.
Deletes the contents of p
(if it is a directory) and the contents of all its subdirectories, recursively, then deletes p
itself.
Returns the number of entityes that were deleted.
Moves or renames the filesystem old
to new
.
Changes the size of a file refered by p
to new_size
.
If the file size was previously larger than new_size
, the remainder of the file is discarded.
If the file was previously smaller than new_size
, the file size is increased and the new area appears as if zero-filled.
Determines the information about the filesystem on which the pathname p
is located.
Returns 3 values, in order;
- Total size of the filesystem in bytes
- free space on the filesystem in bytes
- Free space available to a non-privileged process (may be equal or less than free) )
Returns the permissions
and file type
(in that order) of the filesystem entity refered by p
.
Symbolic links are followed.
Tests if the file status of p
is known.
Returns the permissions
and file type
(in that order) of the symbolic link refered by p
.
Returns the directory location suitable for temporary files.
Returns a path composed by results of calling canonical
for the leading elements of p
that exist (as determined by status
), followed by the elements of p
that do not exist.