2022-08-28 19:21:57 +08:00
|
|
|
// Package move provides the move command.
|
2016-08-06 00:12:27 +08:00
|
|
|
package move
|
|
|
|
|
|
|
|
import (
|
2019-06-17 16:34:30 +08:00
|
|
|
"context"
|
2020-05-25 15:50:20 +08:00
|
|
|
"strings"
|
2019-06-17 16:34:30 +08:00
|
|
|
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/cmd"
|
2019-10-11 23:55:04 +08:00
|
|
|
"github.com/rclone/rclone/fs/config/flags"
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/fs/operations"
|
|
|
|
"github.com/rclone/rclone/fs/sync"
|
2016-08-06 00:12:27 +08:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2017-11-27 19:42:02 +08:00
|
|
|
// Globals
|
|
|
|
var (
|
|
|
|
deleteEmptySrcDirs = false
|
2019-03-06 16:43:46 +08:00
|
|
|
createEmptySrcDirs = false
|
2017-11-27 19:42:02 +08:00
|
|
|
)
|
|
|
|
|
2016-08-06 00:12:27 +08:00
|
|
|
func init() {
|
2019-10-11 23:58:11 +08:00
|
|
|
cmd.Root.AddCommand(commandDefinition)
|
2019-10-11 23:55:04 +08:00
|
|
|
cmdFlags := commandDefinition.Flags()
|
2023-07-11 01:34:10 +08:00
|
|
|
flags.BoolVarP(cmdFlags, &deleteEmptySrcDirs, "delete-empty-src-dirs", "", deleteEmptySrcDirs, "Delete empty source dirs after move", "")
|
|
|
|
flags.BoolVarP(cmdFlags, &createEmptySrcDirs, "create-empty-src-dirs", "", createEmptySrcDirs, "Create empty source dirs on destination after move", "")
|
2016-08-06 00:12:27 +08:00
|
|
|
}
|
|
|
|
|
2019-10-11 23:58:11 +08:00
|
|
|
var commandDefinition = &cobra.Command{
|
2016-08-06 00:12:27 +08:00
|
|
|
Use: "move source:path dest:path",
|
|
|
|
Short: `Move files from source to dest.`,
|
2020-05-25 15:50:20 +08:00
|
|
|
// Warning! "|" will be replaced by backticks below
|
|
|
|
Long: strings.ReplaceAll(`
|
2016-08-06 00:12:27 +08:00
|
|
|
Moves the contents of the source directory to the destination
|
2016-10-23 00:53:52 +08:00
|
|
|
directory. Rclone will error if the source and destination overlap and
|
2020-10-14 05:43:40 +08:00
|
|
|
the remote does not support a server-side directory move operation.
|
2016-08-06 00:12:27 +08:00
|
|
|
|
2022-06-19 21:51:37 +08:00
|
|
|
To move single files, use the [moveto](/commands/rclone_moveto/)
|
|
|
|
command instead.
|
|
|
|
|
2020-10-14 05:43:40 +08:00
|
|
|
If no filters are in use and if possible this will server-side move
|
2020-05-25 15:50:20 +08:00
|
|
|
|source:path| into |dest:path|. After this |source:path| will no
|
2020-05-19 19:02:44 +08:00
|
|
|
longer exist.
|
2016-08-06 00:12:27 +08:00
|
|
|
|
2020-05-25 15:50:20 +08:00
|
|
|
Otherwise for each file in |source:path| selected by the filters (if
|
|
|
|
any) this will move it into |dest:path|. If possible a server-side
|
2020-10-14 05:43:40 +08:00
|
|
|
move will be used, otherwise it will copy it (server-side if possible)
|
2020-05-25 15:50:20 +08:00
|
|
|
into |dest:path| then delete the original (if no errors on copy) in
|
|
|
|
|source:path|.
|
2016-08-06 00:12:27 +08:00
|
|
|
|
2022-06-19 21:51:37 +08:00
|
|
|
If you want to delete empty source directories after move, use the
|
|
|
|
|--delete-empty-src-dirs| flag.
|
2017-11-27 19:42:02 +08:00
|
|
|
|
2018-11-26 00:49:38 +08:00
|
|
|
See the [--no-traverse](/docs/#no-traverse) option for controlling
|
|
|
|
whether rclone lists the destination directory or not. Supplying this
|
|
|
|
option when moving a small number of files into a large destination
|
|
|
|
can speed transfers up greatly.
|
|
|
|
|
2016-08-06 00:12:27 +08:00
|
|
|
**Important**: Since this can cause data loss, test first with the
|
2020-05-25 15:50:20 +08:00
|
|
|
|--dry-run| or the |--interactive|/|-i| flag.
|
2018-10-21 17:51:41 +08:00
|
|
|
|
2020-05-25 15:50:20 +08:00
|
|
|
**Note**: Use the |-P|/|--progress| flag to view real-time transfer statistics.
|
|
|
|
`, "|", "`"),
|
2022-11-27 06:40:49 +08:00
|
|
|
Annotations: map[string]string{
|
|
|
|
"versionIntroduced": "v1.19",
|
2023-07-11 01:34:10 +08:00
|
|
|
"groups": "Filter,Listing,Important,Copy",
|
2022-11-27 06:40:49 +08:00
|
|
|
},
|
2016-08-06 00:12:27 +08:00
|
|
|
Run: func(command *cobra.Command, args []string) {
|
|
|
|
cmd.CheckArgs(2, 2, command, args)
|
2018-05-08 01:13:17 +08:00
|
|
|
fsrc, srcFileName, fdst := cmd.NewFsSrcFileDst(args)
|
2016-12-05 00:52:24 +08:00
|
|
|
cmd.Run(true, true, command, func() error {
|
2018-05-08 01:13:17 +08:00
|
|
|
if srcFileName == "" {
|
2019-06-17 16:34:30 +08:00
|
|
|
return sync.MoveDir(context.Background(), fdst, fsrc, deleteEmptySrcDirs, createEmptySrcDirs)
|
2018-05-08 01:13:17 +08:00
|
|
|
}
|
2019-06-17 16:34:30 +08:00
|
|
|
return operations.MoveFile(context.Background(), fdst, fsrc, srcFileName, srcFileName)
|
2016-08-06 00:12:27 +08:00
|
|
|
})
|
|
|
|
},
|
|
|
|
}
|