LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-12-2012, 04:37 PM   #1
wh33t
Member
 
Registered: Oct 2003
Location: Canada
Posts: 922

Rep: Reputation: 61
tar and gzip at same time from command line: ubuntu


I'm writing a back up script and I'd like to tar a directory called "/current/" and gzip it at the same time.

So far I have tried $ tar -cf <folderpath> > gzip -9 archive.tar.gz and it keeps telling me I'm cowardly trying to create a file with out a name.

Any help would be great
 
Old 10-12-2012, 04:45 PM   #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
The tar command has an option to do that in one shot:
Code:
tar -zcf archive.tar.gz /path/to/dir
-z is intended to compress the archive using gzip. Other options let you select other compression algorithms, for example -j for bzip2, --lzma for lzma and so on. You have also the -I option to select any compression program available on your system arbitrarily.

Anyway, a working version of your attempt is:
Code:
tar -c /path/to/dir | gzip > archive.tar.gz
without -f <archive name> tar sends the output to the standard output. gzip grabs it through the pipe and redirects to the compressed archive.

Last edited by colucix; 10-12-2012 at 04:51 PM.
 
1 members found this post helpful.
Old 10-12-2012, 04:54 PM   #3
wh33t
Member
 
Registered: Oct 2003
Location: Canada
Posts: 922

Original Poster
Rep: Reputation: 61
Quote:
Originally Posted by colucix View Post
The tar command has an option to do that in one shot:
Code:
tar -zcf archive.tar.gz /path/to/dir
-z is intended to compress the archive using gzip. Other options let you select other compression algorithms, for example -j for bzip2, --lzma for lzma and so on. You have also the -I option to select any compression program available on your system arbitrarily.

Anyway, a working version of your attempt is:
Code:
tar -c /path/to/dir | gzip > archive.tar.gz
without -f <archive name> tar sends the output to the standard output. gzip grabs it through the pipe and redirects to the compressed archive.
Ahh! I knew the pipe we needed somewhere! I wasn't sure where and I kept finding examples of Solaris of AIX examples. So that's great to know that tar can handle gzip natively but can I pick the compression level for gzip using tar? I wanted to do a gzip -9 for max compression.
 
Old 10-12-2012, 05:16 PM   #4
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
Indeed with -z, tar uses the default compression level of gzip (that is 6). To specify options for gzip, use -I:
Code:
tar -cf archive.tar.gz -I 'gzip -9' /path/to/dir
.
or - since gzip uses the environment variable GZIP to retrive default options chosen by the user (if any):
Code:
GZIP=-9 tar -zcf archive.tar.gz /path/to/dir
 
Old 10-12-2012, 05:18 PM   #5
wh33t
Member
 
Registered: Oct 2003
Location: Canada
Posts: 922

Original Poster
Rep: Reputation: 61
Quote:
Originally Posted by colucix View Post
Indeed with -z, tar uses the default compression level of gzip (that is 6). To specify options for gzip, use -I:
Code:
tar -cf archive.tar.gz -I 'gzip -9' /path/to/dir
.
or - since gzip uses the environment variable GZIP to retrive default options chosen by the user (if any):
Code:
GZIP=-9 tar -zcf archive.tar.gz /path/to/dir
Brilliant! How do people come to possess so much knowledge about Linux?! I want/need to be like that. Thank you so much.
 
  


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
[SOLVED] Tar command line questions (am I missing something?) steak1987 Linux - Newbie 5 01-14-2012 08:54 PM
Installing an RPM or Tar.GZ package on CDROM in Command Line. mr.linux.noob Linux - General 10 10-28-2008 01:04 AM
[gzip] appears in process table after gzip command completes redmanDBA Linux - General 0 02-26-2008 06:12 AM
How do I install .tar.gz apps via the command line? BigFoot13 Linux - Newbie 33 11-14-2006 03:06 PM
tar command will only do one file at a time Travis86 Linux - Newbie 4 03-15-2003 07:27 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 10:59 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