Solved

Copying a Directory on a UNIX System

Posted on 2013-12-17
5
644 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 250 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
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…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

864 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now