Recursive cat all the files into single file

北城余情 提交于 2019-11-30 06:18:46

问题


I have bunch of files sitting in folders like

data\A\A\A\json1.json
data\A\A\A\json2.json
data\A\A\B\json1.json
...
data\Z\Z\Z\json_x.json

I want to cat all the jsons into one single file?


回答1:


find data/ -name '*.json' -exec cat {} \; > uber.json

a short explanation:

find <where> \
  -name <file_name_pattern> \
  -exec <run_cmd_on_every_hit> {} \; \
    > <where_to_store>



回答2:


Use find to get all the JSON files and concatenate them.

find data -name '*.json' -exec cat {} + > all.json

Note that this will not be valid JSON. If you want a JSON file to contain multiple objects, they need to be in a containing array or object, so you'd need to add [ ] around them and put , between each one.




回答3:


Alternatively -- if you have a list of your files -- you can pipe that to xargs

<path to your files> | xargs cat > all.json



回答4:


find ./ -type f | xargs cat > ../singlefilename

I would like this ,easy and simple.

../ avoid the error input file is output file.



来源:https://stackoverflow.com/questions/24069173/recursive-cat-all-the-files-into-single-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!