1

My dir is as following:

Music: 1.mp3, 2.flac, 3.wav, 4.ogg and 5.mp4

I'm trying to remove all files with extensions: .mp3, .wav, .flac and .ogg with the following command:

find ./Music -type f -name \('*.mp3' -or -name '*.wav' -or -name '*.flac' -or -name '*.ogg'\) -delete

but it's doing nothing to the directory

any idea why?

(i'm one level higher than the Music dir btw)

0

1 Answer 1

3

You have the syntax wrong. You want all the -name options in the same parentheses, and a space around the parentheses, like this:

find ./Music -type f  \( -name '*.mp3' -or -name '*.wav' -or -name '*.flac' -or -name '*.ogg' \) -delete

Your original command was using -name \('*.mp3' -or -name '*.wav' -or -name '*.flac' -or -name '*.ogg'\) which means it was looking for files named \(*.mp3, or '*.wav' or '*.flac' or '*.ogg'\). You can see this if you run the command without the -delete option:

$ find ./Music -type f -name \('*.mp3' -or -name '*.wav' -or -name '*.flac' -or -name '*.ogg'\) 
./Music/3.wav
./Music/2.flac

Since there are no matches for \('*.mp3' or *.ogg\), those files were not found. And since you hadn't grouped the command in parentheses (you need spaces around the parentheses for that to work), the -delete wasn't applied as you expected (sorry, I don't know exactly how find would have parsed that).

3
  • 1
    and is grouped more tightly than or, so you'd have had find ./Music \( -type f -name '(*.mp3' \) -or \( -name '*.wav' \) -or \( -name '*.flac' \) -or \( -name '*.ogg)' -delete \), where I've added the implicit precedence brackets. Notice that the result is effectively a series of no-ops except for deleting files or directories matching the final *.ogg) pattern Commented Sep 18, 2023 at 16:19
  • Also, if ./Music is a symbolic link, they may have to use find -H ./Music ... to resolve it, or find ./Music/ ....
    – Kusalananda
    Commented Sep 18, 2023 at 17:22
  • Thank you, also thanks for explaining!
    – Mathew
    Commented Sep 21, 2023 at 0:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .