LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 12-26-2012, 08:21 AM   #1
miros84
Member
 
Registered: Aug 2009
Location: Spain
Distribution: Debian stable, squeeze
Posts: 501

Rep: Reputation: 31
Copy and crear tar.gz at the same time.


I need to create a bash thata copy some folder and make a tar.gz file.

How can I create it?

Code:
cp -rf /some/folder /backup/name.tar.gz
 
Old 12-26-2012, 08:24 AM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 651

Rep: Reputation: 269Reputation: 269Reputation: 269
Hi, have you tried
Code:
tar zcvf /backup/name.tar.gz /some/folder
 
Old 12-26-2012, 12:53 PM   #3
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Rep: Reputation: 285Reputation: 285Reputation: 285
It can be easily done as:
Code:
bash# cp /path/to/source /path/to/target
bash# tar -cvf sample.tar /path/to/target
bash# gzip sample.tar

Last edited by shivaa; 12-26-2012 at 12:54 PM.
 
Old 12-27-2012, 03:52 AM   #4
miros84
Member
 
Registered: Aug 2009
Location: Spain
Distribution: Debian stable, squeeze
Posts: 501

Original Poster
Rep: Reputation: 31
But this way first I copy the folder and later I make it tar.
Is there way to copy and make it tar at the same time and only one command - one line?
Problem is that I want to change the name to (Year-Month-Day-hour)
and create a variable for that.
 
Old 12-27-2012, 04:53 AM   #5
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Rep: Reputation: 285Reputation: 285Reputation: 285
Define a variable such as timestamp as follow:-
Code:
timestamp=$(date +%Y%m%d%H%M)
And then do:-
Code:
cp /path/to/source /path/to/target.$timestamp
Although cp command cannot create tar while copying, but still you can combinte both in one line as:-
Code:
cp /path/to/source /path/to/target ; tar -cvf sample.tar /path/to/file(s)
But there's no difference whether you do it in one line or two lines.
 
Old 12-27-2012, 11:42 AM   #6
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 1,612

Rep: Reputation: 674Reputation: 674Reputation: 674Reputation: 674Reputation: 674Reputation: 674
If you really want to, you can make a pipeline that unpacks the tar file on-the-fly to do the copy function:
Code:
tar -czf - /path/to/files | tee /backup/name.tar.gz | tar -C /path/to/target -xzf -
The first tar command sends the compressed output to stdout, the tee command makes a copy in the named file and passes the stream along to its stdout, and the final tar command switches to the target directory and extracts the files from the stream on its stdin.
 
Old 12-28-2012, 02:48 AM   #7
miros84
Member
 
Registered: Aug 2009
Location: Spain
Distribution: Debian stable, squeeze
Posts: 501

Original Poster
Rep: Reputation: 31
Quote:
Originally Posted by millgates View Post
Hi, have you tried
Code:
tar zcvf /backup/name.tar.gz /some/folder
I think thatīs OK.
Very simple and work.
Why do I need 2 times tar.

Code:
z, --gzip
c --create
-v, --verbose
-f, --file F
Millgates, only I dont understand why do you use verbose?
 
Old 12-28-2012, 04:29 AM   #8
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 651

Rep: Reputation: 269Reputation: 269Reputation: 269
Quote:
Originally Posted by miros84 View Post
I think thatīs OK.
Very simple and work.
Why do I need 2 times tar.
The difference is that my example only puts the tar.gz archive to your destination directory, while the others will put both the compressed and the uncompressed directory there. It wasn't very clear from your first post which one you want.

Quote:
Originally Posted by miros84 View Post
Millgates, only I dont understand why do you use verbose?
Just a habit. I like to see what the command is doing. Feel free to omit that option.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
tar copy with include eodchop Linux - General 2 08-21-2009 11:46 AM
Tar file copy error rays_pks Gentoo 1 06-17-2009 03:38 AM
Redundant Directory Copy w/ Tar carlosinfl Linux - General 3 03-20-2008 01:30 PM
need copy of tomcat 4 tar file Kaao Linux - Software 1 05-16-2006 01:20 PM
Crear a zip file hansi umayangan Linux - General 3 10-25-2004 04:45 PM


All times are GMT -5. The time now is 08:04 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration