zoey3
zoey3

Reputation: 171

GCP rename files with same name as existing directory

I'm trying to rename my file toto.html by removing its extension using gsutils. My folder/files are organized this way:

toto     ----|
             |
             |--file1.html
             |
             |--file2.txt
             |
             |-- ...
             |
toto.html

So I used the command :

gsutil mv gs://my_bucket/toto.html  gs://my_bucket/toto

the problem is by doing so, the file is moved inside the folder that has the same name as the directory. ( which is normal )

How can I rename my file without having this issue?

thanks in advance.

Upvotes: 0

Views: 1080

Answers (1)

Emmanuel
Emmanuel

Reputation: 1494

Let me explain briefly about how directories work on Google Cloud Storage, as detailed in the documentation:

Cloud Storage operates with a flat namespace, which means that folders don't actually exist within Cloud Storage. If you create an object named folder1/file.txt in the bucket your-bucket, the path to the object is your-bucket/folder1/file.txt, but there is no folder named folder1; instead, the string folder1 is part of the object's name.

So when you perform the move command gsutil mv gs://my_bucket/toto.html gs://my_bucket/toto, gsutil interprets it like you would like to move the file inside the folder named toto as there is no way to differentiate between the file and the folder.

What you could do to achieve the task is move the file inside a folder first to change its name and then get it back to root:

gsutil mv gs://my_bucket/toto.html  gs://my_bucket/toto/toto
gsutil mv gs://my_bucket/toto/toto  gs://my_bucket/

Upvotes: 2

Related Questions