LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-25-2011, 05:05 AM   #1
danishgambit
LQ Newbie
 
Registered: Sep 2009
Location: glasgow, scotland
Posts: 25

Rep: Reputation: 0
problem using gunzip within a script


Morning,

got a small script to uncompress and untar a bunch of files. The command works when I run it against 1 file on the command line, but when I stick it in a script in a for loop, it doesn't work. I've got


for file in `ls *.bd.tar.gz`
do
gzip -dc $file | tar xvf -
done

It's probably something obvious, but I can't see what it is. Any suggestions would be welcome.
 
Old 10-25-2011, 05:24 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
What does "it doesn't work" mean? Any error message? Unexpected behaviour? Your script works fine for me, but I wonder what prevents you from using
Code:
tar xvf $file
instead of the gzip method. GNU tar should automatically take care of the compression (without specifying any additional option). Another note is about parsing the output of ls, which is not advisable for reasons expressed here.
 
Old 10-25-2011, 05:34 AM   #3
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by danishgambit View Post
Morning,

got a small script to uncompress and untar a bunch of files. The command works when I run it against 1 file on the command line, but when I stick it in a script in a for loop, it doesn't work. I've got


for file in `ls *.bd.tar.gz`
do
gzip -dc $file | tar xvf -
done

It's probably something obvious, but I can't see what it is. Any suggestions would be welcome.
Hi,

I can also verify that the script works. So my guess is that your filenames might contain spaces. The for-loop will not process them properly in this case. That is why you should avoid parsing 'ls'. Try this instead:
Code:
for file in *.bd.tar.gz
do
  gzip -dc "$file" | tar xvf -
done
 
Old 10-25-2011, 05:36 AM   #4
danishgambit
LQ Newbie
 
Registered: Sep 2009
Location: glasgow, scotland
Posts: 25

Original Poster
Rep: Reputation: 0
Morning,

yep, sorry about the lack of information re: the errors in the original post.
Getting 2 errors for each $file:

e.g. with a filename of jobs.bd.tar.gz

gzip: jobs.bd.tar.gz.gz: No such file or directory
tar: blocksize = 0

The gzip error message looks like it's appending an extra ".gz" to the end of the file name,
and the fact the .gz.gz file doesn't exist probably explains the 2nd message.

Hope this helps.

Regards,

DG.
 
Old 10-25-2011, 05:41 AM   #5
danishgambit
LQ Newbie
 
Registered: Sep 2009
Location: glasgow, scotland
Posts: 25

Original Poster
Rep: Reputation: 0
solved - thanks

Morning,

as suggested by the 2 responses, parsing the output of the ls seems to have been the cause of my issue.

Tried the code suggested by crts and it works.

Thanks for the help chaps.

DG.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
gunzip and ftp using a script @ a time shipon_97 Linux - Newbie 6 03-22-2009 01:24 PM
Gunzip Problem anandham Red Hat 2 08-11-2005 05:13 AM
Gunzip Problem anandham Linux - Newbie 1 08-09-2005 07:07 AM
Help using gunzip. troycus Linux - Newbie 2 09-22-2003 08:32 PM
gunzip-how to use. OldSarge Linux - Software 11 08-28-2003 04:10 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration