0

I have the following directory tree:

/Directories/
/Directories/somedir/somefile.mkv
/Directories/somefile.rar

I'm trying to find a way to delete the .rar file if the .mkv file exists and is larger than 1 GB.

I've use find -type f -size +1000000k -name "*.mkv" to find the files. I then presume that either -exec or | xargs could help with the rest but I don't know how to exit the output of find before passing it to rm. In my scenario the output would be /Directories/somedir/somefile.mkv and the command passed to rm would Directories/*.rar

Can I do this with -exec or xargs. Is there a more elegant solution?

Morgan
  • 841

1 Answers1

0

I don't think both conditions can be checked within the same find command. You can, however, do a short script that would make the necessary checks and delete the file if they're satisfied.

#!/bin/bash

for file in `find -type f -size +1000000k -name "*.mkv"`; do
  # This would remove the mkv extension
  noext=${file::-4}

  # Parts of directories
  parentdir=`echo $file | cut -d'/' -f2` # This would return 'Directories'

  if [ -e "./$parentdir/$noext.rar" ]; then
     rm -f "./$parentdir/$noext.rar"
  fi
done

Not tested, but it would be something very similar to this.

nKn
  • 5,832