Frankie Hung Frankie Hung -4 years ago 190
Bash Question

Recursively process files in a directory in unix

I have made a script ( to convert json files in pretty print format. For example, source.json

It will replace the source.json file, overwrite itself with pretty print format.

Content of

python -m json.tool $INFILE > $INFILE.tmp

It works very well for one file. However, I would like to process all files under a directory. e.g.


Any single line command to do this? I tried

find dir1 | grep .json | xargs

but no success. Please kindly help. Thanks a lot!

Answer Source

Use find to (1) search for file matching some pattern (2) launch your script on each found file:

find dir1 -name "*.json" -exec {} \;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download