How to Copy (cp) and Move (mv) Buckets with the gsutil Utility - dummies

How to Copy (cp) and Move (mv) Buckets with the gsutil Utility

By Matthew Scarpino

After you create a bucket with the gsutil utility on the Google Cloud Platform (GCP), you can upload files to it, thereby adding objects to the bucket. Similarly, you can download an object to your system as a file. Google makes these operations possible through the cp (copy) and mv (move) commands. Both commands transfer a source entity to a destination, but cp leaves the source entity in place while mv removes the source entity.

The best way to understand these commands is to look at some examples. The following command uploads a local file, hello.txt, to a bucket in Cloud Storage named gs://newbucket:

gsutil cp hello.txt gs://newbucket

Similarly, the following command moves hello.txt from gs://newbucket to the current directory on your development system. Note that mv removes hello.txt from the bucket:

gs mv gs://newbucket/hello.txt .

cp and mv accept many of the same flags as their counterparts in Linux and Unix. These flags include the following:

  • -r: Copy/move a directory and its contents
  • -L: Outputs a log file for each source entity of the copy/move
  • -e: Excludes symbolic links from the copy/move

For example, the following command moves the local mydir directory and its contents to firstbucket.

gsutil mv -r mydir gs://firstbucket

This command copies mydir and its contents from firstbucket to secondbucket:

gsutil cp -re gs://firstbucket/mydir gs://secondbucket

Because of the –e flag, gsutil won’t copy any symbolic links from mydir to secondbucket.