Skip to content

Instantly share code, notes, and snippets.

@TheLinx
Created April 8, 2011 09:19
Show Gist options
  • Save TheLinx/909539 to your computer and use it in GitHub Desktop.
Save TheLinx/909539 to your computer and use it in GitHub Desktop.
Remove every other file
#!/usr/bin/env lua
-- does what it says on the tin
-- note that this removes files and directories indiscriminately, unlike the python script
-- also, no special options or anything like that
-- this is as small as it gets
require"lfs"
t = {}
for n in lfs.dir"." do
if n:sub(1,1) ~= "." then
t[#t+1] = n
end
end
table.sort(t)
for n=1,#t do
if n%2 == 1 then
os.remove(t[n])
end
end
#!/usr/bin/env python
import getopt, os, sys
def gen_remove_list():
files = os.listdir(".")
files.sort()
remove = False
for file in files[:]:
if os.path.isfile(file):
if remove:
files.remove(file)
remove = not remove
else:
files.remove(file)
return files
def usage():
print """Usage: rmevoth.py [OPTIONS]
Remove every other file.
-h, --help Print this usage information
-v, --verbose Extra verbosity
--dry-run Don't actually remove anything"""
def main():
try:
opts, args = getopt.getopt(sys.argv[1:], "hv", ["help", "verbose", "dry-run"])
except getopt.GetoptError, err:
print str(err)
usage()
sys.exit(2)
verbose = False
dryrun = False
for o, a in opts:
if o == "-v":
verbose = True
elif o in ("-h", "--help"):
usage()
sys.exit()
elif o == "--dry-run":
dryrun = True
else:
assert False, "unhandled option"
files = gen_remove_list()
for file in files:
if not dryrun:
os.remove(file)
if verbose:
print("removed `%s'" % file)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment