Link to home
Start Free TrialLog in
Avatar of Link
LinkFlag for United States of America

asked on

simple bash script needed to unpack tar.gz

I have a series of commands I use to unpack a tar.gz file.
Lets say this file is xxxx.tar.gz. Here are the following commands:

gunzip xxxx.tar.gz
tar xvf xxxx.tar
cd xxxx
gunzip *.tgz
for a in *.tar
do
tar xvf $a
done
rm *.tar
rm -rf wirespeed
rm -rf database
cd linuxLogs
gunzip *.gz
cd ../closedfire
gunzip asp_saved_logs.tgz
tar xvf asp_saved_logs.tar
rm *.tar
cd ../tailf
gunzip tailf_saved_logs.tgz
tar xvf tailf_saved_logs.tar
rm *.tar
cd ../np
gunzip np_saved_logs.tgz
tar xvf np_saved_logs.tar
rm *.tar
cd ..
pwd
find ./ -type f | xargs grep "CRITICAL"

I now cut and paste the entire set of commands into my bash window.

Every time I have a new different tar.gz file I need to replace the xxxx with the new file name then cut/paste all the commands again.

I would like to place all the commands in a script named "unpack" and then just use it at the command line with the new filename
like
bash$ unpack filename10.tar.gz


Currently I cut/paste the beginning part of the tar.gz file name (say zzzzwith xxxx above, then paste the entire series into my bash window where the tar.gz is.
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Normally you should create a Linux file containing your set of commands, calling it e.g "myunpack.sh", then issue "chmod +x myunpack. sh". Next, replace inside the file "xxx" with "${1}". Now you can run the commands simply with:

/path/to/myunpack.sh filename

where filename means the name of the desired file and "/path/to/..." means the location of the new file.
If you don't want to proceed as suggested above you can replace "xxx" with ${file} then issue

export file=filename

Now copy and paste as usual.
ASKER CERTIFIED SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Link

ASKER

That was easy, Thanks!