Convert ls output into csv Convert ls output into csv unix unix

Convert ls output into csv


It's a bit long to type in at the command-line, but it properly preserves spaces in the filename (and quotes it, too!)

find . -ls | python -c 'import sysfor line in sys.stdin:    r = line.strip("\n").split(None, 10)    fn = r.pop()    print ",".join(r) + ",\"" + fn.replace("\"", "\"\"") + "\""'


If you don't care about the spaces in the date:

$ find . -ls | tr -s ' ' ,

If you do care about those spaces:

$ find . -ls | awk '{printf( "%s,%s,%s,%s,%s,%s,%s,%s %s %s,%s\n", $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11 )}'

Neither of these will work if your filenames contain any whitespace. As a hack to deal with spaces in the filename, you could try:

 ... | sed 's/,/ /8g'

to get rid of all but the first 8 commas (assuming your sed supports the nonstandard 8g option as gnu sed does). Of course this won't deal with commas in the filename.


And yet another variant. See section "-printf format" in man page of find to customize.

$ find . -type f -fprintf /tmp/files.txt "%i,%b,%M,%n,%u,%g,%s,%CY-%Cm-%Cd %CT,%p\n"

Example output:

$ less /tmp/files.txt3414558,40,-rw-rw-r--,1,webwurst,webwurst,16542,2014-09-18 15:54:36.9232917780,./dump_info.py3414559,8,-rw-rw-r--,1,webwurst,webwurst,68,2014-09-18 15:54:51.1752922580,./metadata