Lua File System (LFS)

Lua is a lightweight scripting language, but it lacks built-in support for file system operations beyond basic file I/O. That’s where LuaFileSystem (LFS) comes in. LFS extends Lua with functions for dealing with directories, file attributes, and time stamps, making it a powerful tool for file-based scripting and automation.


What is LuaFileSystem?

LuaFileSystem (LFS) is a Lua library that provides a portable way to access the underlying operating system’s directory structure and file attributes. It is commonly used in Lua applications that require interaction with the file system, such as:

  • Creating or removing directories
  • Scanning directories
  • Getting file metadata (size, permissions, timestamps)
  • Navigating the filesystem programmatically

Key Features

1. Directory Iteration

for file in lfs.dir("/path/to/dir") do
  print(file)
end

This will print all entries in the directory, including "." and "..".

2. Getting File Attributes

local attr = lfs.attributes("myfile.txt")

for k, v in pairs(attr) do
  print(k, v)
end

Typical attributes include:

  • mode (file, directory, etc.)
  • size
  • modification (last modified timestamp)
  • permissions
  • uid, gid, ino, dev, etc. (platform-dependent)

3. Checking File Type

local mode = lfs.attributes("myfile.txt", "mode")
if mode == "file" then
  print("This is a file.")
elseif mode == "directory" then
  print("This is a directory.")
end

4. Creating and Removing Directories

lfs.mkdir("my_new_dir")
lfs.rmdir("my_new_dir")
  • mkdir creates a directory.
  • rmdir removes an empty directory.

5. Getting and Setting Working Directory

print("Current directory:", lfs.currentdir())

lfs.chdir("..") -- change to parent directory

print("Changed directory:", lfs.currentdir())

6. Getting File Modification Time

local mtime = lfs.attributes("myfile.txt", "modification")
print("Last modified:", os.date("%c", mtime))

This is useful for tasks like backup scripts, file watchers, etc.


Permissions

The permissions attribute returns a string like "rwxr-xr--", representing standard Unix-style file permissions (if supported by the OS).


Example: Recursive Directory Scanner

local function scan_dir(path)
  for file in lfs.dir(path) do
    if file ~= "." and file ~= ".." then
      local fullpath = path .. "/" .. file
      local attr = lfs.attributes(fullpath)
      if attr.mode == "directory" then
        print("Dir:", fullpath)
        scan_dir(fullpath)
      else
        print("File:", fullpath)
      end
    end
  end
end

scan_dir(".")

Limitations

  • LFS does not provide file copying/moving functions — you’d need to use OS-specific commands or Lua bindings to os.execute().
  • Symlink handling is platform-dependent.
  • Windows support is solid, but not as rich as Unix-like systems in terms of file metadata.

When to Use LFS

  • Writing cross-platform file utilities
  • Automating directory structure creation
  • Building Lua-based file explorers or development tools
  • Accessing file metadata for custom tools or games

Documentation


Summary

LuaFileSystem brings essential file system operations to Lua, making the language more suitable for scripting tasks, configuration tools, game engines, and automation. While it’s minimalistic by design, its integration with Lua is smooth and effective.