0
0
GCPcloud~5 mins

Storage commands (gsutil) in GCP - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you need to save files or share data in the cloud. Google Cloud Storage lets you do this easily. The gsutil tool helps you move files to and from this cloud storage using simple commands.
When you want to upload photos or documents from your computer to the cloud for backup.
When you need to download files from cloud storage to your local machine to work on them.
When you want to list all files stored in a specific cloud storage bucket to check what is saved.
When you want to copy files between two cloud storage buckets without downloading them first.
When you want to delete old files from cloud storage to free up space.
Commands
This command creates a new storage bucket named 'example-bucket-storage' where you can store your files.
Terminal
gsutil mb gs://example-bucket-storage
Expected OutputExpected
Creating gs://example-bucket-storage/...
This command uploads the file 'sample-file.txt' from your computer to the bucket you just created.
Terminal
gsutil cp sample-file.txt gs://example-bucket-storage/
Expected OutputExpected
Copying file://sample-file.txt [Content-Type=text/plain]... / [1 files][ 12.0 B/ 12.0 B] 100% Done Operation completed over 1 objects.
This command lists all files currently stored in the 'example-bucket-storage' bucket so you can see what is saved.
Terminal
gsutil ls gs://example-bucket-storage/
Expected OutputExpected
gs://example-bucket-storage/sample-file.txt
This command downloads the file 'sample-file.txt' from the cloud bucket to your local machine and saves it as 'downloaded-file.txt'.
Terminal
gsutil cp gs://example-bucket-storage/sample-file.txt downloaded-file.txt
Expected OutputExpected
Copying gs://example-bucket-storage/sample-file.txt [Content-Type=text/plain]... / [1 files][ 12.0 B/ 12.0 B] 100% Done Operation completed over 1 objects.
This command deletes the file 'sample-file.txt' from the cloud bucket to free up space or remove unwanted files.
Terminal
gsutil rm gs://example-bucket-storage/sample-file.txt
Expected OutputExpected
Removing gs://example-bucket-storage/sample-file.txt...
Key Concept

If you remember nothing else from this pattern, remember: gsutil lets you easily move files to, from, and inside Google Cloud Storage using simple commands.

Common Mistakes
Trying to upload a file to a bucket that does not exist.
The command fails because the storage location is missing and gsutil cannot create files without a bucket.
Always create the bucket first using 'gsutil mb' before uploading files.
Using incorrect bucket names with typos or missing 'gs://' prefix.
Commands fail because gsutil cannot find the bucket or interprets the name as a local path.
Use the full bucket path starting with 'gs://' and double-check spelling.
Deleting files without confirming the file name or path.
You might remove important files accidentally, causing data loss.
List files first with 'gsutil ls' and confirm the exact file name before deleting.
Summary
Create a storage bucket with 'gsutil mb' to hold your files.
Upload files to the bucket using 'gsutil cp'.
List files in the bucket with 'gsutil ls' to see what is stored.
Download files from the bucket back to your computer using 'gsutil cp'.
Remove files from the bucket with 'gsutil rm' when no longer needed.