下面的代码通过了测试。
#!/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.