Ubuntu – Edit find command to exclude a file

command linefind

So I made this script Convert videos recursively with handbrake.

And it uses this command as an option

find . -name '*.mkv' -exec rm -r {} \;

But i made this new script, for removing audio streams recursively Remove Audio Streams of videos – recursively without converting audio or video with FFMPEG

But I would like to add the option to remove all *.mkv files EXCEPT ones that are *FINAL.mkv as that is the output my second script uses for the files.

Could somebody help real quick, I'm not very good at coding, I can just manipulate it very well and understand it 😛

Best Answer

To remove all mkv files recursively except FINAL.mkv you can do :

find . -type f -not -name '*FINAL.mkv' -name '*.mkv' -delete
  • Use -type f to search for only files

  • -name '*.mkv' will get us all .mkv files

  • -not -name '*FINAL.mkv' will leave out all the *FINAL.mkv files

  • -delete will remove the files found.

Also do a dry run by the following at first to check that everything is all right :

find . -type f -not -name 'FINAL.mkv' -name '*.mkv'