Copy a directory on Unix
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------
Take control of your privacy with Proton's trusted, Swiss-based, secure services.
Choose what you need and safeguard your digital life:
Mail: https://go.getproton.me/SH1CU
VPN: https://go.getproton.me/SH1DI
Password Manager: https://go.getproton.me/SH1DJ
Drive: https://go.getproton.me/SH1CT
Music by Eric Matyas
https://www.soundimage.org
Track title: Peaceful Mind
--
Chapters
00:00 Copy A Directory On Unix
00:16 Accepted Answer Score 117
00:45 Answer 2 Score 11
01:33 Answer 3 Score 9
01:43 Thank you
--
Full question
https://superuser.com/questions/222395/c...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#unix #bash #terminal
#avk47
ACCEPTED ANSWER
Score 117
cp -rf /source/path/ /destination/path/
-r
= recursive, copies all the sub-directories
-f
= force, if an existing destination file cannot be opened, remove it and try again
Note You should be careful when using the -f
flag because it will forcefully overwrite anything you copy to. Thank @Nifle for this suggestion.
You may want to use the * wildcard to copy all of the files in the directory if you need to.
ANSWER 2
Score 11
While the cp -R
answers are right (BTW the case of the flag on BSD must be capital, both are supported on linux), there is an old incantation involving tar:
$ tar cf - . | (cd DIR; tar xf - )
Why the heck would you do that? Because tar has a fairly sophisticated understanding of links both hard and symbolic.
Do you want you copy to replace existing symbolic links with one that have the same text? Or with links to the same target (adjusting relative paths to compensate)? Or with bitwise copies of the target?
If two files in the original are hard linked should the new structure have two copies of the data or just one?
Decisions, decisions. Tar has sensible defaults, but lets you be very specific about it.
ANSWER 3
Score 9
I like
cp -axv source dest
Rsync is another good tool for this
rsync -va source dest