Solved

unix cp command

Posted on 2011-09-20
13
642 Views
Last Modified: 2012-06-22
could I use the cp command to copy recursively a directory to another directory, and over-write what is in the destination directory, so something like

cp -R /source /destination

0
Comment
Question by:JeffBeall
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +2
13 Comments
 
LVL 21

Expert Comment

by:Papertrip
ID: 36570854
If you want to copy the contents of /source, then just add a * at the end
cp -R /source/* /destination

Open in new window

Without it, the /source directory will be copied into /destination as a subdirectory of it.
0
 
LVL 16

Expert Comment

by:Joseph Gan
ID: 36570865
Also can add "-p" to preserve ownerid, group id and permission etc.
0
 
LVL 1

Author Comment

by:JeffBeall
ID: 36570868
so i don't need to do anything special to over write the destination?
i ask because i already ran

cp -R /source /destination

a little while ago
0
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
LVL 21

Expert Comment

by:Papertrip
ID: 36570879
No you don't need anything special.

If you ran that command exactly how you typed it, then you will end up with '/destination/source/'
0
 
LVL 16

Accepted Solution

by:
Joseph Gan earned 125 total points
ID: 36570888
Correct, it will over write everythings in destination directory without asking.
0
 
LVL 1

Author Comment

by:JeffBeall
ID: 36570889
ok, i'll try it.
0
 
LVL 3

Assisted Solution

by:RitBit
RitBit earned 125 total points
ID: 36572990
To force overwriting and keep permissions for a recursive copy try this:

cp -Rpf <SOURCE>  <DESTINATION>

R -> Recursive
p -> keep permissions
f -> force overwrite all
0
 
LVL 19

Assisted Solution

by:xterm
xterm earned 125 total points
ID: 36574274
No iteration of cp will "merge" the two recursively - the source will always end up inside the destination directory, and I don't think that is what you want.

If you would like the entire contents of foo/*/* to end up IN bar/*/* then do this:

(cd /foo && tar cpf - . ) | (cd /bar && tar xvfp -)
0
 
LVL 1

Author Comment

by:JeffBeall
ID: 36574688
i don't really need to merge directories. overwriting in this case is fine, so will RitBit's solution work?
0
 
LVL 21

Expert Comment

by:Papertrip
ID: 36574724
The first answer I gave is sufficient for what you said you need to do, did you try it?
0
 
LVL 1

Author Comment

by:JeffBeall
ID: 36575683
i did try it, but everything from the source directory ended up not in the destination directory that i wanted, and i ran out of space on my external drive. so in my setup i did this

cp -R /DELTAR /t4mbu

however, maybe i should have done

cp -R /DELTAR /t4mbu/DELTAR

since i ran out of disk space , i thought maybe the command doesn't over right the destination, of course now that i think of it, it didn't have anything to over right, since all the files were in
/t4mbu/DELTAR and not /t4mbu
0
 
LVL 21

Assisted Solution

by:Papertrip
Papertrip earned 125 total points
ID: 36575717
cp -R /DELTAR /t4mbu
since all the files were in/t4mbu/DELTAR and not /t4mbu

If you followed my instructions, then the contents of /DELTAR would be under /t4mbu, instead of /t4mbu/DELTAR


If you want to copy the contents of /source, then just add a * at the end
cp -R /source/* /destination

Open in new window

Without it, the /source directory will be copied into /destination as a subdirectory of it.
0
 
LVL 1

Author Closing Comment

by:JeffBeall
ID: 36587159
thanks for the help.
0

Featured Post

Industry Leaders: 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

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
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…

688 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