Recursively listing directories in elisp

I was looking for a function that recursively scanned directories, and could not find one. So I wrote my own. Maybe somebody else will find it useful.

How to list, recursively, the files in a given directory

Its parameters are:

Parameter Description
directory Directory to scan
match Regular expression of files to match
depth Max depth to scan. Avoid long scans or getting into a loop
ignore Regular expression of files and directories to ignore–tested before match and recursion

Here is an example on how to use it: search directory /home/dmg/git.dmg/projects for files with extension .org but ignore those files/directories that contain the substring rip or stage.

(directory-files-recursive "/home/dmg/git.dmg/projects" "\\.org$" 2 "\\(rip\\|stage\\)")

You can find it in github as a gist.


This code snippet is licensed under the same terms of Emacs (currently GPL v3 or LATER).