Help with tar syntax please

Help with tar syntax please

Post by Slugg » Fri, 14 Jun 2002 00:35:52



Hello,

I'm planning on copying one of my partitions to a new one.  Originally I was
going to use Partimage, but I'd like to try it using tar.  Some searching
has shown me that the following command should work:

cd/home; tar cvpf - * | (cd/newhome; tar xvpf -)

However, I would like to fully understand the command before I use it.
Manual pages tell me what the options c, v, p, f, x do.  I don't know what
the "-", "|", and paranthesis do.  I can see what the command is doing
overall, but I don't understand the specific syntax mentioned.  If anyone
can shed light on this for me, or give me a link to a more comprehensive
explanation of tar, I would appreciate it.

thanks
sluggo

 
 
 

Help with tar syntax please

Post by Lew Pitch » Fri, 14 Jun 2002 01:54:58



>Hello,

>I'm planning on copying one of my partitions to a new one.  Originally I was
>going to use Partimage, but I'd like to try it using tar.  Some searching
>has shown me that the following command should work:

>cd/home; tar cvpf - * | (cd/newhome; tar xvpf -)

>However, I would like to fully understand the command before I use it.
>Manual pages tell me what the options c, v, p, f, x do.  I don't know what
>the "-", "|", and paranthesis do.  I can see what the command is doing
>overall, but I don't understand the specific syntax mentioned.  If anyone
>can shed light on this for me, or give me a link to a more comprehensive
>explanation of tar, I would appreciate it.

1st: change to /home directory
        ( cd /home ; )
now: tar the current directory, outputing the tarfile to stdout
        ( tar cvpf - *)
     pipe stdout from the tar into a shell that
        a) has changed to /newhome directory
                (cd /newhome ; )
        b) untars stdin

Lew Pitcher, Information Technology Consultant, Toronto Dominion Bank Financial Group

(Opinions expressed are my own, not my employer's.)

 
 
 

Help with tar syntax please

Post by Steve Kirkendal » Fri, 14 Jun 2002 02:06:04



> Hello,

> I'm planning on copying one of my partitions to a new one.  Originally I
> was
> going to use Partimage, but I'd like to try it using tar.  Some searching
> has shown me that the following command should work:

> cd/home; tar cvpf - * | (cd/newhome; tar xvpf -)

Actually, I'd recommend...

        cd /home; tar cf - . | (cd /newhome; tar xvfp -)

You need spaces after the "cd" command name.

In the first "tar" command, I dropped the "v" flag for reasons that are
described below.  I also dropped the "p" flag because it is only meaningful
when you're extracting files (the "x" flag).  And instead of using "*" to
get all visible files, I used "." to get the current directory and
everything
under it.  (Using "*" would skip any file in /home whose name starts with a
".", such as ".profile".)

But really I'd probably use "cp -R" myself.

Quote:> However, I would like to fully understand the command before I use it.
> Manual pages tell me what the options c, v, p, f, x do.  I don't know what
> the "-", "|", and paranthesis do.  I can see what the command is doing
> overall, but I don't understand the specific syntax mentioned.  If anyone
> can shed light on this for me, or give me a link to a more comprehensive
> explanation of tar, I would appreciate it.

Okay, I'll give it a shot.

tar's first argument is a collection of flags that tell it what to do.
You've already figured that part out.  You have two tar commands here
-- one with a "c" flag which creates an archive, and one with an "x"
flag that extracts files from an archive.

Both tar commands are also using the "f" flag, which tells tar that the
next argument is the name of the archive.  Both tar commands use "-" as
the archive name.  This is a special case.  When creating an archive,
"-" means the archive data should be written to stdout.  When reading
the archive, "-" means the archive data should be read from stdin.

(This, by the way, is why the first tar command shouldn't have a "v"
flag.  Normally the verbose file list is written to stdout, but that
tar command is trying to write the archive data to stdout, so the two
outputs could get mixed.  I'm pretty sure the Gnu version of tar works
around this by writing the verbose list to stderr when the archive is
stdout... but other versions of tar won't always do that, so it's a
bad habit to get into.  NEVER USE "v" WITH "cf -"!  And even with Gnu
tar, using "v" with both tar commands would simply cause each file name
to be listed twice, which could be confusing.)

The "|" character is the pipe symbol.  It connects the left-hand program's
stdout to the right-hand program's stdin.  This will allow the two tar
commands to communicate without using a temporary file.

The parentheses are used for grouping commands together.  When bash
parses a command line that uses parentheses, it forks off a separate
copy of bash to handle the commands inside the parentheses, so the
command is basically equivalent to...

        cd /home; tar cf - . | bash -c "cd /newhome; tar xvpf -"

In this particular case, it's just a way to make the second tar command
run with a different current directory.

Putting it all together, the first tar runs in the /home directory,
and builds an archive containing all the files in that directory or
its subdirectories.  In the archive, all of the file names will be
relative to "." (which is always the current directory, no matter
what directory that happens to be at the time).  The archive is
sent through a pipe to the second tar command, which runs in /newhome.
It extracts the files.  Since the file names are all relative to ".",
it extracts them relative to its current directory, /newhome.

Suggested readings:

man bash
man cp

 
 
 

Help with tar syntax please

Post by Michael Heimin » Fri, 14 Jun 2002 02:20:14



Quote:> Hello,

> I'm planning on copying one of my partitions to a new one.
> Originally I was
> going to use Partimage, but I'd like to try it using tar.  Some
> searching has shown me that the following command should work:

> cd/home; tar cvpf - * | (cd/newhome; tar xvpf -)

You'll lose every file/directory named .whatever, use:

cd/home; tar cvpf - . | (cd/newhome; tar xvpf -)

Quote:> the "-", "|", and paranthesis do.  I can see what the command is
> doing
> overall, but I don't understand the specific syntax mentioned.  If
> anyone can shed light on this for me, or give me a link to a more
> comprehensive explanation of tar, I would appreciate it.

'tar cvpf - .' means use STDOUT instead of a file, '|' (pipe) means
redirect STDOUT of the previous command, to the next one, 'tar'
again as STDIN to tar which uses the '-f -' switch again to get
STDIN instead of a filename/tape device/whatever.

However, 'cd /home; cp -a . /newhome' should do the same.

Michael Heiming
--
Remove the +SIGNS case mail bounces.

 
 
 

Help with tar syntax please

Post by Slugg » Fri, 14 Jun 2002 02:25:21


thanks,

It looks like part of my question had do with bash.  Your reply along with
some more searching has almost answered most of my questions.

cd/home;  semi-colon breaks up the line into separate commands
tar cvpf - *    can you explain what the "-" does?  is this for
stdout/stdin?
|   bash symbol for pipeline inputs and outputs
parenthesis used to group the two commands together, just like arithmetic.

So my last remaining puzzle is what does the "-" do?

thanks again,
sluggo


> 1st: change to /home directory
> ( cd /home ; )
> now: tar the current directory, outputing the tarfile to stdout
>      ( tar cvpf - *)
>      pipe stdout from the tar into a shell that
> a) has changed to /newhome directory
> (cd /newhome ; )
> b) untars stdin

> Lew Pitcher, Information Technology Consultant, Toronto Dominion Bank
Financial Group

> (Opinions expressed are my own, not my employer's.)

 
 
 

Help with tar syntax please

Post by Slugg » Fri, 14 Jun 2002 02:28:49


oops, sorted.  I just received some more replies, thanks everyone.

sluggo


> thanks,

> It looks like part of my question had do with bash.  Your reply along with
> some more searching has almost answered most of my questions.

> cd/home;  semi-colon breaks up the line into separate commands
> tar cvpf - *    can you explain what the "-" does?  is this for
> stdout/stdin?
> |   bash symbol for pipeline inputs and outputs
> parenthesis used to group the two commands together, just like arithmetic.

> So my last remaining puzzle is what does the "-" do?

> thanks again,
> sluggo


> > 1st: change to /home directory
> > ( cd /home ; )
> > now: tar the current directory, outputing the tarfile to stdout
> >      ( tar cvpf - *)
> >      pipe stdout from the tar into a shell that
> > a) has changed to /newhome directory
> > (cd /newhome ; )
> > b) untars stdin

> > Lew Pitcher, Information Technology Consultant, Toronto Dominion Bank
> Financial Group

> > (Opinions expressed are my own, not my employer's.)

 
 
 

Help with tar syntax please

Post by Slugg » Fri, 14 Jun 2002 02:39:18


Thanks for the help,

I believe I would lose (symbolic) links if I use the cp command?  Last night
I first tried copying all files from one partition to another, but I
received several errors about links not being updated, changed, or something
like that.
Oh, I just read what -a does: preserves links and attributes.
It sounds like either method will work, cool.

sluggo


> However, 'cd /home; cp -a . /newhome' should do the same.

> Michael Heiming
> --
> Remove the +SIGNS case mail bounces.

 
 
 

1. Please help with tar syntax.

I am in need of some actual examples of commands to create tar files.
I have a directory I want to turn into a tar.gz file.
I need it to include all files and sub directory's with ownership, mode
and everything intact.

Please post answers here.
Thank you
Roy

  webmaster.vcf
< 1K Download

2. vesafb & multiple XFree v4 resolutions

3. help on tar command and its syntax.

4. which library for GUI is best?

5. help: tar syntax

6. Installing Linux

7. Help with samples of regular expressions syntax please

8. ndbm for apache on linux

9. Apache:mod_rewrite syntax help please

10. Please help me with Syntax problem- Parsing Mail Files

11. Sendmail problems. Please please please please help!

12. PLEASE PLEASE HELP Major problem with my SCSI & Redhat 5.0 PLEASE Help

13. NFS please help me please please please sniff