将所有文件递归到单个文件中

Moh*_*hit 47 unix linux bash grep cat

我有一堆文件坐在文件夹中

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
Run Code Online (Sandbox Code Playgroud)

我想把所有的jsons都塞进一个文件中?

Pav*_*vel 86

find data/ -name '*.json' -exec cat {} \; > uber.json
Run Code Online (Sandbox Code Playgroud)

一个简短的解释:

find <where> \
  -name <file_name_pattern> \
  -exec <run_cmd_on_every_hit> {} \; \
    > <where_to_store>
Run Code Online (Sandbox Code Playgroud)

  • 不使用find:`cat ./data/*/*/*/*.json> ./ all.json` (7认同)
  • 什么是 \; 正在做? (3认同)
  • 您还应该添加“ -type f”(仅文件),以避免在cat尝试打印目录时出错。 (2认同)

Bar*_*mar 13

使用find让所有的JSON文件,将它们连接起来.

find data -name '*.json' -exec cat {} + > all.json
Run Code Online (Sandbox Code Playgroud)

请注意,这不是有效的JSON.如果您希望JSON文件包含多个对象,则它们需要位于包含数组或对象中,因此您需要[ ]在它们周围添加并放在,每个对象之间.

  • 严格来说,但是使用`.json`扩展名的目录是相当不正常的. (3认同)
  • 我也会在`find`命令中添加`-type f`参数. (2认同)

csi*_*siu 10

或者 - 如果你有一个文件列表 - 你可以管道 xargs

<path to your files> | xargs cat > all.json
Run Code Online (Sandbox Code Playgroud)

  • 以递归方式获取列表:find \`pwd \` (2认同)

Luc*_*ack 6

find ./ -type f | xargs cat > ../singlefilename
Run Code Online (Sandbox Code Playgroud)

我想要这个,简单易行。

../避免错误input file is output file