gcloud CLI Examples
Audience: Cloud Engineers and Developers.
Purpose: Reference commands for common GCP operations using the gcloud CLI.
Overview
The gcloud CLI is the primary command-line interface for Google Cloud Platform. This guide provides examples for common administrative tasks.
Documentation Links
| Resource | Description |
|---|---|
| gcloud CLI Overview | Official documentation |
| gcloud Reference | Complete command reference |
| Scripting Guide | Automation best practices |
Project Queries
List Projects by Billing Account
Return information about all projects linked to the TAMUNI-i2-Primary-Burwood billing account:
# Billing Account ID: 0152CF-9A23B4-75E0E7
gcloud beta billing projects list \
--billing-account="0152CF-9A23B4-75E0E7" \
--format="value(projectId)" | \
while read -r line; do
gcloud projects describe $line --format=json(projectId,projectNumber,labels)
done
Query Project Tags
Return all tags associated with projects linked to the billing account:
gcloud beta billing projects list \
--billing-account="0152CF-9A23B4-75E0E7" \
--format="value(projectId)" | \
while read -r line; do
gcloud asset search-all-resources \
--scope=projects/$line \
--asset-types=cloudresourcemanager.googleapis.com/TagKey \
--format=json
done
Output Formatting
Common format options for gcloud commands:
| Format | Description | Example |
|---|---|---|
json | Full JSON output | --format=json |
yaml | YAML output | --format=yaml |
table | Tabular output | --format=table |
value | Plain values (for scripting) | --format="value(field)" |
csv | CSV output | --format=csv |
Selective Field Output
# Return only specific fields
gcloud projects describe PROJECT_ID \
--format="json(projectId,projectNumber,labels)"
# Get just the project ID
gcloud projects list --format="value(projectId)"
Tips for Scripting
Avoid Interactive Prompts
Use --quiet or -q to suppress interactive prompts in scripts:
gcloud projects delete PROJECT_ID --quiet
Output Parsing
Use jq to parse JSON output:
gcloud projects list --format=json | jq '.[].projectId'