17

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?

Dan
  • 32,584
  • 6
  • 98
  • 168
iLemming
  • 1,223
  • 9
  • 14
  • 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`](https://www.emacswiki.org/emacs/download/find-dired%2b.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

2 Answers2

16

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.

Marco Wahl
  • 2,796
  • 11
  • 13
  • Is it possible to automatize this process where automatically check for `*org` without manually entering it – alper Mar 15 '20 at 11:21
  • If I understand you correctly there are many possibilities. E.g. make a special seach a command with (defun my-find-orgfiles () (interactive) (find-name-dired "~/myorgs/" "*org")) and put it into your init file. Then use M-x my-find-orgfiles RET. Or even bind the command to a key. – Marco Wahl Mar 16 '20 at 11:23
  • Can `my-find-orgfiles ()` followed by "t" and "Q" // that selects all and apply? @Marco Wahl – alper Apr 10 '20 at 12:07
5

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
Tino
  • 420
  • 5
  • 9