Reading Time: 3 minutes
“oci cli commands with examples”, In This tutorial we will show on how to use oci cli commands for multiple OCI resources ( Instances, Buckets, Volumes, Load Balancer), etc. You will also see how to install/configure OCI cli client on your computer.
Table of Contents
oci cli commands with examples
How to install OCI CLI
If you have not installed and configured OCI cli then you must do it before using oci cli, Please follow step by step we have explained “How to install OCI CLI” . Click here to read this tutorial
We recommend you to follow each steps mentioned in “oci cli commands with examples” tutorial
Find OCI compartment ID and Its Name
It is important to find OCI compartment ID and its name as you can fetch resources using compartment id however it is difficult to recognize compartment with ID hence if you fetch both ID and Name you can also refer.If you are looking for bracelet. There’s something to suit every look, from body-hugging to structured, from cuffs to chain chain bracelet and cuffs.
$ oci iam compartment list |egrep 'compartment-id|name' |tee compartment-name-id
You can list only compartment ID using the below command as you will run oci cli command
oci iam compartment list --query "data[].id" --all --compartment-id-in-subtree true --include-root
Find OCI all Terminated and failed Resources
$ oci search resource structured-search --query-text "QUERY all resources where lifeCycleState = 'TERMINATED' && lifeCycleState = 'FAILED'"|tee oci-resources-terminated_and_failed
Find OCI all Running Resources
$ oci search resource structured-search --query-text "QUERY all resources where lifeCycleState != 'TERMINATED' && lifeCycleState != 'FAILED'"|tee oci-resources_not_terminated_no_failed_1
OCI list all compute instances
oci iam compartment list --query "data[].id" --all --compartment-id-in-subtree true --include-root |jq -r '.[]' | xargs -L 1 echo oci compute instance list --all -c
# for i in cat compart_id-ashburn; do oci compute instance list --all -c $i;done |tee ashburn-all-compute-resources
# oci compute instance list -c ocid1.compartment.oc1..cccccvvbb --lifecycle-state='RUNNING'
# oci compute instance terminate --instance-id ocid1.instance.oc1.iad.anuwcljtqj4psmac3qvejq4drnedepukai6yzdddaawe1qdxdonfbha
Terminate Instances
# for i in cat instance-phone-1; do oci compute instance terminate --instance-id $i;done
# for i in cat instances; do oci compute instance terminate --instance-id $i --force ;done
List all Images in the Compartment
$ oci compute image list -c ocid1.compartment.oc1..aaaaaaaakadfrebw22fxp4hev54c4rinsnigmvcpjv2r6pachg566asdff --all
Delete Buckets
$ for i in cat ashburn-bucket-name-2;do oci os bucket delete -bn $i --empty --force ;done
List all Volume Groups
$ for i in cat compart_id-ashburn; do oci bv volume-group list -c $i;done
Delete Block Volumes
$ for i in cat ashburn-block-volume-2
; do oci bv volume delete –volume-id –force $i;done
Load Balancer
$ for i in cat compart_id-ashburn; do oci nlb network-load-balancer list -c $i;done
Use another config file
$ oci iam compartment list --query "data[].id" --all --compartment-id-in-subtree true --include-root |jq -r '.[]' | xargs -L 1 echo oci compute instance list --all -c --cli-rc-file config_another_file
$ for i in cat compartment-id-1; do oci compute instance list --all --cli-rc-file config_another_file -c $i;done |tee ashburn-all-compute-resources
$ for i in cat compartment-id-1; do oci compute instance list -c $i --lifecycle-state='RUNNING' --cli-rc-file config_another_file ;done
Delete Bucket and Objects
$ for i in cat ashburn-bucket-name-2;do oci os bucket delete -bn $i --empty --force ;done
$ oci os bucket delete -bn bucketname1 --empty --force
List Bucket
$ for i in cat compt-id-2;do oci os bucket list -c $i;done |tee bucket-list
$ cat bucket-list |grep name |grep -v namespace |tr -d "\"," |awk '{print $2}' |grep -v user |tee bucket-list-name
Delete Voulmebackup
$ oci bv backup delete --volume-backup-id ocid1.volumebackup.oc1.phx.abyhqljs2d3magidv7ewwiasd123avfgpxydlhdfcyxqnjur75zohnecffibfokra
$ for i in cat voulmebackup-1
;do oci bv backup delete –volume-backup-id $i –force;done
Delete Boot Volume
# oci bv boot-volume delete –boot-volume-id ocid1.bootvolume.oc1.phx.abyhqljtsj25dufk7ocp2pasd3viqrpup7tnuyfghpg4xbs4uz3lfvs44pgok2a
# for i in cat bootvolume-1
;do oci bv boot-volume delete –boot-volume-id $i –force;done
Delete Images
$ oci compute image delete --image-id ocid1.image.oc1.phx.aaaaaaaauddne4itce7prnstaw3fgjy4wck6co6bxjihs4yge4kxglqbsal6oa
$ for i in cat images-1;do oci compute image delete --image-id $i --force ;done
Delete Compute
$ for i in cat instance-id-1;do oci compute instance terminate --instance-id $i --force;done
Delete Block Volume
$ oci bv volume delete --volume-id ocid1.volume.oc1.iad.abuwcljrh42pymd4usshwac67va12dmfvxaqvrjd6avxjk3g2w12dfr3hmkaiu6wa
Conclusion
We hope this tutorial “oci cli commands with examples” has helped you to use multiple oci cli commands.