Here's what I have so far:
for file in $(find /path/to/directory -type f); echo $file; done
but I get this error:
zsh: parse error near `done'
There is no need to use find
. You could try the following:
for file in /path/to/directory/**/*(.); do echo $file; done
or
for file in /path/to/directory/**/*(.); echo $file
**
pattern matches multiple directories recursively. So a/**/b
matches any b
somewhere below a
. It is essentially matches the list find a -name b
produces.(.)
is a glob qualifier and tells zsh to only match plain files. It is the equivalent to the -type f
option from find
.$file
because zsh does not split variables into words on substitution.for
-loop; the second one is the short form without do
and done
The reason for the error you get is due to the last point: when running a single command in the loop you need either both do
and done
or none of them. If you want to run more than one command in the loop, you must use them.
N
to the glob qualifier, e. g. for file in /path/to/directory/**/*(N.); echo $file
. This sets the NULL_GLOB
option for that pattern, which deletes the pattern from the argument list instead of reporting an error. So in case no matching files exist, the command would effectively be reduced to for file in ; echo $file
.
Can you check if adding ""
around $file
solves this problem like so:
for file in $(find /path/to/directory -type f); echo "$file"; done
Edit:
add do
before echo
and let me know if it solves the problem:
for file in $(find /path/to/directory -type f); do echo "$file"; done
zsh
, unlike in bash
, wordsplitting is disabled by default (unless for some reason you set sh_word_split
) and quoting variables has no effect.
Mar 5, 2017 at 2:49
do
after the first ;