Convert ls output into csv

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.


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 sys
for line in sys.stdin:
    r = line.strip("\n").split(None, 10)
    fn = r.pop()
    print ",".join(r) + ",\"" + fn.replace("\"", "\"\"") + "\""
'

Tags:

Linux

Unix

Csv

Find