Reputation: 61
How do I change the extension of multiple files in GCP's Cloud Storage?
For example, in the following directory:
gs://[bucket-name]/filesdirectory/
I want to change the files with extension .ipynb to .py.
Upvotes: 3
Views: 2866
Reputation: 1994
You can do it by using gsutil which you can install on your local machine or use directly from cloud shell.
gsutil uses command much similar to the linux CLI, you can use the gsutil mv command to achieve this, but since you can't use wildcards there you have to use something similar to this:
IFS=$'\n'
gsutil ls gs://your-bucket/*.ipynb| while read x; do gsutil mv $x $(echo $x | sed "s/.ipynb/.py/g"); done
I'm not a shell expert so probably this can be improved, but here's an explanation:
This is like "rewriting" the files entirely since gcs objects are immutable, so there are probably a few considerations that you should keep in mind, although this might not be your case:
hope this helps :)
Upvotes: 6
Reputation: 776
As mentioned in the documentation, you can rename your files in your GCS buckets using Console, GSutil command, Code or REST APIS.
The Gsutil command you should use is the following:
gsutil mv gs://[BUCKET_NAME]/[OLD_OBJECT_NAME] gs://[BUCKET_NAME]/[NEW_OBJECT_NAME]
Furthermore, in case you want to change more than one file, I would suggest you to use a script in order to do it for each file you need to change.
Upvotes: 0