gregseth gregseth - 1 year ago 76
Linux Question

Iterate over list of files with spaces

I want to iterate over a list of files. This list is the result of a

command, so I came up with:

getlist() {
for f in $(find . -iname "foo*")
echo "File found: $f"
# do something useful

It's fine except if a file has spaces in its name:

$ ls
foo bar baz.txt

$ getlist
File found: foo_bar_baz.txt
File found: foo
File found: bar
File found: baz.txt

How can I do to avoid the split on spaces ?

Answer Source

You could replace the word-based iteration with a line-based one:

find . -iname "foo*" | while read f
    # ... loop body
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download