> I'm planning on copying one of my partitions to a new one. Originally I
> 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
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.