Copy a full folder to another location on the same server
Solution 1
If you want to keep in sync two folders, using the command cp
is the wrong approach. It will only make a copy, that is sync only at the moment when you perform that command.
It depends on your purposes, but to keep in sync two folders I would use rsync
command within a cronjob
Anyway, if you want to do it manually, your command is almost fine, just a little fix:
cp -urp /home/my_site/public_html/wp-content/uploads/ /home/my_site/public_html/dev/wp-content/
Solution 2
Your cp
command and flags appear to meet the stated requirements except for one that you didn't state but that I feel is implied. If you delete a file from the source folder, your CP command would not do anything with the destination folder, which may be your desired action. The word "sync" suggests you want the two folders to be identical, which means leaving an 'orphaned' copy of a file wouldn't meet this implied requirement.
I would suggest you consider rsync
to do the task. It has many options and capabilities and is specifically for synchronizing files between two locations. The following would probably work just fine for your needs:
rsync -avhd /source /target
Related videos on Youtube
Comments
-
JayGarcia almost 2 years
I would like to make a backup of a WordPress uploads folder from a a master site to a development site.
I thought I would give this code a try:
cp -urp /home/my_site/public_html/wp-content/uploads/* /home/my_site/public_html/dev/wp-content/uploads
Can you confirm this is correct before I do any damage, or advise about how should I proceed with this task?
-
lese over 8 yearsWhich folder you would like to keep in sync?
/home/my_site/public_html/wp-content/uploads
? -
JayGarcia over 8 yearsYes, that's the master folder, and in /home/my_site/public_html/dev i have a copy of my site I use for development purposes
-
-
JayGarcia over 8 yearsYou are right, I don't really care about syncing, I just care about the the destination folder having at least the same files as the source. It might have some more files (created while testing the site) which I don't care about. I will update my question.
-
JayGarcia over 8 yearsIs rsync present by default in an Ubuntu installation?
-
0xSheepdog over 8 yearsI believe so, but if not it should be a standard package from the Ubuntu repos. Try
which rsync
and if no results,locate rsync
. If neither turns up the executable,apt-get install rsync
should download and install the package. -
JayGarcia over 8 yearsThanks I appreciate your concern, but I feel cp is fine for my simple purpose, I have no need for sync, just need a copy of master uploaded files to the development folder. If some file is added to development and it's not present on master, I don't care.
-
JayGarcia over 8 yearsNo worries, i develop locally, then deploy to develop and people mess around with it. What i want is to give them actual assets when they mess up with development site (since I will weekly sync the two databases (manually, unfortunately)