Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert ls output into csv

Tags:

linux

find

unix

csv

How would I convert:

$ find . -ls > /tmp/files.txt

Which gives me something like:

908715       40 -rwxrwxr-x    1 david            staff               16542 Nov 15 14:12 ./dump_info.py
908723        0 drwxr-xr-x    2 david            staff                  68 Nov 20 17:35 ./metadata

Into a csv output? It would look like:

908715,40,-rwxrwxr-x,1,david,staff,16542,Nov 15 14:12,./dump_info.py
908723,0,drwxr-xr-x,2,david,staff,68,Nov 20 17:35,./metadata

Here is an example title with spaces in the filename:

652640,80,-rw-rw-r--,1,david,staff,40036,Nov,6,15:32,./v_all_titles/V Catalog Report 11.5.xlsx
like image 628
David542 Avatar asked Jan 28 '13 23:01

David542


People also ask

How create csv file in Linux?

To create a CSV file with a text editor, first choose your favorite text editor, such as Notepad or vim, and open a new file. Then enter the text data you want the file to contain, separating each value with a comma and each row with a new line.

What is ls command output?

The ls command writes to standard output the contents of each specified Directory or the name of each specified File, along with any other information you ask for with the flags. If you do not specify a File or Directory, the ls command displays the contents of the current directory.

What is ls LTR output?

Long Output Format The output from ls -l summarizes all the most important information about the file on one line. If the specified pathname is a directory, ls displays information on every file in that directory (one file per line).


1 Answers

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("\"", "\"\"") + "\""
'
like image 106
nneonneo Avatar answered Sep 20 '22 09:09

nneonneo