github.com/xhghs/rclone@v1.51.1-0.20200430155106-e186a28cced8/cmd/copy/copy.go (about)

     1  package copy
     2  
     3  import (
     4  	"context"
     5  	"github.com/rclone/rclone/cmd"
     6  	"github.com/rclone/rclone/fs/config/flags"
     7  	"github.com/rclone/rclone/fs/operations"
     8  	"github.com/rclone/rclone/fs/sync"
     9  	"github.com/spf13/cobra"
    10  )
    11  
    12  var (
    13  	createEmptySrcDirs = false
    14  )
    15  
    16  func init() {
    17  	cmd.Root.AddCommand(commandDefinition)
    18  	cmdFlags := commandDefinition.Flags()
    19  	flags.BoolVarP(cmdFlags, &createEmptySrcDirs, "create-empty-src-dirs", "", createEmptySrcDirs, "Create empty source dirs on destination after copy")
    20  }
    21  
    22  var commandDefinition = &cobra.Command{
    23  	Use:   "copy source:path dest:path",
    24  	Short: `Copy files from source to dest, skipping already copied`,
    25  	Long: `
    26  Copy the source to the destination.  Doesn't transfer
    27  unchanged files, testing by size and modification time or
    28  MD5SUM.  Doesn't delete files from the destination.
    29  
    30  Note that it is always the contents of the directory that is synced,
    31  not the directory 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  
    35  If dest:path doesn't exist, it is created and the source:path contents
    36  go there.
    37  
    38  For example
    39  
    40      rclone copy source:sourcepath dest:destpath
    41  
    42  Let's say there are two files in sourcepath
    43  
    44      sourcepath/one.txt
    45      sourcepath/two.txt
    46  
    47  This copies them to
    48  
    49      destpath/one.txt
    50      destpath/two.txt
    51  
    52  Not to
    53  
    54      destpath/sourcepath/one.txt
    55      destpath/sourcepath/two.txt
    56  
    57  If you are familiar with ` + "`rsync`" + `, rclone always works as if you had
    58  written a trailing / - meaning "copy the contents of this directory".
    59  This applies to all commands and whether you are talking about the
    60  source or destination.
    61  
    62  See the [--no-traverse](/docs/#no-traverse) option for controlling
    63  whether rclone lists the destination directory or not.  Supplying this
    64  option when copying a small number of files into a large destination
    65  can speed transfers up greatly.
    66  
    67  For example, if you have many files in /path/to/src but only a few of
    68  them change every day, you can to copy all the files which have
    69  changed recently very efficiently like this:
    70  
    71      rclone copy --max-age 24h --no-traverse /path/to/src remote:
    72  
    73  **Note**: Use the ` + "`-P`" + `/` + "`--progress`" + ` flag to view real-time transfer statistics
    74  `,
    75  	Run: func(command *cobra.Command, args []string) {
    76  		cmd.CheckArgs(2, 2, command, args)
    77  		fsrc, srcFileName, fdst := cmd.NewFsSrcFileDst(args)
    78  		if(len(fsrc.Root()) > 7 && "isFile:" == fsrc.Root()[0:7]){
    79  			srcFileName = fsrc.Root()[7:]
    80  		}
    81  		cmd.Run(true, true, command, func() error {
    82  			if srcFileName == "" {
    83  				return sync.CopyDir(context.Background(), fdst, fsrc, createEmptySrcDirs)
    84  			}
    85  			return operations.CopyFile(context.Background(), fdst, fsrc, srcFileName, srcFileName)
    86  		})
    87  	},
    88  }