I have a cron job which runs a script to unrar all files in a certain directory (/rared for argument's sake) and place the expanded files in /unrared. I would like to change this script so that it deletes the original rar archives from /rared only if they successfully extracted.

This does not mean that unrar has reported that they have been fully extracted, because I have had data corruption during decompression before.

Ideally (pie-in-the-sky, just to give you an idea of what I'm shooting for,) the unrar program would include this functionality, comparing an expected md5sum value with the actual md5sum value and only deleting the archive if they match. I don't mind scripting this entire process if I have to, but there must be a better way than unraring twice and comparing md5sums.

  • Reposted as non-community-wiki (original deleted) – Jeremy Oct 14 '10 at 5:57
  • I have a straaaange feeling I'm gonna have to offer a bounty on this one! But I'll lose the right to post bounties :/ #zerothworldproblems – Jeremy Oct 14 '10 at 6:04
  • If unrar doesn't report data corruption, I'd rather check my disk/filesystem/memory for errors... – JanC Oct 14 '10 at 15:05
  • It doesn't happen often (only seen it once or twice in years), but I don't want to risk losing a huge chunk of data because I didn't want to have to write a script to confirm that it extracted properly – Jeremy Oct 14 '10 at 15:36

A friend of mine has been working on a bash shell script to do this which is on github here.



This is the script I have so far, it first checks if there is a current unrar operation running, and exits if there is (don't want to swamp the disk with heaps of read/writes). It then unRARs all files in /rared which have not already been unRARed, placing extracted files in /unrared. It does not yet check the extracted files or delete the archives:


# First check if there is an "unrar" running already, if so, exit.

if ps -ef | grep -v grep | grep -v unrarall | grep unrar ; then
        exit 0

# This line probably unnecessary

# The RARs I download are always multi-part, so I have to find the
#   first file in the archive and extract only that. This is done
#   using the "find" command. -exec means "run this command on the
#   file" and the filename is substituted wherever {} is 

find /rared/ -name "*part01.rar" -exec unrar -y -o- x \{\} /unrared/ \;
find /rared/ -name "*part001.rar" -exec unrar -y -o- x \{\} /unrared/ \;
find /rared/ -name "*.r00" -exec unrar -y -o- x \{\} /unrared \; 

# If you only want .rar files, comment out the above 3 lines and
#   uncomment the one below
# find /rared/ -name "*.rar" -exec unrar -y -o- x \{\} /unrared/ \;


This is the crontab entry which accompanies it:

# m h dom mon dow   command
  * * *   *   *     rared/unrarall > /dev/null
  • Notes on unrar flags for anyone reading the script: "-y" means "answer yes to all questions" and "-o-" means "do not overwrite existing files when decompressing" – Jeremy Oct 14 '10 at 6:05
  • Ok, so it turns out that unrar has a return code, which will at least tell me if all of the parts of the rar were present, and a file was extracted from them, if not getting me to the md5sum stage. I'll post an updated script when I've played around with $? – Jeremy Oct 18 '10 at 12:57

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.