8

How can I bring all files with names that conform a specific regex, recursively in a directory tree, to a single dired buffer?

So I can rename/delete them all at once?

  • 2
    Have you considered `find-name-dired' already? (Uses wildcard, not regexps.) – Marco Wahl Nov 28 '16 at 19:01
  • 2
    You should submit that as an answer @MarcoWahl . I had no idea dired could do that! – Tyler Nov 28 '16 at 20:19
  • 1
    Library find-dired+.el gives you an enhanced version of command find-name-dired. It accepts optional args that specify (1) min & max search depths and (2) paths to exclude from searching. – Drew Nov 28 '16 at 22:35
8

Wildcard

Use

M-x find-name-dired

to get files according to a wildcard.

Example

Find all files with suffix "org" in directory "~/org/". Enter the following

M-x find-name-dired
~/org
*org

and enjoy.

Regular Expression

Use

M-x find-dired

with suitable arguments to get files according to a regular expression.

Example

Find all files with suffix "org" in directory "~/org/". Enter the following

M-x find-dired
~/org
-regex ".*org$"

and enjoy.

2

Regular Expression

To use a regex, you might also try find-lisp-find-dired. This command belongs to the standard lib. find-lisp, which is entirely written in elisp i.e., you don't need to have installed an external find program.

For instance, the following shows all the files with extension '.el' under the directory ~/lisp:

M-x find-lisp-find-dired RET ~/lisp RET \.el\' RET

(The match honors case-fold-search).

Wildcard

It's worth to note that since the next Emacs release (v26.1) Dired supports wilcards in the directory part of the file name argument. That means the following works:

C-x d ~/soft/*/*.c RET

(The match is case-sensitive).

This command shows in a Dired buffer all the files with extension '.c', 2 depth levels under '~/soft'. That means, all files like '~/soft/foo/bar.c' will be listed, but not files like '~/soft/qux.c' nor '~/soft/foo/baz/qux.c'.

The same but case-insensitive:

C-x d ~/soft/*/*.[cC] RET

Bonus

If you are running Dired with a 'ls' implemented in elisp, 'ls-lisp' or 'eshell-ls', then you can recursively list all the files matching a wildcard with the following syntaxis:

C-x d ~/soft/**/*.c RET

Note the '**'. In this case all files under '~/soft' matching the wildcard are shown, even '~/soft/qux.c'.

However, when you are using insert-directory-program the wildcard expansion is made by the system shell; that means, this recursive '**' syntaxis just works if it's supported by your shell. For instance zsh does support it, but bash doesn't. With shells other that zsh the previous command will do the same as:

C-x d ~/soft/*/*.c RET

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.