Last Updated: February 25, 2016
· jrmoran

Find files and exclude some dirs

I'm currently working on my dotfiles and needed a way to find files with a *.zsh extension inside a folder. And this worked fine on my machine (osx, zsh).

find $DOT -follow -name "*.zsh" 
    ! -path "$DOT/zsh/tools/*" 
    ! -path "$DOT/backup/*"

Intended to run in a single line

The -follow flag tells find to follow symlinks and in this case I needed it since $HOME/.dotfiles is a link pointing to another directory in my system.

The exclamation symbols ! are negations and combined with -path tell find to ignore those folders.


I use it inside my .zshrc file to source *.zsh files within my dotfiles

function ls-zsh-files {
  find $DOT -follow -name "*.zsh" 
      ! -path "$DOT/zsh/tools/*"
      ! -path "$DOT/backup/*"

# source all files with a .zsh config files
for zshfile in $(ls-zsh-files)
  source $zshfile

Also, the ls-zsh-file is made available to the shell, so it can be run from anywhere

zsh> ls-zsh-files

This is helpful for troubleshooting and finding out which *.zsh files get sourced.