Is it possible to create a directory in lua ? If so, how ?
4 Answers
There's a "system" call (or something like that, this is from memory) which you should be able to use to run an arbitrary program, which could include the mkdir command.
EDIT: I found my Programming in Lua book. On page 203, it mentions how you could use an
os.execute("mkdir " .. dirname)
to "fake" a directory creation command.
EDIT 2: Take note of Jonas Thiem's warning that this command can be abused if the directory name comes from an untrusted source!

- 66,391
- 18
- 125
- 167
-
10The Lua design philosophy is to be pure ISO C, so as to be portable to anything with a C compiler. There is no directory creation function in the C standard library. This is left up to platform-specific extensions, like mkdir(2) on POSIX systems and CreateDirectory*() on Windows. – Warren Young Nov 06 '09 at 22:42
-
Thanks ;) ! I knew I could do that kind of execute(), but I was wondering if there was a Lua alternative... I guess there isn't ;) ! – Wookai Nov 06 '09 at 22:43
-
3This response is **very dangerous**. As soon as your dirname contains ; followed by actual bash commands, you set yourself up for a nice arbitrary command execution exploit. Be very careful to escape the dirname correctly - or just skip this bad advise and use the LuaFileSystem instead to do things properly. – E. T. Aug 12 '14 at 20:32
-
2Thanks for pointing out that there's (now) a LuaFileSystem library for doing this kind of thing! I've upvoted the answer that recommends using it. But did you also rebuke the author of Lua for recommending this technique in his own book? The `os.execute()` approach is perfectly fine if the directory names come from trustworthy sources, e.g. being hardcoded in the program, and it has the advantage of not requiring any libraries beyond the Lua standard. – Carl Smotricz Aug 14 '14 at 06:41
-
Ecaping a filename correctly is not trivial, and even if the Lua author recommends it, I would advise against it. Better use a proper extension library. It works, yes, but it isn't safe except when you're very familiar with possible exploits (and there are a lot of characters that might seem regular in a file name, but will do something in the bash and suddenly introduce arbitrary command execution into your simple directory creation), or if the filename is hardcoded. If the author hasn't added such a strong security advise him/herself, then I'd say that section in the book should be revised. – E. T. Aug 21 '14 at 02:20
You may find the LuaFileSystem library useful. It has a mkdir function.
require "lfs"
lfs.mkdir("/path/to/dir")

- 497
- 5
- 16

- 2,622
- 1
- 17
- 15
-
Thanks for the link ! I can't user other libraires for the moment, so I'll stick with the os.execute() version, but I'll keep LuaFileSystem in mind for next time ! – Wookai Nov 09 '09 at 20:43
You may also want to look at Lua/APR, the Apache Portable Runtime binding for Lua. The docs can be found at here
One of the reasons I use Lua is that I can write code that runs across multiple OSes. I was using LFS for some time, but have found that using Lua/APR provides a more platform-neutral library. And there are lots of other useful routines in the APR.

- 51
- 1