I have hundreds of folders with multi part archives inside them.


I am using the code below to extract them inside their respective folders and to remove them upon successful extraction,

flock -n locked -c "find . -name '*.rar' -execdir unrar e -o- {} \; -execdir rm {} \; && find . -name '*.r[:0-9:][:0-9:]' -exec rm {} \;"

The problem is if something goes wrong, lets say;

  • One of the parts of the archive is corrupted,
  • Files are not uploaded yet

This script deletes the multi parts even though extracting is not successful. How can improve this script so it deletes rar files and parts if extraction is successful. I spend my whole morning to figure this out but so far no success.

  • how do you expect the event "Files are not uploaded yet" to be detected ? – RomanPerekhrest Feb 8 '18 at 16:14
  • I am depending on successful extraction on that side. – user611811 Feb 8 '18 at 16:20

The rm commands must be dependent on the result of the unrar command where in your example they are all run sequentially whatever happens.

So try something like that instead (on a test directory first!):

flock -n locked -c "find . -name '*.rar' -print0 | xargs -0 -I FILE sh -c \"unrar e -o- FILE && rm FILE `basename FILE .rar`.r[:digit:][:digit:]\" "

Then the rmpart is executed only if the unrarcommand finished successfully.

You will need extra quotes if your file names have spaces.

  • Sorry but I am strugling to understand this; Extraction went OK, deleted .rar file but the rest stays. How can I delete the rest of the files "test_file.r[:0-9:]" rm: cannot remove './test_file.rar.r[:0-9:]': No such file or directory – user611811 Feb 9 '18 at 7:19
  • I have edited my answer, it should work for all files from .r00 to .r99 – Patrick Mevzek Feb 9 '18 at 14:11

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.