35

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'
smci
  • 32,567
  • 20
  • 113
  • 146
Paul Baltescu
  • 2,095
  • 4
  • 25
  • 30

2 Answers2

95

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 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.

Adaephon
  • 16,929
  • 1
  • 54
  • 71
  • This prints an error if no files could be found. How would one disable that message? – Lars Nyström Apr 19 '21 at 10:19
  • 2
    @LarsNyström You can add `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`. – Adaephon Jun 04 '21 at 08:45
  • Answer is a goldmine for leveraging great features in zsh. Awesome explanation and referencing. – Ari Sep 21 '22 at 04:01
3

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
  • 1
    In `zsh`, unlike in `bash`, wordsplitting is disabled by default (unless for some reason you set `sh_word_split`) and quoting variables has no effect. – Resigned June 2023 Mar 05 '17 at 02:49