Copy a full folder to another location on the same server

5,556

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
Share:
5,556

Related videos on Youtube

JayGarcia
Author by

JayGarcia

I do web!

Updated on September 18, 2022

Comments

  • JayGarcia
    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
      lese over 8 years
      Which folder you would like to keep in sync? /home/my_site/public_html/wp-content/uploads ?
    • JayGarcia
      JayGarcia over 8 years
      Yes, 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
    JayGarcia over 8 years
    You 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
    JayGarcia over 8 years
    Is rsync present by default in an Ubuntu installation?
  • 0xSheepdog
    0xSheepdog over 8 years
    I 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
    JayGarcia over 8 years
    Thanks 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
    JayGarcia over 8 years
    No 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)