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?


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 '18 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 '18 at 16:38
  • How to exclude a folder under ~/scripts/? In case that the *.zsh files in that folder need to be sourced in order manually. Can't arbitrary source file under that folder. I tried | folder/* and | folder/*.zsh, both didn't work, I got bad pattern when trying doing that. – Tuyen Pham Dec 3 '18 at 4:03
  • Okay, I got that: unix.stackexchange.com/a/71884/256195 – Tuyen Pham Dec 3 '18 at 4:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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