-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdelete_by_percentage.lua
More file actions
executable file
·109 lines (91 loc) · 2.86 KB
/
delete_by_percentage.lua
File metadata and controls
executable file
·109 lines (91 loc) · 2.86 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env lua
---
-- @author Gianluca Fiore
-- @copyright 2011-2020, Gianluca Fiore <forod.g@gmail.com>
--
local lfs = require("lfs")
local DIR = arg[1]
local PERCENTAGE = arg[2]
-- declare the table to contain the numeric index of files to keep
local NUMERIC_KEEPTABLE = {}
-- and the table to actually contain the filenames to keep
local KEEPTABLE = {}
-- commented because #FILETABLE will suffice when using lfs
--local FILECOUNT = string.gsub(io.popen("ls -A " .. DIR .. " | wc -l"):read("*a"), "\n", "")
-- this is commented because it's not needed with lfs
--local FILELIST = { io.popen("ls -1 " .. DIR):read("*a") }
---Generate a table with unique random integers
--@param n the number of integers to generate
--@param min the lowest number that can be randomly generated
--@param max the highest number that can be randomly generated
function unique_rand(n, min, max)
local res, buf = {}, {}
local range = max - min + 1
assert(n <= range)
-- set random seed to current date in ms
local milliseconds = io.popen("date +%N"):read("*a")
math.randomseed(milliseconds)
for i=1,n do
local r = math.random(range) - 1
range = range - 1
res[i] = (buf[r] or r) + min
buf[r] = buf[range] or range
end
return res
end
---"Setify" a table, using the items as string indices and their values
--set all true
--@param t the table to transform in a set
function table.set(t)
local u = {}
for _,v in ipairs(t) do
u[v] = true
end
return u
end
---Compare a table contents with a set (table with string index and true
--values) and return a table containing only the elements not presents
--in the set
--@param s the set
--@param t a table
function table.setcompare(s, t)
local u = {}
for _,v in ipairs(t) do
if not s[v] then
table.insert(u, v)
end
end
return u
end
-- declare a table and insert all filenames in DIR
local FILETABLE = {}
for f in lfs.dir(DIR) do
if f ~= "." and f ~= ".." then
table.insert(FILETABLE, f)
end
end
-- simple version without requiring lfs. Stops working when filenames
-- contain spaces
--for w in FILELIST[1]:gmatch("%S+") do
-- table.insert(FILETABLE, w)
--end
print("How many files do we have? ", #FILETABLE)
-- calculate the number of files to keep based on the wanted percentage
local to_keep = math.ceil(#FILETABLE / 100 * PERCENTAGE)
print("The number of files to keep is ", to_keep)
NUMERIC_KEEPTABLE = unique_rand(to_keep, 1, #FILETABLE)
for _,n in ipairs(NUMERIC_KEEPTABLE) do
table.insert(KEEPTABLE, FILETABLE[n])
end
-- change into DIR
lfs.chdir(DIR)
-- Make a set out of the table of files to keep
local set_keeptable = table.set(KEEPTABLE)
-- and generate a new table comparing the complete and the keeptable
-- tables
local to_delete_table = table.setcompare(set_keeptable, FILETABLE)
-- delete every file in the to_delete_table
for _,e in ipairs(to_delete_table) do
print("Deleting " .. e)
local status = os.execute("rm -rf " .. e)
end