move.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Package move provides the move command.
  2. package move
  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. // Globals
  13. var (
  14. deleteEmptySrcDirs = false
  15. createEmptySrcDirs = false
  16. )
  17. func init() {
  18. cmd.Root.AddCommand(commandDefinition)
  19. cmdFlags := commandDefinition.Flags()
  20. flags.BoolVarP(cmdFlags, &deleteEmptySrcDirs, "delete-empty-src-dirs", "", deleteEmptySrcDirs, "Delete empty source dirs after move", "")
  21. flags.BoolVarP(cmdFlags, &createEmptySrcDirs, "create-empty-src-dirs", "", createEmptySrcDirs, "Create empty source dirs on destination after move", "")
  22. }
  23. var commandDefinition = &cobra.Command{
  24. Use: "move source:path dest:path",
  25. Short: `Move files from source to dest.`,
  26. // Warning! "|" will be replaced by backticks below
  27. Long: strings.ReplaceAll(`
  28. Moves the contents of the source directory to the destination
  29. directory. Rclone will error if the source and destination overlap and
  30. the remote does not support a server-side directory move operation.
  31. To move single files, use the [moveto](/commands/rclone_moveto/)
  32. command instead.
  33. If no filters are in use and if possible this will server-side move
  34. |source:path| into |dest:path|. After this |source:path| will no
  35. longer exist.
  36. Otherwise for each file in |source:path| selected by the filters (if
  37. any) this will move it into |dest:path|. If possible a server-side
  38. move will be used, otherwise it will copy it (server-side if possible)
  39. into |dest:path| then delete the original (if no errors on copy) in
  40. |source:path|.
  41. If you want to delete empty source directories after move, use the
  42. |--delete-empty-src-dirs| flag.
  43. See the [--no-traverse](/docs/#no-traverse) option for controlling
  44. whether rclone lists the destination directory or not. Supplying this
  45. option when moving a small number of files into a large destination
  46. can speed transfers up greatly.
  47. Rclone will sync the modification times of files and directories if
  48. the backend supports it. If metadata syncing is required then use the
  49. |--metadata| flag.
  50. Note that the modification time and metadata for the root directory
  51. will **not** be synced. See https://github.com/rclone/rclone/issues/7652
  52. for more info.
  53. **Important**: Since this can cause data loss, test first with the
  54. |--dry-run| or the |--interactive|/|-i| flag.
  55. **Note**: Use the |-P|/|--progress| flag to view real-time transfer statistics.
  56. `, "|", "`"),
  57. Annotations: map[string]string{
  58. "versionIntroduced": "v1.19",
  59. "groups": "Filter,Listing,Important,Copy",
  60. },
  61. Run: func(command *cobra.Command, args []string) {
  62. cmd.CheckArgs(2, 2, command, args)
  63. fsrc, srcFileName, fdst := cmd.NewFsSrcFileDst(args)
  64. cmd.Run(true, true, command, func() error {
  65. if srcFileName == "" {
  66. return sync.MoveDir(context.Background(), fdst, fsrc, deleteEmptySrcDirs, createEmptySrcDirs)
  67. }
  68. return operations.MoveFile(context.Background(), fdst, fsrc, srcFileName, srcFileName)
  69. })
  70. },
  71. }