---
# System prepended metadata

title: ' OCP count containers'

---

---
date-created: 20240222
tags:
- ocp
- bash/script
---

# OCP count containers

```bash=
#!/bin/bash
# Initialize total container count
total=0
NS_PATTERN="- r 'openshit-*'"
# Loop through all namespaces starting with "fibi"
for ns in $(oc get namespaces --no-headers | grep "$NS_PATTERN" | awk '{print $1}'); do
  # For each namespace, get the count of containers in all pods
  count=$(oc get pods -n "$ns" -o json | jq '.items | .[] | .spec.containers | length' | awk '{s+=$1} END {print s}')
  # Add the count from this namespace to the total
  total=$((total + count))
done
# Print the total count of containers
echo "Total containers across all namespaces starting with 'fibi': $total"
```