command isn't just for tape archives.
It can copy files from disk to disk, too.
And even if your computer has
there are advantages to using tar
The obvious way to copy directories with tar
is to write them onto
a tape archive with relative pathnames - then read back the tape and
write it somewhere else on the disk.
can also write to a UNIX pipe - and read from a pipe.
This looks like:
with one trick:
has a different current directory (38.3
(the place where you want the copy made) than the
To do that, run the
The argument(s) to the
can be directory(s) or file(s).
Just be sure to use
relative pathnames (14.2
that don't start with a slash - otherwise, the
will write the
copies in the same place the originals came from!
"How about an example," you ask?
It copies from the directory /home/jane
, with all its files and
The copy is made in the directory /work/bkup/jane
tar cf - . | (cd /work/bkup/jane && tar xBf -)
tells the shell to start
only if the previous command
That prevents tar
writing files into the same directory it's reading
from - if the destination directory isn't accessible or you flub its pathname.
If your tar
has a B
(reblocking) option, use it to help be sure
that the copy is made
If your tar
doesn't have a reblocking option, you can use this
trick suggested by Chris Torek:
tar cf - . | cat | (cd /work/backup/jane && tar xbf 1 -)
At least one tar
version has a v
(verbose) option that
writes the verbose text to standard output instead of standard error ! (19.8
If your tar
does that, don't use v
that feeds the pipe)-use v
You can use other options that your tar
excluding files or directories (20.8
Symbolic links (18.4
will be copied exactly.
If they point to relative pathnames, the copied links might point
to locations that don't exist.
You can search for these symbolic links with
find -type l
hard link (18.4
will be copied as a file.
If there are more hard links to that file in the files you're copying,
they will be linked to the copy of the first link.
That can be good because the destination might be on a different
filesystem (a hard link to the original file can't work then).
It can be bad if the link pointed to a really big file;
the copy can take a lot of disk space.
You can search for these hard links by:
Searching the directory from which you're copying with
find -links +1 -type f
to find all files that have more than one link, and
with its l
(lowercase letter L) option
to complain if it didn't copy all links to a file.
If your system has
you can run the
on a remote system.
For example, to copy a directory to the computer named kumquat
rsh kumquat mkdir /work/bkup/jane
tar cf - . | rsh kumquat 'cd /work/bkup/jane && tar xBf -'