Skip to main content

Useful script for unrar files in multiple directories

A friend of mine recently asked me to help with a problem he had. When he downloaded files from the internet, no doubt legitimate, many of them contained nested directories with an rar file and associated components in them. Some of these downloads look like this (for example):

  • Main Folder
    • Sub-Folder 1
    • Sub-Folder 2
    • Sub-Folder n etc

This is really tedious to go through each sub-folder and unrar each archive so I wrote a simple script for him to run straight from the linux/*BSD command line:

angus@server: ~# directory=/path/to/directory ; for dir in $( ls $directory ) ; do cd $dir ; unrar e *.rar ; cp *.avi /path/to/end/directory ; cd .. ; done

It seems to work relatively well. An expansion of this as a bash script:

# Script to extract RAR files downloaded in torrents - usually TV series type torrents
# This is the directory your torrents are downloaded to
echo "Please input torrent directory: "
read -r "input_torrent"
echo "$input_torrent"
# This is the directory you want the extracted files to be copied to
echo "Please input directory for extraction: "
read -r "output_dir"
echo "$output_dir" 
#enable for loops over items with spaces in their name
for dir in `find "$input_torrent" -type d`
        cd $dir
        # ls # uncomment this line and comment the two lines below for testing
        unrar e *.part001.rar #or this can be unrar e *.rar
        cp *.avi "$output_dir"
        cd ..

Notes about this script:
  • unrar e *.part001.rar
    • I've found that this may need to be altered dependent on my friend's torrents. The directory may have the files set up in a similar pattern to this above: file.partXXX.rar OR also commonly found is file.XXX with a file.rar that is the key file to the archive
  • The input_torrent and output_dir variables need to be written without backslashes i.e.
    • /path/to/files with a space in the name
    • NOT /path/to/files\ with\ a\ space\ in\ the\ name as you would usually expect in a *nix environment
      • This is because I'm learning bash scripting and making things all neat and tidy is more than I'm capable of doing :-)
  • It's set up to copy the extracted avi file elsewhere
The bit of the script between the "do" and the "done" can be modified to do different things which might be handy for you down the track.

Modify as you require and drop a comment if you have anything to add to the script!

AB out.


Popular posts from this blog

Plone - the open source Content Management System - a review

One of my clients, a non-profit, has a lot of files on it's clients. They need a way to digitally store these files, securely and with availability for certain people. They also need these files to expire and be deleted after a given length of time - usually about 7 years. These were the parameters I was given to search for a Document Management System (DMS) or more commonly a Content Management System (CMS). There are quite a lot of them, but most are designed for front facing information delivery - that is, to write something, put it up for review, have it reviewed and then published. We do not want this data published ever - and some CMS's make that a bit tricky to manage. So at the end of the day, I looked into several CMS systems that looked like they could be useful. The first one to be reviewed was OpenKM ( ). It looked OK, was open source which is preferable and seemed to have solid security and publishing options. Backing up the database and upgradin

Windows 10 Enterprise Eval - gotchas

After an annoying turn of events where my Windows 10 Enterprise USB drive failed, attempts to install Win10 onto a computer failed miserably. I turned to the net and managed to get my hands on Microsoft's Windows 10 Enterprise Evaluation. I have an enterprise key so I thought - cool! Here's the opportunity to get it going and to then upgrade the license later. Full install, patched etc and all is swell. Except when I try to upgrade. I straight up tried changing the licence key only to get a variety of errors, most of which are pertaining to the activation system being unavailable. The I try this: but it doesn't work either. Next I'll try this: h ttp:// And if all else fails, in goes the bootable USB I've now created. If only I'd had this in the first instance I would not be writing t

elementary OS 5.1 Hera - a review and a revisit

 It's been ages since I used a desktop Linux distribution - being up to my ears in the horror of implementing ISO 27001 doesn't leave you much time to play around with computers - too busy writing policies, auditing and generally trying to improve security to a formally acceptable and risk managed level. I need a quick, small OS though to do the occasional network scan, view the contents of a dodgy file on and for general, low impact activities. I remembered reviewing elementary OS ( ) some time ago ( see ) from 2015 so I thought it was worth a revisit.  I downloaded the ISO from their website, forgoing to donation for the moment while I review it. If it turns out I'm going to keep using it, I'll send them some love. The ISO is 1.38GB in size and I booted it in a VMware Player instance. From go to whoa (I won't include the install photos here) it took about 10 minutes with a dual vCPU and 4GB of