1

I would like to print my output of the ls command as a JSON array with the current working directory as the prefix for every element in the array.

I managed to find another thread which helped me sort the first part out of my problem, but would appreciate some help with the prefix part.

Lets say I have the following directory and file structure:

 Docs (Directory)
 |
 +-- Random file 1
 |    
 +-- Readme (Working Directory)
 |  |  
 |  +-- Readme.md
 |  +-- Readyou.md

When I execute the following command in the Readme Directory (taken from the thread I linked to earlier):

python -c 'import os, json; print json.dumps(os.listdir("."))'

I get the following output:

["Readme.md", "Readyou.md"]

What I would like to achieve is the structure above, BUT with the working directory as the prefix for every element in the array.

["Readme/Readme.md", "Readme/Readyou.md"]

Is this something that can managed pretty straightforward in bash?

Nael
  • 23
  • 2

1 Answers1

2

One way:

(cd .. && find Readme -print0) | jq -sR 'split("\u0000")[1:]'
Shawn
  • 47,241
  • 3
  • 26
  • 60
  • Would have upvoted if I could. Worked pretty smooth. – Nael Oct 22 '19 at 10:59
  • @Nael you should be able to accept the answer even if you can't upvote them yet; it gives even more reputation! Check [this question](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) if you have troubles doing so. – Aaron Oct 22 '19 at 15:14