6

I have a folder where my untagged music is where I want to launch a script to delete all the .png and .jpg except those beginning by the word 'cover'. Here's what I tried:

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

And other variation of the two, none worked. (I didn't included the -delete at the end on purpose)

7

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.

  • 1
    Excellent. Maybe a hint to the man page could prove usefull for other readers. My first move in such cases is to read the manual. – Marc Vanhoomissen Sep 2 '19 at 10:42
  • @MarcVanhoomissen Hint added. Thanks for the suggestion! – user3140225 Sep 2 '19 at 10:57
  • Thanks, it worked! – user991023 Sep 2 '19 at 11:38
  • 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 Sep 2 '19 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! – user3140225 Sep 2 '19 at 21:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy