?
Solved

Copying a Directory on a UNIX System

Posted on 2013-12-17
5
Medium Priority
?
657 Views
Last Modified: 2013-12-31
I've always preferred using the tar command instead of the cp command to copy the content of a directory on a UNIX system. That's because I found that using the cp command was unpredictable and unreliable. I know there are those who are fond of cpio. Anyway, I was just wondering whether my misgivings about the cp command are justified.
0
Comment
Question by:babyb00mer
5 Comments
 
LVL 19

Expert Comment

by:xterm
ID: 39724519
I only encounter one real issue when using cp vs. tar, and that is that if your source directory has the same name as the destination, it will end up inside the destination directory.

So for jobs that I do repetitively, I prefer:

(cd /source/directory && tar cpf - dir1 dir2 dirX ) | (cd /dest/directory && tar xvfp -)

over:

cp -Rv /source/directory /dest/directory

But to answer your question, misgivings are much the same as preferences which means you don't have to "justify" them per se - you should use what gets the job done and poses the least stress or risk to you.
0
 

Author Comment

by:babyb00mer
ID: 39732588
Hmm. I'm wondering whether tar is faster than cp.
0
 
LVL 19

Expert Comment

by:xterm
ID: 39732592
No, tar isn't any faster - from a system standpoint, they're doing virtually the identical thing.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 39732624
>> unpredictable and unreliable
How is it unpredictable?
How is it unreliable?
Curious - are you using NFS?

For performance considerations, if the cp is over a network and if you have 1000's of files being copied, then transmitting a tar archive, a single file, incurs less overhead than transmitting many files.
0
 
LVL 4

Accepted Solution

by:
Anacreo earned 1000 total points
ID: 39744605
cpio is more reliable than tar in my opinion, file name lengths and directory loops can become issues, hard links as well.

find . | cpio -pdum /var/tmp/target

tar and cpio are old and have numerous limitations that can bite you... a more modern solution is to use pax:
    mkdir /tmp/to
    cd /tmp/from
    pax -rw . /tmp/to


But on any system that has rsync, why not go with the gold standard.
rsync -az -H --delete /path/to/source /path/to/dest
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Suggested Courses
Course of the Month16 days, 23 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question