Extract Script
Autor: ThOR27 | Publicado: 2008-03-28T10:32:00-07:00

Esse script é para facilitar a vida de quem usa muito os terminais do Linux. Sempre que você vai extrar um arquivo você tem que:1- Verificar o formato do arquivo2- Descobrir o programa que descompacta esse formato3- Descobrir o processo de descompactar o arquivo4- Descobrir se quando descompactado ele vai colocar tudo em uma pasta já ou não.5- Descompactar na forma possível6- Descobrir o nome da pasta aonde os arquivos extraídos ficaramEntão fiz esse script que resume esses 6 passos em apenas 1:extract arquivo.qualquer_coisaele extrai o arquivo no formato que for e ainda MELHOR: se os arquivos não foram criados com uma pasta raiz, ao invés de ver todos seus arquivos descompactados jogados no seu desktop, ele cria a pasta ANTES e extrai os arquivos lá dentro.e depois diz aonde ele está ;)Por enquanto ele suporta apenas tar.gz, tar.bz2, zip e rar... é facilmente extensível e o código está bem comentado para ajudar a todos nessa tarefa#!/bin/bash#     Extract - Extract many formats of files, and create a root dir #     if doesn't exists#     #     Copyright (C) 2008  Thomaz de Oliveira dos Reis# #     This program is free software; you can redistribute it and/or modify#     it under the terms of the GNU General Public License as published by#     the Free Software Foundation; either version 2 of the License, or#     (at your option) any later version.# #     This program is distributed in the hope that it will be useful,#     but WITHOUT ANY WARRANTY; without even the implied warranty of#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the#     GNU General Public License for more details.# #     You should have received a copy of the GNU General Public License#     along with this program; if not, write to the Free Software#     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USAmain_execute(){ # Parameters: 1- Command to get root directory[ies] inside the compressed file#       2- Filename without extension#       3- Extract command for the previous directory#       4- Extract command for this directory  DIRETORIO=`eval $1` if (( `echo "$DIRETORIO" | wc -l` > 1 )) then  DIRETORIO=$2    if ! [ -d $DIRETORIO ]  then   mkdir $DIRETORIO  fi    cd $DIRETORIO  eval $3 >/dev/null else  eval $4 >/dev/null fi echo "File(s) extracted at: $DIRETORIO"}tar_execute(){# 1- Command to get root directory[ies] inside the compressed file P1="tar -tf \"$1\" | cut -f1 -d/ | uniq"#  2- Filename without extension P2=`basename $1 | sed 's#.tar.gz$##g;s#.tar.bz2$##g;s#.tgz$##g'`# 3- Extract command for the previous directory P3="tar -$2xf \"../$1\""#  4- Extract command for this directory P4="tar -$2xf \"$1\"" #  Running main_execute "$P1" "$P2" "$P3" "$P4"}zip_execute(){# 1- Command to get root directory[ies] inside the compressed file P1="unzip -l \"$1\" | tr -s ' ' | cut -f5- -d' ' | sed 's#Name##g;s#-*##g;/^ *$/d' | cut -f1 -d/ | uniq"#  2- Filename without extension P2=`basename $1 | sed 's#.zip$##g'`# 3- Extract command for the previous directory P3="unzip \"../$1\""#  4- Extract command for this directory P4="unzip \"$1\""#  Running main_execute "$P1" "$P2" "$P3" "$P4"}rar_execute(){# 1- Command to get root directory[ies] inside the compressed file P1="unrar vb \"$1\" | cut -f1 -d/ | uniq"#  2- Filename without extension P2=`basename $1 | sed 's#.rar$##g'`# 3- Extract command for the previous directory P3="unrar x \"../$1\""#  4- Extract command for this directory P4="unrar x \"$1\"" #  Running main_execute "$P1" "$P2" "$P3" "$P4"}# your_new_extension(){# # 1- Command to get root directory[ies] inside the compressed file#  P1="the list of files, one per line, with directories, inside $1 | cut -f1 -d/ | uniq"# #  2- Filename without extension#  P2=`basename $1 | sed 's#.YOUR_NEW_EXTENSION$##g'`# # 3- Extract command for the previous directory#  P3="extract command \"../$1\""# #  4- Extract command for this directory#  P4="extract command \"$1\"" # #  Running#  main_execute "$P1" "$P2" "$P3" "$P4"# }if (( $# < 1 )) #Any file given?then echo "Missing files." echo "Usage: $0 files." exit 1fiwhile (( $# > 0 )) #Main Loopdo  if ! [ -f $1 ] then  echo "File $1 does not exists!" else  OK=0    if echo extracting $1 | grep tar.gz$ || echo $1 | grep tgz$   then   tar_execute $1 z   OK=1  fi    if echo extracting $1 | grep tar.bz2$   then   tar_execute $1 j   OK=1  fi    if echo extracting $1 | grep zip$  then   zip_execute $1   OK=1  fi    if echo extracting $1 | grep rar$  then   rar_execute $1   OK=1  fi   #if echo extracting $1 | grep YOUR_NEW_EXTENSION$  #then  # YOUR_NEW_EXTENSION $1  # OK=1  #fi   if [ OK == 0 ]  then   echo "Sorry, I don't know what to do with $1"   echo "What do you think about extending this script to support it? :)"  fi fi  shift 1done