使用bash脚本遍历目录中的文件 [英] Iterate over files in directory using bash script

查看:154
本文介绍了使用bash脚本遍历目录中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这些文件遍历中给出的目录。我尝试使用for循环一样...但我有一个这样的循环,我需要读取多个文件,直到条件是循环中真正来一次上传在另一个循环......但withing内环我能够访问只有一个文件,因为文件迭代循环是内部循环之外。
有没有像file.getNext任何事情,这样我可以访问环路内的文件数不使用索引/循环。

I would like to iterate over the files in given directory. I tried using the for loop for the same... but I have one another loop inside this loop where I need to read multiple files till the condition is true within that loop to upload at a time... but withing inner loop I am able to access only one file since file iterator loop is outside the inner loop. Is there any thing like file.getNext so that i can access number of files inside the loop without using index/loop.

我需要的东西是这样的:

I need something like this:

while [ count -le $uploadRate ]
do
   files="$files,directory.getNextFile"
   $(curl -o $log_Path -T "{$files}" url)
done

其中, $文件将有一次上传的文件数量
我不想将文件加载到数组,因为目录中有文件数量巨大,所以数组是不是可行的选择,所以想遍历像GETNEXT文件目录。

where $files will have number of files uploaded at a time I don't want to load the files into array since directory has huge number of files, so array is not feasible option, so would like to iterate over directory like getNext file.

推荐答案

您可以让您的外环这样读取目录中的 / somedir / :

You can keep your outer loop like this for reading all the files/directories in a directory /somedir/:

filesToBeRead=5
chunkFiles=()
for f in /somedir/*; do
    chunkFiles+=( "$f" )
    if [[ ${#chunkFiles[@]} -eq 5 ]]; then
        processChunkFiles( "${chunkFiles[@]}" )
        chunkFiles=()
    fi
done
processChunkFiles( "${chunkFiles[@]}" )

PS:您需要写一个 processChunkFiles 函数来处理/过程5档

PS: You need to write a processChunkFiles function to handle/process 5 files.

这篇关于使用bash脚本遍历目录中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆