遍历带空格的文件列表 [英] Iterate over a list of files with spaces
本文介绍了遍历带空格的文件列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想遍历文件列表.该列表是find
命令的结果,所以我想到了:
I want to iterate over a list of files. This list is the result of a find
command, so I came up with:
getlist() {
for f in $(find . -iname "foo*")
do
echo "File found: $f"
# do something useful
done
}
可以,除非文件名中包含空格:
It's fine except if a file has spaces in its name:
$ ls
foo_bar_baz.txt
foo bar baz.txt
$ getlist
File found: foo_bar_baz.txt
File found: foo
File found: bar
File found: baz.txt
我该怎么做才能避免空格分裂?
What can I do to avoid the split on spaces?
推荐答案
您可以将基于单词的迭代替换为基于行的迭代:
You could replace the word-based iteration with a line-based one:
find . -iname "foo*" | while read f
do
# ... loop body
done
这篇关于遍历带空格的文件列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文