Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

up vote 8 down vote accepted

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.

share|improve this answer
3  
Taking all the buffering that's going on, cp and tar operate essentially identically: read some stuff until RAM gets nearly saturated, then write some stuff, and repeat. Do you have benchmarks that show a significant speed advantage to tar over cp? –  Gilles Aug 30 '11 at 22:58
    
@Gilles: The buffering may or may not be happening; FWIW, the fwrite()s could very well be fully synchronous for that particular mountpoint. Using tar would abstract this away somewhat - but in practice, I agree that buffering will erase the difference in 99% of the cases. –  Piskvor Aug 31 '11 at 12:39
add comment

The command performs an exact archive-like copy of a directory sub-tree (i.e. where tar preservers permissions, owner-ship, links etc.).

First, it the command

tar cpf - . | ( cd /var/ftp/ks/centos/; tar xpf - )

is equivalent to

tar cpf - . | tar -C /var/ftp/ks/centos -x -p -f -

where -C specifies the working directory tar should change to before starting to extract the files.

Probably, the colleague just was not aware of the -C option.

Also kind of equivalent is:

cp -a * /var/ftp/ks/centos/

(where -a or --archive stands for -r --preserve=all --no-dereference --preserve=links, i.e. cp preserves then permissions, copies recursively etc. like one expects when doing an exact archive-like copy)

It is not equivalent because the shell pattern matching * does not catch the dot-files in the current working directory. With a different source pattern the reason to choose tar over cp -a is that cp -a is a GNU extension and not POSIX, i.e. a tar solution would be a more portable one.

There are no performance reasons for choosing tar over cp.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.