empty-dirs

Find empty directories
git clone git://bvnf.space/empty-dirs.git
Log | Files | Refs | README

empty-dirs.lua (734B)


      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
#!/usr/bin/env lua
local sys_stat = require "posix.sys.stat"
local dirent = require "posix.dirent"

local function recurse(path)
	local i = 0

	for f in dirent.files(path) do
		if f ~= "." and f ~= ".." then
			local fp = path .. "/" .. f
			i = i + 1

			local sb = sys_stat.stat(fp)
			if sb and sys_stat.S_ISDIR(sb.st_mode) ~= 0 then
				recurse(fp)
			end
		end
	end

	if i == 0 then
		print(path)
	end
end

local function main(arg)
	if #arg == 0 then
		print("usage: " .. arg[0] .. " dir...")
		os.exit(1)
	end

	for _,path in ipairs(arg) do
		local sb = sys_stat.stat(path)
		if not sb or sys_stat.S_ISDIR(sb.st_mode) == 0 then
			io.stderr:write(path .. ": not a directory\n")
		else
			recurse(path)
		end
	end
end

main(arg)