Tuesday, January 11, 2011

Extracting multiple file types while ignoring errors

System info:

aggitan@moneque:~$ uname -a
Linux moneque 2.6.32-25-generic #44-Ubuntu SMP Fri Sep 17 20:05:27 UTC 2010 x86_64 GNU/Linux

aggitan@moneque:~$ 7z

7-Zip 9.04 beta  Copyright (c) 1999-2009 Igor Pavlov  2009-05-30
p7zip Version 9.04 (locale=en_US.utf8,Utf16=on,HugeFiles=on,2 CPUs)

I've got a folder that has 68 archives in it ranging from .rar, .ace, & .zip.

I want to extract all of these files using their folder name as the first directory ("Extract here")

If I use file-roller it halts at the first error, there doesn't appear to be an "ignore error" flag for file roller.

If I use 7zip it dumps everything into the current folder and doesn't use clean folders

How can I extract everything into separate folders without spilling everything into the current directory?

  • for i in *; do mkdir "$i.extracted"; (cd "$i.extracted" && 7z x "../$i") || echo "Error with $i"; done

    Riccardo Murri : The simplest of the lot :-)
    aggitan : I cheated and cross posted this to super user, this is the answer I originally used. http://superuser.com/questions/200640/mass-file-extraction
  • I'm not aware of any direct solution but with a little bash loop you can do it in a terminal (if, as your question seems to suggest, 7zip is able to do everything you want except for extracting into a folder based on the filename). Try (in the directory with the archives):

    for FILE in *.*; do DIR=${FILE%.*}; mkdir $DIR && 7z x -o$DIR $FILE ; done
    

    The ${FILE%.*} extracts the filename without the extension.

    Tobias Kienzler : you need to `cd $DIR` or otherwise tell 7zip to extract there...
    Tobias Kienzler : if there are both `stuff.zip` and `stuff.rar` this will mix up their contents
    Marcel Stimberg : The `-o${FILE%.**}` is telling to use the dir, but I should have reused the `$DIR` variable instead. I'll edit my answer.
    Marcel Stimberg : About `stuff.zip` and `stuff.rar`: That's true, but I think the OP wanted to recreate file-roller's "Extract Here" behaviour which does not create folders like `stuff.zip.extracted` but only uses the filename without the extension.
    Tobias Kienzler : that's true, +1ing
    aggitan : Thank you for your post. I'll try and use this script and the others for similar problems I come along!
  • A little shell scripting might come to the rescue.

    #! /bin/bash
    
    for archive in "$@"; do (
        archive_dir="$(cd $(dirname "$archive"); pwd -P)"
        archive_name="$(basename "$archive")"
        # make a directory by appending `.d` to the archive file name
        name="${archive_name}.d"
        mkdir -p "$name"
        cd "$name"
        # extract contents with full path, 
        # replace 'x' with 'e' to extract into $name directory
        7z x "${archive_dir}/${archive_name}"
    ); done
    

    Paste the above into a file extract.sh (in the directory where you want to extract files) and then make it executable:

    chmod +x ./extract.sh
    

    The script creates a directory for each archive given on the command line by appending .d to its file name (e.g., for an archive stuff.zip it will create directory stuff.zip.d), and then extracts files from the archive into it.

    You can invoke it in a terminal like this (use wildcards to extract multiple archives in one go):

    ./extract.sh stuff.zip stuff2.rar
    

    Disclaimer: untested, so try it out with one or two sample archives before making the big run.

    Tobias Kienzler : if there are both `stuff.zip` and `stuff.rar` this will mix up their contents
    Riccardo Murri : @Tobias Thanks, fixed.
    Tobias Kienzler : +1. although as Marcel states, that *is* file-roller's behaviour. so it depends on aggitan's exact intentions
    aggitan : Thank you for the script I'll try and use this in the future.

0 comments:

Post a Comment