Is it possible from within Emacs to find directories with a given depth from the current directory from within Emacs and receive their respective location in form of a list? I want it to work cross platform...
I'd be inclined to use
find for that:
(let ((dirname (expand-file-name "~/")) (depth 3)) (split-string (shell-command-to-string (format "find %s -mindepth %d -maxdepth %d -type d -print0 2>/dev/null" (shell-quote-argument dirname) depth depth)) "\0" :omit-nulls))
print0 are not POSIX options, so YMMV regarding portability.
The elgrep-search command has options
:maxdepth. Elgrep is available via package-install from the melpa package archive (instructions how to add
package-archives can be found on the melpa "Getting Started" page).
You can use
elgrep as follows to get a list of matching file names of a specific depth. In the example I search in my HOME-directory (
~/) for files matching
\.el\' without in-file matches (that's the
nil) with minimal and maximal recursion depth 2.
(require 'elgrep) (mapcar 'car (elgrep-search "~/" "\\.el\\'" nil :recursive t :mindepth 2 :maxdepth 2))
Elgrep actually returns filematches. That are conses of the file name and a list of in-file matches. In our case the list of in-file matches is empty since we passed
nil for the in-file regexp.
Because of that structure you need the
(mapcar 'car ...). See the documentation of the
elgrep-search command for further details.
maxdepth are also available via the gui-version
elgrep-menu. There you can let elgrep search asynchronously. That way it does not block your Emacs.
find-dired+.el can help with this.
C-h f find-dired:
find-diredis an interactive compiled Lisp function in
(find-dired DIR ARGS &optional DEPTH-LIMITS EXCLUDED-PATHS)
findand put its output in a buffer in Dired Mode.
find’ command run (after changing intoDIR
) is essentially this, whereLS-SWITCHES
find . \( ARGS \) LS-SWITCHES
DEPTH-LIMITS: Minimum and maximum depths:
EXCLUDED-PATHS: Strings matching paths to be excluded. Uses
When both optional args are non-
findcommand run is this:
find . -mindepth MIN-DEPTH -maxdepth MAX-DEPTH \( -path EXCLUDE1 -o -path EXCLUDE2 ... \) -prune -o \( ARGS \) LS-SWITCHES
EXCLUDE2... are the
EXCLUDED-PATHS, but shell-quoted.
Same thing for the related commands:
find-time-dired - they all take arg
To reply to @Tobias's comment: Yes,
find-dired+.el (which extends standard library
find-dired.el) depends on a UNIX or GNU/Linux
find command. On MS Windows you can use Cygwin or any other porting of the command to Windows.
You can use the
ls command of the built-in
eshell for listing the files with some predefined depth.
You have to specify the depth by the right number of concatenated file name generator expressions
(split-string (substring-no-properties (eshell-command-result "ls /usr/share/emacs/26.1/*/*/*/*/*/*(.)") ) "\n" t)
The appended modifier
(.) says that the generator only expands regular file paths.
Note that the backend
eshell-do-ls command is a pure Emacs Lisp implementation of the
Yet in this case the actual work is not done by
ls but by
If you also want to list directories you should add the
-d switch to the
ls command. Otherwise the files contained in the directories would also be listed:
(split-string (substring-no-properties (eshell-command-result "ls -d /usr/share/emacs/26.1/*/*/*/*/*/*") ) "\n" t)
If you actually don't want any special features of
ls you can also use pure
If you want to list all files with some minimal depths you can expand with the two-star form