Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting multiple json files recursively

This is a theoretical question about minimizing side effects in bash scripting.

I recently used a simple mechanism for formatting a bunch of json files, in a nested directory structure...

for f in `find ./ -name *json`; do echo $f ; python -mjson.tool $f > /tmp/1 && cp /tmp/1 $f ; done.   

The mechanism is simply to

  • format each file using python's mjson.tool,
  • write it to a tmp location, and
  • then rewrite it back in place.

Is there a way to do this which is more elegant, i.e. with minimal side effects? I'm assuming bash experts have a better way of doing this sort of thing .

like image 948
jayunit100 Avatar asked Apr 05 '15 13:04

jayunit100


1 Answers

Unix tools working on a streaming basis -- they don't store all of the contents of the files in memory at once. Therefore, you have to use an intermediary location since you would be overwriting a file that is currently being read from.

You may consider that your snippet isn't fault tolerant. If you make a mistake, you would have just overwritten all your data. You should store the output in a new location, verify, then move to overwrite. :)

like image 197
RJ Nowling Avatar answered Oct 29 '22 04:10

RJ Nowling