Author Topic: Script to extract multiple files musonio  (Read 1004 times)

Offline Texstar

  • Administrator
  • Super Villain
  • *****
  • Posts: 12539
Script to extract multiple files musonio
« on: July 13, 2009, 04:28:21 AM »
In response to a problem posed by guitarmike,
travisn000 offered the following script to extract multiple archives in a directory at the same time.
Actually, the script pops up a dialog which asks you to select which files you want to unpack.

To getting it running you should do the following:

1) Open a text editor (nano, kate, kwrite, etc...) and paste the following:

Code: [Select]
#!/bin/bash -x

FILE=$1
cd "${FILE%/*}"

for i in $1; do
  echo -e "\n\nTrying to extract $i"

  if [[ $i == *.[Rr][Aa][Rr] ]]; then
    unrar x -ad $1;

  elif [[ $i == *.7[Zz] ]]; then
    file_basename=`basename $FILE .7[Zz]`
    echo -e "\n\nCreating directory $file_basename"
    mkdir "$file_basename .7z"
    cd "$file_basename";
    7za x $i;
 
  elif [[ $i == *.[Zz][Ii][Pp] ]]; then
    file_basename=`basename $FILE .zip`
    echo -e "\n\nCreating directory $file_basename"
    mkdir "$file_basename"
    unzip $i -d "$file_basename";

  elif [[ $i == *.[Tt][Aa][Rr].[Gg][Zz] ]]; then
    file_basename=`basename $FILE .tar.gz`
    echo -e "\n\nCreating directory $file_basename";
    mkdir "$file_basename";
    cd "$file_basename";
    tar -xf $i;

  elif [[ $i == *.[Tt][Aa][Rr].[Bb][Zz][2] ]]; then
    file_basename=`basename $FILE .[Tt][Aa][Rr].[Bb][Zz][2]`;
    echo -e "\n\nCreating directory $file_basename";
    mkdir "$file_basename";
    cd "$file_basename";
    tar -xf $i;

  fi
done


2) Save the file with whatever name you like.
3) Make the file executable.
4) Copy it to /usr/local/bin.
5) Open a text editor (nano, kate, kwrite, etc...) and paste the following:

[
Code: [Select]
Desktop Entry]
Actions=extractHereMultiple
ServiceTypes=inode/directory
X-KDE-Priority=TopLevel

[Desktop Action extractHereMultiple]
Name=Extract compressed files
Icon=ark
Exec=konsole --noclose -e bash name_of_the_script %U

(If you want konsole to close itself after unpacking everything, remove the --noclose option).

Save the file as something like extract_here.desktop in ~/.kde/share/apps/konqueror/servicemenus

That's it.

Now, when you right click in an empty spot in a directory, a dialog will pop-up asking you which of the archives in the directory you want to unpack, and voila.

As it is, the script will work for .rar, .zip, .tar.gz and .7z.
If you want to add more archive types, modify the script to your liking.

CAUTION: everything you read above is an expression of my complete ignorance on the subject.
I am just reporting the steps I took based on travisn000 suggestions and script.
If something goes wrong, I am to blame. If everything goes OK, he is the one to thank.
« Last Edit: July 13, 2009, 04:30:28 AM by texstar »

Thanks to everyone who donates. You keep the servers running.