Solved

unix cp command

Posted on 2011-09-20
13
606 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
  • 5
  • 4
  • 2
  • +2
13 Comments
 
LVL 21

Expert Comment

by:Papertrip
Comment Utility
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
Comment Utility
Also can add "-p" to preserve ownerid, group id and permission etc.
0
 
LVL 1

Author Comment

by:JeffBeall
Comment Utility
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
 
LVL 21

Expert Comment

by:Papertrip
Comment Utility
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
Comment Utility
Correct, it will over write everythings in destination directory without asking.
0
 
LVL 1

Author Comment

by:JeffBeall
Comment Utility
ok, i'll try it.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 3

Assisted Solution

by:RitBit
RitBit earned 125 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
thanks for the help.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
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.

743 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

16 Experts available now in Live!

Get 1:1 Help Now