9

I have a folder where I keep my untagged music in which I want to launch a script to delete all the PNG and JPG files, except those beginning with the word "cover".

Here's what I tried (I didn't include the -delete flag at the end on purpose):

find . -name '*.jpg' -or -name '*.png' -not - -name 'cover.*'
find . (-name '*.jpg' -or -name '*.png'\) -not - -name '*.png'

I have also tried other variations of the above two commands, but none worked.

1 Answer 1

12

The command you are looking for is:

find . -type f \( -name '*.jpg' -or -name '*.png' \) -not -name "cover.*"
  • Adding type -f will make the find command look only for files.

  • In your second command, you need to add a space after \( and before \) (you also forgot \ before ().

  • Also, you don't need a - between -not and -name. Your first command works fine if you remove it, although not producing the output that you want (see JoL's comment).

You can read more about find's syntax and options at the command's online Ubuntu manpage, or run the command man find to read the manual in the terminal. Specifically, read the OPERATORS section of the manual, since that's what appears to be confusing you the most.

Note: Most terminal commands provide a manual with the proper command syntax and available options which you can read if you run man <command> in your terminal.

3
  • 1
    "Your first command works fine if you remove it." -- No, it wouldn't. -a (which is the default operator when you have 2 adjacent expressions) has higher precedence than -o/-or. That means the first command would be equivalent to: find . -name '*.jpg' -or \( -name '*.png' -not -name 'cover.*' \) which is different.
    – JoL
    Commented Sep 2, 2019 at 20:44
  • You are right. My comment about the first command refers to the OP's statement that none of the commands worked for them, not on the command producing the correct result. I am editing my answer to clarify this. Thank you! Commented Sep 2, 2019 at 21:04
  • @JoL The OP's first command was not running due to the -. Commented Sep 2, 2019 at 21:20

You must log in to answer this question.