github.com/ncw/rclone@v1.48.1-0.20190724201158-a35aa1360e3e/cmd/move/move.go (about)

     1  package move
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/ncw/rclone/cmd"
     7  	"github.com/ncw/rclone/fs/operations"
     8  	"github.com/ncw/rclone/fs/sync"
     9  	"github.com/spf13/cobra"
    10  )
    11  
    12  // Globals
    13  var (
    14  	deleteEmptySrcDirs = false
    15  	createEmptySrcDirs = false
    16  )
    17  
    18  func init() {
    19  	cmd.Root.AddCommand(commandDefintion)
    20  	commandDefintion.Flags().BoolVarP(&deleteEmptySrcDirs, "delete-empty-src-dirs", "", deleteEmptySrcDirs, "Delete empty source dirs after move")
    21  	commandDefintion.Flags().BoolVarP(&createEmptySrcDirs, "create-empty-src-dirs", "", createEmptySrcDirs, "Create empty source dirs on destination after move")
    22  }
    23  
    24  var commandDefintion = &cobra.Command{
    25  	Use:   "move source:path dest:path",
    26  	Short: `Move files from source to dest.`,
    27  	Long: `
    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  
    32  If no filters are in use and if possible this will server side move
    33  ` + "`source:path`" + ` into ` + "`dest:path`" + `. After this ` + "`source:path`" + ` will no
    34  longer longer exist.
    35  
    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  
    42  If you want to delete empty source directories after move, use the --delete-empty-src-dirs flag.
    43  
    44  See the [--no-traverse](/docs/#no-traverse) option for controlling
    45  whether rclone lists the destination directory or not.  Supplying this
    46  option when moving a small number of files into a large destination
    47  can speed transfers up greatly.
    48  
    49  **Important**: Since this can cause data loss, test first with the
    50  --dry-run flag.
    51  
    52  **Note**: Use the ` + "`-P`" + `/` + "`--progress`" + ` flag to view real-time transfer statistics.
    53  `,
    54  	Run: func(command *cobra.Command, args []string) {
    55  		cmd.CheckArgs(2, 2, command, args)
    56  		fsrc, srcFileName, fdst := cmd.NewFsSrcFileDst(args)
    57  		cmd.Run(true, true, command, func() error {
    58  			if srcFileName == "" {
    59  				return sync.MoveDir(context.Background(), fdst, fsrc, deleteEmptySrcDirs, createEmptySrcDirs)
    60  			}
    61  			return operations.MoveFile(context.Background(), fdst, fsrc, srcFileName, srcFileName)
    62  		})
    63  	},
    64  }