Zsh: source scripts recursively

scriptingzsh

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?

Best Answer

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

etc.