Bash逐层遍历某个目录下的所有文件,并进行某种处理

下面的代码通过了测试。

#!/bin/bash
while IFS= read -r -d '' file; do
     # single filename is in $file
     echo "$file"
     # your code here
done < <(find /ifs/DRR_Datasets_Unzipped/Mixed_Dataset -type f -print0)


注意,原文解释了代码如下:

filenames can contain blanks, tabs, spaces, newlines, ... to process filenames in a safe way, find with -print0 is used: filename is printed with all control characters & terminated with NUL. see also Gnu Findutils Manpage, Unsafe File Name Handling, safe File Name Handling, unusual characters in filenames. See David A. Wheeler below for detailed discussion of this topic.

大意是这段代码,即使文件名中有空格,或者控制字符也可以正常处理。


参考资料

=============

https://stackoverflow.com/questions/9612090/how-to-loop-through-file-names-returned-by-find 的answer17.

posted on 2020-09-15 17:47  中道学友  阅读(590)  评论(0编辑  收藏  举报

导航

技术追求准确,态度积极向上