sexta-feira, 28 de março de 2008

Extract Script

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 arquivo
2- Descobrir o programa que descompacta esse formato
3- Descobrir o processo de descompactar o arquivo
4- Descobrir se quando descompactado ele vai colocar tudo em uma pasta já ou não.
5- Descompactar na forma possível
6- Descobrir o nome da pasta aonde os arquivos extraídos ficaram

Então fiz esse script que resume esses 6 passos em apenas 1:

extract arquivo.qualquer_coisa


ele 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 USA

main_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 1
fi

while (( $# > 0 )) #Main Loop
do

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 1
done

4 comentários:

Anônimo disse...

Hi Guys,Just registered here and looking to have a great time. I am looking for the best cash gifting program out there in the internet. Can you guide me?

Below are some sites that I found and I am not sure how much they are going to help me.
[url=http://www.squidoo.com/Residual-Cash-Forever-Cash-Gifting-System]cash gifting[/url]
[url=http://www.squidoo.com/Residual-Cash-Forever-Cash-Gifting-System]join cash gifting[/url]
[url=http://www.squidoo.com/Residual-Cash-Forever-Cash-Gifting-System]best cash gifting program[/url]

Anônimo disse...

Predilection casinos? scrutinization this unsophisticated [url=http://www.realcazinoz.com]casino[/url] advisor and accentuate online casino games like slots, blackjack, roulette, baccarat and more at www.realcazinoz.com .
you can also sojourn our modish [url=http://freecasinogames2010.webs.com]casino[/url] declare something at http://freecasinogames2010.webs.com and pick up business-like politely away !
another inventive [url=http://www.ttittancasino.com]casino spiele[/url] conspire is www.ttittancasino.com , in lieu of of german gamblers, scuttle in frustrate lead around online casino bonus.

Anônimo disse...

top [url=http://www.xgambling.org/]casino games[/url] check the latest [url=http://www.realcazinoz.com/]casino online[/url] unshackled no store reward at the best [url=http://www.baywatchcasino.com/]no put gratuity
[/url].

Anônimo disse...

[url=http://www.casino-online.gd]online casino[/url], also known as arranged casinos or Internet casinos, are online versions of bit ("confrere and mortar") casinos. Online casinos franchise gamblers to haul site in and wager on casino games from guv to foot the Internet.
Online casinos typically drag up as a replacement after sales marathon odds and payback percentages that are comparable to land-based casinos. Some online casinos supervision higher payback percentages agreeable daring automobile games, and some quality general payout share audits on their websites. Assuming that the online casino is using an correctly programmed unsystematic tot up generator, catalogue games like blackjack comprise an established column edge. The payout segment search of these games are established at closer the rules of the game.
Uncountable online casinos sublease pass‚ or come into the capture of their software from companies like Microgaming, Realtime Gaming, Playtech, Intercontinental Game Technology and CryptoLogic Inc.