I would like to maintain a mirror copy of many files and directories using cp, but I cannot get it to function (similar to MS XCOPY with switches)

I am trying to copy SOURCE to DESTINATION, so that DESTINATION is identical to SOURCE.

Note that SOURCE is 89GB with 26k files and hundreds of folders, so it is not practical to do an entire copy when only 5 or 10 files may have changed.

I realize that I could use an incremental backup app, but I need the files to be directly accessible and editable since DESTINATION is a USB hard drive that is plugged into another computer (also running Ubuntu).

I have tried numerous variations of the following but I cannot get it to work. It either does not recurse subdirectories or identify changed files. Most times the processor returns to an open command line as if it just successfully completed the operation.

I thought the problem may be related to the folder and file names which contain spaces. If I had created these folder and files, I would have used _ instead of " ", but that is not an option. All of the files are usable within Ubuntu applications. I have used the -v switch, but nothing is printed.

In Ubuntu 20.04 I enter the following within the SOURCE directory:

cp "*.*" -i -r -u -v /media/kevin/'MyDocs Backup'/Documents

I have also tried it using the explicit SOURCENAME but that does even less.

If it helps to relay my question, the XCOPY equivalent is from the is:

XCOPY "*.*" K:\Documents /d /s /c

Use rsync:

Rsync is a fast and extraordinarily versatile file copying tool. It can copy locally, to/from another host over any remote shell, or to/from a remote rsync daemon. It offers a large number of options that control every aspect of its behavior and permit very flexible specification of the set of files to be copied. It is famous for its delta-transfer algorithm, which reduces the amount of data sent over the network by sending only the differences between the source files and the existing files in the destination. Rsync is widely used for backups and mirroring and as an improved copy command for everyday use.

rsync -av SOURCE DESTINATION
  • -a means "archive mode" which is an aggregation of several other options

    "archive mode [...] ensures that symbolic links, devices, attributes, permissions, ownerships, etc. are preserved in the transfer.")

  • -v means "verbose", so we see a status on terminal what has been copied.

Check man rsync for more details and options.


Note the difference between SOURCE and SOURCE/:

A trailing slash on the source changes this behavior to avoid creating an additional directory level at the destination. You can think of a trailing / on a source as meaning "copy the contents of this directory" as opposed to "copy the directory by name"