Skip to content

Instantly share code, notes, and snippets.

@overplumbum
Created July 18, 2012 14:30
Show Gist options
  • Save overplumbum/3136517 to your computer and use it in GitHub Desktop.
Save overplumbum/3136517 to your computer and use it in GitHub Desktop.
Convert `ls -UR1` output (fastest way to get recursive listing) to plain file listing
#!/usr/bin/env python
import sys
import argparse
def self_check():
from subprocess import check_output, check_call
from StringIO import StringIO
ls_r = StringIO(check_output(['ls', '-1RU']))
parse(ls_r, open('self_check_output.txt', 'w'))
check_call(['sort', 'self_check_output.txt', '-o', 'self_check_output.txt'])
find_output = check_output(r'find . -mindepth 1 | sort > self_check_reference.txt', shell=True)
check_call(['diff', '-u', 'self_check_reference.txt', 'self_check_output.txt'])
print 'passed'
def parse(input, output):
lines = iter(input)
root = './'
try:
while True:
line = next(lines).rstrip()
if len(line):
output.write(root + line + '\n')
else:
line = next(lines).rstrip()
assert line[-1] == ':'
root = line[:-1] + '/'
except StopIteration:
pass
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--self-check', action='store_true')
args = parser.parse_args()
if args.self_check:
self_check()
else:
parse(sys.stdin, sys.stdout)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment