How to loop through all the files located under a certain path in zsh?

for-loop, glob, zsh

Solution

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

- the `**` 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`.

- you do not really need double quotes around `$file` because zsh does not split variables into words on substitution.

- the first version is the regular form of the `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.

Problem

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' ```

Original source