copy.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Package copy provides the copy command.
  2. package copy
  3. import (
  4. "context"
  5. "strings"
  6. "github.com/rclone/rclone/cmd"
  7. "github.com/rclone/rclone/fs/config/flags"
  8. "github.com/rclone/rclone/fs/operations"
  9. "github.com/rclone/rclone/fs/sync"
  10. "github.com/spf13/cobra"
  11. )
  12. var (
  13. createEmptySrcDirs = false
  14. )
  15. func init() {
  16. cmd.Root.AddCommand(commandDefinition)
  17. cmdFlags := commandDefinition.Flags()
  18. flags.BoolVarP(cmdFlags, &createEmptySrcDirs, "create-empty-src-dirs", "", createEmptySrcDirs, "Create empty source dirs on destination after copy", "")
  19. }
  20. var commandDefinition = &cobra.Command{
  21. Use: "copy source:path dest:path",
  22. Short: `Copy files from source to dest, skipping identical files.`,
  23. // Note: "|" will be replaced by backticks below
  24. Long: strings.ReplaceAll(`
  25. Copy the source to the destination. Does not transfer files that are
  26. identical on source and destination, testing by size and modification
  27. time or MD5SUM. Doesn't delete files from the destination. If you
  28. want to also delete files from destination, to make it match source,
  29. use the [sync](/commands/rclone_sync/) command instead.
  30. Note that it is always the contents of the directory that is synced,
  31. not the directory itself. So when source:path is a directory, it's the
  32. contents of source:path that are copied, not the directory name and
  33. contents.
  34. To copy single files, use the [copyto](/commands/rclone_copyto/)
  35. command instead.
  36. If dest:path doesn't exist, it is created and the source:path contents
  37. go there.
  38. For example
  39. rclone copy source:sourcepath dest:destpath
  40. Let's say there are two files in sourcepath
  41. sourcepath/one.txt
  42. sourcepath/two.txt
  43. This copies them to
  44. destpath/one.txt
  45. destpath/two.txt
  46. Not to
  47. destpath/sourcepath/one.txt
  48. destpath/sourcepath/two.txt
  49. If you are familiar with |rsync|, rclone always works as if you had
  50. written a trailing |/| - meaning "copy the contents of this directory".
  51. This applies to all commands and whether you are talking about the
  52. source or destination.
  53. See the [--no-traverse](/docs/#no-traverse) option for controlling
  54. whether rclone lists the destination directory or not. Supplying this
  55. option when copying a small number of files into a large destination
  56. can speed transfers up greatly.
  57. For example, if you have many files in /path/to/src but only a few of
  58. them change every day, you can copy all the files which have changed
  59. recently very efficiently like this:
  60. rclone copy --max-age 24h --no-traverse /path/to/src remote:
  61. Rclone will sync the modification times of files and directories if
  62. the backend supports it. If metadata syncing is required then use the
  63. |--metadata| flag.
  64. Note that the modification time and metadata for the root directory
  65. will **not** be synced. See https://github.com/rclone/rclone/issues/7652
  66. for more info.
  67. **Note**: Use the |-P|/|--progress| flag to view real-time transfer statistics.
  68. **Note**: Use the |--dry-run| or the |--interactive|/|-i| flag to test without copying anything.
  69. `, "|", "`"),
  70. Annotations: map[string]string{
  71. "groups": "Copy,Filter,Listing,Important",
  72. },
  73. Run: func(command *cobra.Command, args []string) {
  74. cmd.CheckArgs(2, 2, command, args)
  75. fsrc, srcFileName, fdst := cmd.NewFsSrcFileDst(args)
  76. cmd.Run(true, true, command, func() error {
  77. if srcFileName == "" {
  78. return sync.CopyDir(context.Background(), fdst, fsrc, createEmptySrcDirs)
  79. }
  80. return operations.CopyFile(context.Background(), fdst, fsrc, srcFileName, srcFileName)
  81. })
  82. },
  83. }