1 # -*- coding: utf-8 -*-
2 # Copyright 2011 Google Inc. All Rights Reserved.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 """Implementation of Unix-like mv command for cloud storage providers."""
17 from __future__
import absolute_import
19 from gslib
.command
import Command
20 from gslib
.command_argument
import CommandArgument
21 from gslib
.commands
.cp
import CP_SUB_ARGS
22 from gslib
.cs_api_map
import ApiSelector
23 from gslib
.exception
import CommandException
24 from gslib
.storage_url
import StorageUrlFromString
25 from gslib
.util
import NO_MAX
29 gsutil mv [-p] src_url dst_url
30 gsutil mv [-p] src_url... dst_url
31 gsutil mv [-p] -I dst_url
34 _DETAILED_HELP_TEXT
= ("""
40 The gsutil mv command allows you to move data between your local file
41 system and the cloud, move data within the cloud, and move data between
42 cloud storage providers. For example, to move all objects from a
43 bucket to a local directory you could use:
45 gsutil mv gs://my_bucket dir
47 Similarly, to move all objects from a local directory to a bucket you could
50 gsutil mv ./dir gs://my_bucket
53 <B>RENAMING BUCKET SUBDIRECTORIES</B>
54 You can use the gsutil mv command to rename subdirectories. For example,
57 gsutil mv gs://my_bucket/olddir gs://my_bucket/newdir
59 would rename all objects and subdirectories under gs://my_bucket/olddir to be
60 under gs://my_bucket/newdir, otherwise preserving the subdirectory structure.
62 If you do a rename as specified above and you want to preserve ACLs, you
63 should use the -p option (see OPTIONS).
65 Note that when using mv to rename bucket subdirectories you cannot specify
66 the source URL using wildcards. You need to spell out the complete name:
68 gsutil mv gs://my_bucket/olddir gs://my_bucket/newdir
70 If you have a large number of files to move you might want to use the
71 gsutil -m option, to perform a multi-threaded/multi-processing move:
73 gsutil -m mv gs://my_bucket/olddir gs://my_bucket/newdir
76 <B>NON-ATOMIC OPERATION</B>
77 Unlike the case with many file systems, the gsutil mv command does not
78 perform a single atomic operation. Rather, it performs a copy from source
79 to destination followed by removing the source for each object.
83 All options that are available for the gsutil cp command are also available
84 for the gsutil mv command (except for the -R flag, which is implied by the
85 gsutil mv command). Please see the OPTIONS sections of "gsutil help cp"
91 class MvCommand(Command
):
92 """Implementation of gsutil mv command.
94 Note that there is no atomic rename operation - this command is simply
95 a shorthand for 'cp' followed by 'rm'.
98 # Command specification. See base class for documentation.
99 command_spec
= Command
.CreateCommandSpec(
101 command_name_aliases
=['move', 'ren', 'rename'],
102 usage_synopsis
=_SYNOPSIS
,
105 # Flags for mv are passed through to cp.
106 supported_sub_args
=CP_SUB_ARGS
,
108 provider_url_ok
=False,
110 gs_api_support
=[ApiSelector
.XML
, ApiSelector
.JSON
],
111 gs_default_api
=ApiSelector
.JSON
,
113 CommandArgument
.MakeZeroOrMoreCloudOrFileURLsArgument()
116 # Help specification. See help_provider.py for documentation.
117 help_spec
= Command
.HelpSpec(
119 help_name_aliases
=['move', 'rename'],
120 help_type
='command_help',
121 help_one_line_summary
='Move/rename objects and/or subdirectories',
122 help_text
=_DETAILED_HELP_TEXT
,
123 subcommand_help_text
={},
126 def RunCommand(self
):
127 """Command entry point for the mv command."""
128 # Check each source arg up, refusing to delete a bucket src URL (force users
129 # to explicitly do that as a separate operation).
130 for arg_to_check
in self
.args
[0:-1]:
131 url
= StorageUrlFromString(arg_to_check
)
132 if url
.IsCloudUrl() and (url
.IsBucket() or url
.IsProvider()):
133 raise CommandException('You cannot move a source bucket using the mv '
134 'command. If you meant to move\nall objects in '
135 'the bucket, you can use a command like:\n'
136 '\tgsutil mv %s/* %s' %
137 (arg_to_check
, self
.args
[-1]))
139 # Insert command-line opts in front of args so they'll be picked up by cp
140 # and rm commands (e.g., for -p option). Use undocumented (internal
141 # use-only) cp -M option, which causes each original object to be deleted
142 # after successfully copying to its destination, and also causes naming
143 # behavior consistent with Unix mv naming behavior (see comments in
145 unparsed_args
= ['-M']
146 if self
.recursion_requested
:
147 unparsed_args
.append('-R')
148 unparsed_args
.extend(self
.unparsed_args
)
149 self
.command_runner
.RunNamedCommand('cp', unparsed_args
, self
.headers
,
150 self
.debug
, self
.parallel_operations
)