0

To find files by multiple file extensions I use in Linux smt like this (in terminal):

find ./ -type f \( -iname \*.txt -o -iname \*.json \)

And here result:

./test.txt
./project_1_1/test.json
./project_1_1/test1_1.txt
./project_1_2/test_1.2.txt

Nice. But what about Emacs? Is it possible in Emacs to find files with multiple file extensions and open result in separate buffer? I need this because then I want to delete all find files.

a_subscriber
  • 4,062
  • 1
  • 18
  • 56
  • You can run that exact shell command using M-!. – NickD Nov 16 '23 at 22:27
  • as in find-file to open them all in emacs? – Maxim Kim Nov 16 '23 at 22:41
  • @NickD I need then to open result in separate buffer – a_subscriber Nov 17 '23 at 07:02
  • What does that mean? The "result" is the list. So do you want to have the list in a separate buffer? Do you wnant to be able to choose some files from the list and open them in separate buffers? Open every file in the list in separate buffers? In the latter case, have you thought about what would happen if the list consists of thousands of files? In any case, please modify the question and describe exactly what you want to accomplish. – NickD Nov 17 '23 at 07:15
  • Would M-x find-name-dired be enough? – Manuel Uberti Nov 17 '23 at 07:43
  • @NickD I was edit my post – a_subscriber Nov 17 '23 at 08:43
  • If you don't know about the -exec option or the -execdir option of find (both with the ; and the + suffix), you should find out about them. You should also probably become familiar with xargs. OTOH, if you want to learn some Elisp, @shynur's answer contains a bunch of things that you probably want to learn. But the quickest and safest way to a solution to your problem is to add -execdir rm '{} \+ to your find command. BTW, I would always run the initial find command first to make sure that it is picking up only files you want removed. Then run it with -execdir .... – NickD Nov 17 '23 at 15:34
  • Correction: missing quote in the execdir option: -execdir rm '{}' \+. – NickD Nov 17 '23 at 16:45
  • @ManuelUberti Not enough. It can't find files with differents extenstions – a_subscriber Nov 17 '23 at 19:38
  • 1
    True, you can use find-dired with the args you prefer, e.g., -name \*.json -o -name \*.txt. – Manuel Uberti Nov 20 '23 at 08:16
  • @ManuelUberti Yes, this help: find-dired -name *.json -o -name *.txt – a_subscriber Nov 20 '23 at 08:32

2 Answers2

3

I need this because then I want to delete all find files.

Easy to do if you know some ELisp.

(mapcan #'delete-file
        (directory-files-recursively
         "./"  ; current directory
         (concat ".+\\.\\("
                 (mapconcat #'identity
                            [  ; Here's what you want to del
                             "txt"   ; <--
                             "json"  ; <--
                             ]
                            "\\|")
                 "\\)\\'")))
shynur
  • 5,253
  • 1
  • 4
  • 25
1

You can use find-dired to specify the find arguments you need. So, starting from the directory you want to search in:

  • M-x find-dired RET
  • -name \*.json -o -name \*.txt RET
Manuel Uberti
  • 3,250
  • 19
  • 37