suppose we have a directory containing 2 sub directories from-stuff
and to-stuff, from-stuff contains an entire tree of files , symbolic links and so forth. something that is difficult to mirror precisely using a recursive cp. in order to mirror the entire tree beneath from-stuff to to-stuff , we could use the commands:
cd from-stuff
tar cf - . | (cd ../to-stuff; tar xvf -)
source:- Running Linux
Grant edwards
The '-' is in the spot where tar expects to see the filename of the archive to be created/read. Using '-' for a filename tells tar to write the archive to stdout (or stdin when an archive is
being read) instead of a "regular" named file.
One would hope that would be explained on tar's man/info page, but it isn't -- the man/info page only explains options in their "standard" notation like this:
tar c -f -
That said, specifying '-' to use stdout/stdin is a bit redundant since that's the default. The example line would work just as well like this:
tar c . | (cd ../to-stuff; tar xv)
Or this
tar c . | tar xv -C ../to-stuff
But I don't really see why "cp -a" won't work. It handles links, device files, permissions, etc.
kiaaze
Code: tar c . | (cd ../to-stuff; tar xv)
works too, since tar uses standard output/input by default.
robert heller
Depends on the version of tar. For stock commercial UNIX tar, the default is still the tape drive. It appears that GNU Tar defaults to stdin/stdout. Traditionally, tar reads/writes to the tape drive and some versions might still do so by default. If you don't know what version of tar you are using, adding '-f -' will ALWAYS work, even if it is redundant. It certainly does not hurt.
rainer krienke
the problem with this tar-trick is that if you suppose that from-stuff is not just a small directory but say eg 1TByte in size with million of files in it.
Tar of course works but if anything goes wrong in such a tar run, (perhaps the to-stuff filesystem gets full or some files are modified during the run of tar that might take hours for a huge from-stuff dir, then all you can do is to start from scratch i.e. copy all files again.
In such situations its better to use rsync because rsync does only synchronize i.e. copy files that are different in from-stuff and to-stuff. Even if you stop rsync in the middle of its work and start it again it won't copy all files again like tar would, it would only copy those files not already in to-stuff. If you modify a file in from-stuff and start rsync again it will find this file and copy only this one instead of the whole TerraByte of data. Actually rsync is even better. if only a some bytes of one file in from-stuff changed it will transfer only those bytes that changed and insert them into the already existing file in to-stuff. This is important if you eg copy many data across a slow network link.
The command also preserves symlinks, permissions and ownership and using appropiate options also hard links and in its basic form that does all this its really simple:
rsync -av from-stuff to-stuff
thats all. Call it for the first time it will copy all files and directories. Call it a second time it will only copy files that have changed in from-stuff since the last run.
No comments:
Post a Comment