Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I'm writing a shell script to compress and backup certain files. This will compress and move large files with sizes up to 4 GB.

I'm having trouble with this line:

    gzip < $filelocation > $backuplocation

Where

  $filelocation = /home/user/image.img 

  $backuplocation = /home

And I will add a similar line to decompress the file

    gunzip < $filelocation > $backuplocation

Now it doesn't work for some reason.

I try gunzip $filelocation > $backuplocation

Will I be able to pipe it and move the compressed file instead into the directory?

share|improve this question
    
What you've written is plausible, but knowing what $filelocation and $backuplocation are might help. What error message do you get? What exactly do you mean by "it doesn't work for some reason"? –  roaima Mar 21 at 14:57
    
Well, I have an if statement to check if $backuplocation is a directory. And if it is, compress the file and dump it into that directory. The error I'm getting is code./backup.sh: line 28: /home: Is a directory. Check my edit to see the paths specified. –  Bruce Strafford Mar 21 at 15:01

1 Answer 1

up vote 3 down vote accepted

You don't make a backup with gzip into a directory, but into a different file:

 gzip < file.in > file.out

or in your case:

 filelocation=/home/user/image.img
 backuplocation=/home/image.img.gz
 gzip < "$filelocation" > "$backuplocation"

based on that you can do:

 gunzip < "$backuplocation" > /some/new/location/image.img
share|improve this answer
    
"$backuplocation", surely? –  roaima Mar 21 at 15:04
    
@roaima yes, should have done copy and paste –  Anthon Mar 21 at 15:06
    
I run the above with bash -x to see the process and it gets stuck on gzip command. it just shows ` + gzip` which should be followed by the paths but apparently it's not working. No error msg either. the line in question: gzip < "$filedir/$filename" > "$dirpath/$filename" [I'm asking for directory and file name this time so I can use the file name when specifying the backup. Should it be "$dirpath/$filename.gz" instead? –  Bruce Strafford Mar 21 at 15:24
    
@BruceStrafford the output name is not required to have the .gz extension, it is just custom, and what you get if you do gzip somefile (without redirection, the result is somefile.gz) gzip can take a while, use gzip -vv to see some progress. –  Anthon Mar 21 at 15:35
    
@Anthon It worked! Marked as the answer. –  Bruce Strafford Mar 21 at 15:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.