LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Copy and crear tar.gz at the same time. (https://www.linuxquestions.org/questions/linux-newbie-8/copy-and-crear-tar-gz-at-the-same-time-4175442979/)

miros84 12-26-2012 07:21 AM

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

millgates 12-26-2012 07:24 AM

Hi, have you tried
Code:

tar zcvf /backup/name.tar.gz /some/folder

shivaa 12-26-2012 11:53 AM

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


miros84 12-27-2012 02:52 AM

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.

shivaa 12-27-2012 03:53 AM

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.

rknichols 12-27-2012 10:42 AM

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.

miros84 12-28-2012 01:48 AM

Quote:

Originally Posted by millgates (Post 4857016)
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?

millgates 12-28-2012 03:29 AM

Quote:

Originally Posted by miros84 (Post 4858241)
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 (Post 4858241)
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.


All times are GMT -5. The time now is 10:08 PM.