I have ~/scripts/ folder that contains multiple subfolders with arbitrary directory levels.

This folder only for scripts that need to be sourced when start zsh, how to recursively source all files under its folder and its subfolders in an short and effective way?

up vote 7 down vote accepted

Sourcing all non-hidden regular files in there, in collation order:

for f (~/scripts/**/*(N.))  . $f

I would suggest however that you name those files using a specific template like with a .zsh extension (and use *.zsh instead of * above) to avoid problems if there are backup files lying about in there for instance.

Or you could at least exclude some common ones like file~, file.dpkg-dist, file.back...:

set -o extendedglob
for f (~/scripts/**/^*("~"|dpkg-(dist|old|new)|.(tmp|back|bak))(N.))  . $f


  • for f (~/scripts/**/^*("~"|dpkg-(dist|old|new)|.(tmp|back|bak)))(N.)) . $f. With .zsh means <..>s/**/^*.zsh("~"|dpkg<...>? – Tuyen Pham Nov 9 at 15:19
  • 3
    @TuyenPham No, there is no sense in combining them like that. It's about using a whitelist vs a blacklist. The whitelist is /*.zsh, and the blacklist is /^*("~"|dpkg<...>). It's safer to use a whitelist, but if you have naming requirements that don't allow for requiring files to end in .zsh, then you could at least opt for using a blacklist. – JoL Nov 9 at 16:38

Your Answer


By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.