Search code examples
luafile-attributes

How to check if a file is executable in Lua?


How can I check in Lua if a string is the path to an executable file? It seems that neither the standard library nor, surprisingly, LuaFileSystem provides a way to do this.


Solution

  • LuaFileSystem has the lfs.attributes() function which returns a table. That, rather perversely, has a key named "mode" which contains a string describing the "type" of node (file, directory, socket, etc).

    Although it's not listed in the manual at: http://keplerproject.github.io/luafilesystem/manual.html ... which seems to be the canonical reference for that module ... there is also a 'permissions' key in that table. I think you could parse it for any "x" characters.

    I discovered this with:

    #!lua 
    local lfs = require 'lfs'
    attr = lfs.attributes('./some_file')
    for name, value in pairs(attr) do
        print (name,value)
        end