user2883071
user2883071

Reputation: 980

My script does not unzip file

I downloaded a file from a server in the format .tar.gz my script has the commands

tar -zxvf data.tar.gz
rm -rf data.tar.gz

When I run this manually it unzips it and displays the contents, and then it deletes the file data.tar.gz However, after running this from the script, when I go in to the folder where it is saved, the file data.tar.gz is there, but none of its contents are present

What I do not get is why it does not unzip or get deleted.

Part of the script:

OUT=$(date +%Y%m%d -d yesterday)-blah.gz
wget ftp://blah:blah@ftp.haha.com/"$OUT" -O /myFolder/Documents/"$OUT"

tar -zxvf /myFolder/Documents/"$OUT"
#whent the file is unzipped it produces 2 files called abcd and efgh

#because i dont need abcd or the original zipped file
rm -rf /myFolder/Documents/"$OUT"
rm -rf /myFolder/Documents/abcd

OK SO ANOTHER UPDATE: When I run this with cron, it does not work, However, when I bash run it, it works.

Upvotes: 2

Views: 1420

Answers (2)

erKURITA
erKURITA

Reputation: 407

The problem you have is that you're not specifying the output directory for tar. By default, it outputs the file from where you're executing the script. You have several fixes available, but the easiest is:

OUT=$(date +%Y%m%d -d yesterday)-blah.gz
wget ftp://blah:blah@ftp.haha.com/"$OUT" -O /myFolder/Documents/"$OUT"

cd /myFolder/Documents/
tar -zxvf "$OUT"
#whent the file is unzipped it produces 2 files called abcd and efgh

#because i dont need abcd or the original zipped file
#remember you cd'd here, after we're done ...
rm -rf "$OUT"
rm -rf abcd
#go back
cd -

Upvotes: 2

izissise
izissise

Reputation: 951

Its seem you don't launch your script from the directory your archive is in, You must use cd command in your script to move to that directory and then launch tar and rm command

Edit:

tar -zxvf /myFolder/Documents/"$OUT"

tar will extract the archive in the current directory.

Upvotes: 1

Related Questions