Solved

Copying a Directory on a UNIX System

Posted on 2013-12-17
5
643 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
Comment Utility
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
Comment Utility
Hmm. I'm wondering whether tar is faster than cp.
0
 
LVL 19

Expert Comment

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

Expert Comment

by:phoffric
Comment Utility
>> 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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

763 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

9 Experts available now in Live!

Get 1:1 Help Now