Skip to content

Instantly share code, notes, and snippets.

@StephenMcGill-TRI
Created July 26, 2016 15:35
Show Gist options
  • Select an option

  • Save StephenMcGill-TRI/85f8deae8f5c9816a0461fa55db09248 to your computer and use it in GitHub Desktop.

Select an option

Save StephenMcGill-TRI/85f8deae8f5c9816a0461fa55db09248 to your computer and use it in GitHub Desktop.
Analyze a directory by file type, in terms of file size
#!/usr/bin/env luajit
-- Usage: ./sum.lua [DIRECTORY]
-- NOTE: The directory is not safely escaped
local dir = arg[1] or "."
-- Command gathered from: http://stackoverflow.com/questions/28376003
local f = io.popen('find '..dir..[[ -type f -print0 | xargs -0 du -k | grep "\.[a-zA-Z]*$" | rev | sed -e "s/\..*\t/\t/g" | rev]])
local ftype2ksz = {}
local ftype2n = {}
for line in f:lines() do
local ksz, ftype = line:match("(%d+)%s+(%w+)")
--print(line, ftype, ksz)
ftype2ksz[ftype] = (ftype2ksz[ftype] or 0) + assert(tonumber(ksz), "Bad filesize: "..line)
ftype2n[ftype] = (ftype2n[ftype] or 0) + 1
end
f:close()
-- Find the average file sizes
local fprops = {}
for ftype, ksz in pairs(ftype2ksz) do
-- filetype, total kilobytes, number of files, average file size
table.insert(fprops, {ftype, ksz, ftype2n[ftype], ksz/ftype2n[ftype]})
end
-- Add analysis of fprops here
table.sort(fprops, function(a,b) return a[4]>b[4] end)
for i, props in ipairs(fprops) do
local str = string.format("%s,%d,%d,%d\n", unpack(props))
io.write(str)
end
io.flush()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment