Command Line – How to Use Multiple Criteria for the ‘find’ Command

18.04command linefind

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)

Best Answer

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.