1

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 :P

FreeSoftwareServers
  • 1,109
  • 1
  • 13
  • 31

1 Answers1

2

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'
heemayl
  • 90,425
  • 20
  • 200
  • 267
  • can I ask why the code I found uses -exec rm -r {} \; instead of -delete? – FreeSoftwareServers Aug 13 '15 at 06:28
  • @FreeSoftwareServers thats a clumsy way of doing what `-delete` does, also `-r` is redundant with `rm` as we are dealing with files only..`-exec rm -r {} \;` will spawn an extra process `rm` for every file, it will be a improvement of `-exec rm -r {} \;` if `-exec rm {} +` was being used.. – heemayl Aug 13 '15 at 06:31
  • and to sum up your previous comment altho -exec rm {} + is an improvement, i am better off using -delete anyway – FreeSoftwareServers Aug 13 '15 at 06:34
  • @FreeSoftwareServers Yeah..i did not look the `*FINAL.mkv` glob you wanted..fixed.. – heemayl Aug 13 '15 at 06:38
  • would it be to much to ask for second bit of code that renamed all remaining files the same name without the word FINAL?, I can give you credit in the script im creating for the help with the perfections, im all about automation, but I can start a new thread or research more if not. – FreeSoftwareServers Aug 13 '15 at 06:42
  • Im assuming there is only the files wanting to be renamed, not extra files that need to be ignored, say just a folder with 1 season of episodes, now renamed *FINAL.mkv that i want to revert to *.mkv after removing original files – FreeSoftwareServers Aug 13 '15 at 06:44
  • @FreeSoftwareServers I would love to help but i don't think i get this one properly..could you please ask a new question with more details (and examples).. – heemayl Aug 13 '15 at 06:56
  • http://askubuntu.com/questions/660839/rename-final-mkv-to-mkv-keeping-original – FreeSoftwareServers Aug 13 '15 at 06:57