小编use*_*355的帖子

循环子目录中的 json 文件

我有一个文件夹,里面有子文件夹。我尝试遍历每个子文件夹并在 mongo 数据库中导入 json 文件:

#!/bin/bash
for dir in /data/Mydata/*/; 
do
    ls -1 *.json | while read jsonfile; 
    do 
        mongoimport --db MyApp --collection logs --file $jsonfile --type json
        done;
done;
Run Code Online (Sandbox Code Playgroud)

但这给了我一个错误:

ls: cannot access *.json: No such file or directory
Run Code Online (Sandbox Code Playgroud)

如果我在每个子目录中并执行以下操作:

ls -1 *.json | while read jsonfile; 
do 
    mongoimport --db MyApp --collection logs --file $jsonfile --type json
done;
Run Code Online (Sandbox Code Playgroud)

有用。

我究竟做错了什么?我对 Ubuntu 很陌生。此致

command-line bash

6
推荐指数
2
解决办法
6552
查看次数

标签 统计

bash ×1

command-line ×1