0

Linux Mint 20 Emacs 27.2

I want to find ONLY folders target (include empty) in some folder (e.g myProject)

Steps:

In dired mode


M-x consult-find
/target

And here result: 1

As you can see it found not only folders target. But also all files. But I need ONLY folders target. The result must be smt like this:

2

In Linux terminal I can do this by the next command:

find myProject/ -type d -name "target" 

But how I can do this by consult-find ?

1
  • Does consult-find use the Unix tool find underneath? If so, you can try to figure out how to pass to the underlying find a -type d option to select only directories.
    – NickD
    Oct 1, 2021 at 12:23

3 Answers 3

2

Both answers (@NickD' and the OP's) are right in that consult-find is running find and that passing type d to it is the way to achieve the desired effect. But both of them suggested to create a wrapper command or configure consult-find-args. However, there's actually a idiom for such things in consult's asynchronous search commands (consult-find being one of them). You can actually pass additional command line options on the spot to the "vanilla" consult-find with --. So, you could search for #target -- -type d to get only directories in your search.

2

I just checked and indeed consult-find uses find underneath through the variable consult-find-args whose default value is find . -not ( -wholename */.* -prune ). So try throwing in a -type d in there to make it find . -type d -not ( -wholename */.* -prune ) and see if that works (I have not tested it). If it does, you can write your own function - perhaps consult-find-dir - that let-binds consult-find-args as above, before calling consult-find.

1
0

I found solution. From documentation:

Consult splits the input string into two parts, if the first character is a punctuation character, like #. For example #grep-regexps#filter-string, is split at the second #. The string grep-regexps is passed to Grep. If you enter multiple regular expressions separated by space only lines matching all regular expressions are shown. In order to match space literally, escape the space with a backslash. The filter-string is passed to the fast Emacs filtering to further narrow down the list of matches

.

consult-find, consult-locate: Find file by matching the path against a regexp. Like for =consult-grep,= either the project root or the current directory is the root directory for the search. The input string is treated similarly to consult-grep, where the first part is passed to find, and the second part is used for Emacs filtering.

So in my case:

#target#target$

And here result:enter image description here

And now its find target folders.

Thanks for @NickD the problem with this approach is that it also find file with name target.

So I use another approach. I create my custom function (change value of variable consult-find-args):

(defun consult-find-dir ()
  "Search for regexp with find only DIR in DIR with INITIAL input."
  (interactive)
  (setq consult-find-args "find . -type d -not ( -wholename */.* -prune )")
  (consult-find)
  )

This is better written as follows, with consult-find-args let-bound to the desired value:

(defun consult-find-dir ()
    "Search for regexp with find only DIR in DIR with INITIAL input."
    (interactive)
    (let ((consult-find-args "find . -type d -not ( -wholename */.* -prune )"))
       (consult-find))

Here result:

M-x consult-find-dir
target#target$

enter image description here

And now it found ONLY folder target enter image description here

6
  • 1
    One problem: if you have a file called target, then you cannot use this method to exclude it.
    – NickD
    Oct 1, 2021 at 14:51
  • @NickD I updated my answer Oct 1, 2021 at 17:44
  • 1
    ... which is what I suggested in my answer in the first place :-) There is also a find-dired function that might be just what the doctor ordered: (find-dired DIR "-type d -name target"). See this question for details.
    – NickD
    Oct 1, 2021 at 17:52
  • 1
    BTW, you don't want to setq the variable consult-find-args in your function. You want to let-bind it: ...(let ((consult-find-args "find ...")) (consult-find))).
    – NickD
    Oct 1, 2021 at 17:58
  • 1
    It depends on how often you need the functionality: writing many functions to deal with slightly different situations does not work as well as having one function that deals with a slightly more general situation by passing a single parameter to specialize it. It's a matter of judgment.
    – NickD
    Oct 1, 2021 at 18:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.