Understanding tar Commands and Finding Better Alternatives

bashlinuxtar

I am going over some documentation pertaining to creating a distro repo. However, I cannot understand exactly what these commands perform:

# cd /mnt/
# ls
centos-1  centos-2  centos-3
# for i in 1 2 3 
> do
> cd /mnt/centos-$i
> tar cpf - . | ( cd /var/ftp/ks/centos/; tar xpf - )
> done

My (obviously incorrect) rundown of it is: Loop through every mounted CD (/mnt/centos-<index>) and create a tar of the folder preserving the permissions. Then, extract that tar file to another folder, again, preserving permissions.

If that is the case, why do it like this? Is there really not a better way? Or perhaps I'm missing the whole idea.

Best Answer

The reason why tar (or cpio) is recommended over cp for this procedure is because of how the tools operate.

cp operates on each file object in turn, reading it from disk and then writing it in its new location. Since the locations of the source and destination may not be close on the disk, this results in a lot of seeking between the locations.

tar and cpio read as much as possible in one go, and then write it into the archive. This means that the source files will be read one after another and the destination files will be written one after another (allowing for process switching, of course), resulting in much less seeking and hence less time taken.

Related Question