Rename folder in Google Cloud Storage (GCS)

To rename a folder in Google Cloud Storage (GCS), say, gs://your-bucket/folderA to gs://your-bucket/folderB, you can use the gsutil mv command. However, the tricky part is that you have to go inside folderA to list all sub-folders and objects, otherwise you’ll ended up with an incorrect move result.

Here’s the snippet to rename the files correctly. Also, It’s better to run on a Google VM so that you don’t incur additional bandwidth charges and with best performance.

gsutil ls gs://your-bucket/folderA | xargs -P 9 -I {} gsutil -m mv {} gs://your-bucket/folderB

The trick is that we’ll have to do an ls on folderA, then pipe it through xargs with the -P 9 to run 9 processes in parallel, and also use the gsutil -m flag to enable multi-threading. The -I {} for xargs lets us define the placeholder so we can freely format the command that xagrs triggers.

Happy moving files!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.