Batch extract multiple archives with password - command line issues!

Discussion in 'Mac Apps and Mac App Store' started by peanutismint, Dec 22, 2009.

  1. peanutismint macrumors 6502

    peanutismint

    Joined:
    Apr 4, 2007
    Location:
    Cardiff, UK
    #1
    Hello all!

    I'm trying to batch-extract a large folder of *.rar archives which all have the same password without having to manually enter the password each time. To this end I have managed to install the popular extraction utility 'unrar' and use it via Terminal.

    I think I've gotten a handle on using the switches etc, as I have managed to successfully extract one file, without creating a subdirectory, and without having to enter the password (apart from in the single command, obviously...). The next part, applying the 'unrar' command to all 578 files, is proving more difficult...

    So the command I'm using is as follows:

    Code:
    From within the archives' directory:
    
    unrar e -p[password] *.rar
    
    Apparently, according to tutorials, this should extract all of the files in the current location to their current directory using the password specified. However, it does something bizzarre - it seems to try to extract a few of the archives in the folder from quite a way down the list and then the process just seems to end:

    Code:
    UNRAR 3.91 freeware      Copyright (c) 1993-2009 Alexander Roshal
    
    
    Extracting from /Users/Me/Folder/Files/archive207.rar
    
    Extracting from /Users/Me/Folder/Files/archive208.rar
    
    Extracting from /Users/Me/Folder/Files/archive209.rar
    
    No files to extract
    Peanuts-MacBook-Pro:Frasier Peanut$ 
    

    I think it's the '*' wildcard function that is throwing up issues, as when I replace *.rar for an individual filename, it works perfectly. Obviously, this is no good.

    Anyone got any clue as to what I might be doing wrong??
     
  2. Mumford macrumors regular

    Joined:
    Oct 8, 2006
    Location:
    Altadena, CA
    #2
    The *.rar on the end is being expanded by the shell before being passed to unrar. So the command turns out to be
    Code:
    unrar -e archive1.rar archive2.rar archive3.rar ...
    If you look at the help for unrar (unrar -h), you can see this is being interpreted as "extract archive2.rar, archive3.rar ... from archive1.rar".

    If you have multiple archives you want to extract, you'd need to use a loop.
    In bash:
    Code:
    $ for archive in *.rar
    > do
    >   unrar e $archive
    > done
    In tcsh:
    Code:
    % foreach archive (*.rar)
    foreach? unrar e $archive
    foreach? end
     
  3. peanutismint thread starter macrumors 6502

    peanutismint

    Joined:
    Apr 4, 2007
    Location:
    Cardiff, UK
    #3
    Ah right, thanks - i think I understand what you say is happening, although I'm afraid I don't understand how to implement this 'loop'....

    Any chance you could offer a bit more explanation?

    Thanks for your help so far! :)
     
  4. Mumford macrumors regular

    Joined:
    Oct 8, 2006
    Location:
    Altadena, CA
    #4
    Not sure how unix-y we want to get here. You can hang out at the Unix Shell Programming & Scripting forums to learn more.

    If you have 3 rar files, named archive1.rar archive2.rar archive3.rar, you were doing this:
    Code:
    unrar e archive1.rar archive2.rar archive3.rar
    which unrar interprets as "try to extract archive2.rar and archive3.rar from archive1.rar". Most unix commands don't behave this way. If you type
    Code:
    ls -al archive1.rar archive2.rar archive3.rar
    ...it lists each one as if you did them one at a time. But unrar (and unzip!) are different.

    What my for loop does is this:
    Code:
    unrar e archive1.rar
    unrar e archive2.rar
    unrar e archive3.rar
    I just rolled it into a nice for-loop so I wouldn't have to type out each name, which is convenient since there may be many more than 3 archives (maybe there are 3000?). This would break if there were spaces in any of the file names (e.g. "some archive name with spaces in it.rar"). In that case we'd have to use find:
    Code:
    find *.rar -exec unrar e {} \;
     
  5. peanutismint thread starter macrumors 6502

    peanutismint

    Joined:
    Apr 4, 2007
    Location:
    Cardiff, UK
    #5
    Ah right, I think I understand a little bit better now - thanks....

    So is the 'for loop' basically telling terminal to repeat the process but add one number higher to the end of the .rar filename it tries to unzip?

    If this is the case, then I will have a problem, as I know for my example I used 'archive1.rar, archive2.rar...' etc, but in reality the archives all have completely different filenames.... I probably should have made that clear from the start.......

    Does this change things?

    (this is turning out to be a lot more difficult than I'd have hoped - it's 2009! Surely somebody makes a program that extracts multiple rars with the same password??!? :confused:)
     
  6. PacoH macrumors newbie

    Joined:
    Jul 8, 2013
    #6
    Thanks Mumford. This is exactly what I was looking for.

    For the passworded multi-part archive I used
    Code:
    unrar x -pPassword $archive "/POSIX path/to/destination folder/"
    in place of
    Code:
    unrar e $archive
    This worked perfectly.

    For those completely unfamiliar with the terminal app and the command line, to do this, just type

    Code:
    $ for archive in *.rar
    at the $ prompt and hit <enter> (or <return>).

    You will then see the > prompt. Type "do" (without the quotes) and hit <enter>. Then type the command I gave above, replacing Password with the correct password (no space, no quotes), and the destination path with your own desired path. e.g., "/Users/My User Name/Pictures/". Use quotes to be safe, in case there are any spaces in the path. Hit <enter>. Type "done" (no quotes) and hit <enter> and, if you did everything right, the extraction will begin without prompting for the password.
     

Share This Page