Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

i need to store directory information in a way that i can parse as json.

when i do ls -m it gives me comma separated values but no user/group/size info.

bash-3.2$ ls -mp
100CANON/, 2009-11-27_1540.swf, 2009-11-27_1546.swf, 2009-11-27_1551.swf, 2009-11-30_0149.png, 429355_1.sql,, About Stacks.pdf,

ls -l is problematic to parse.

is there anyway you know that I can get ls -l info as comma(or anything) separated values?

share|improve this question
You should ask this on stackoverflow, because you need a function that returns data about files, rather than parsing ls. ls is designed to be human readable, and you can get the same data from a syscall. – Tom O'Connor Jan 9 '10 at 10:30
I respectfully disagree; as Joschi has clearly demonstrated. – Devrim Jan 9 '10 at 10:55
Why parse output of a command when the data is available in a more native format? – Tom O'Connor Jan 9 '10 at 13:28
I don't mean to drag it but if i haven't mentioned JSON in my text, you wouldn't have your argument, thus you shouldn't use it. This question could have been "how to use stat to get comma separated values". Besides, clearly no programmer would know how to use stat command as well as a system admin would which is why this question is asked here. – Devrim Jan 11 '10 at 2:17
I think it is also impolite to imply "oh you'd do X if it is Y" this is as good as saying, why wouldn't one eat meat if hungry. Allergies? Diabetes? Religious? How could u dare to guess the conditions that this answer needs to fit? – Devrim Jan 11 '10 at 2:24
up vote 5 down vote accepted

You could use stat and define your own output format (see parameter -c).


$ stat -c '{"name": "%n", "size": "%s"}' *
{"name": "directory1", "size": "4096"}
{"name": "directory2", "size": "6"}
{"name": "file1", "size": "2070"}
{"name": "file2", "size": "83013"}
share|improve this answer

just combine the ls -l output with some other commands, like this ls -l | tr ' ' ',' | tr '\n' ','. This way you should get the output you need.

share|improve this answer
thx Christian but filenames may contain spaces too. – Devrim Jan 9 '10 at 10:34

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.