Ollama CLI Basics - Command-Line Mastery
Status: Active
Last Updated: 2026-08-14
Category: AImL - Phase 1: LLM Fundamentals
Prerequisites: ollama-setup
Time: 1-2 hours
Resources: [███░░░░░░░] 30%
Tags: ollama, cli, commands, prompts, parameters, system-prompts, scripting
Summary
Everything the ollama command line can do beyond ollama run: managing models from the shell, piping prompts through stdin for one-shot automation, tuning model parameters per-session and per-model-file, and setting persistent system prompts via custom Modelfiles.
🎯 What You'll Learn
- ✅ Use every core subcommand (
run,pull,list,ps,rm,cp,show,stop) - ✅ Pipe prompts and files into models from stdin for scripted workflows
- ✅ Set sampling parameters interactively and persistently
- ✅ Create custom model variants with system prompts using a Modelfile
- ✅ Build small shell automations around local inference
1. The Core Commands
ollama run <model> [prompt] # interactive chat, or one-shot with prompt arg
ollama pull <model> # download/update without running
ollama list # downloaded models
ollama ps # models loaded in memory right now
ollama rm <model> # delete
ollama cp <src> <dst> # duplicate/tag
ollama show <model> # metadata: params, template, license, capabilities
ollama stop <model> # unload from memory immediately
ollama serve # start server (normally managed by systemd)
Inspecting a Model Deeply
ollama show llama3.1:8b
Model
architecture llama
parameters 8.0B
context length 131072
embedding length 4096
quantization Q4_K_M
Capabilities
completion, tools
What Happens: This tells you the quantization level you're actually running, the max context window, and whether the model supports tool calling — all things that matter later in context-and-tokens and agent-frameworks. Check this before assuming any capability.
2. Interactive Session Essentials
$ ollama run llama3.1:8b
>>> /?
Available Commands:
/set Set session variables
/show Show model information
/load <model> Load a model or session
/save <model> Save your current session
/clear Clear session context
/bye Exit
The /set family:
>>> /set parameter temperature 0.2 # deterministic-ish answers
>>> /set parameter num_ctx 8192 # raise context window for this session
>>> /set system "You are a senior Linux sysadmin. Answer tersely with commands."
>>> /show system # verify what's set
>>> /clear # wipes conversation but KEEPS /set values? No:
# /clear resets context; use /load to fully reset
Gotcha:
/set parameterapplies only to the current session. For persistence, create a custom model (§5).
3. One-Shot Prompts and Stdin Pipes
This is where the CLI becomes a Unix citizen.
# Argument form
ollama run llama3.1:8b "In one sentence: why does DNS propagation take time?"
# Heredoc for multi-line prompts
ollama run llama3.1:8b <<'EOF'
Rewrite these release notes in plain English:
- Refactored ingestion worker for O(1) dedup lookups
- Bumped gRPC deadline to 30s
EOF
# Pipe a file in — the killer feature
cat error.log | ollama run llama3.1:8b "Explain the root cause of this error log."
git diff | ollama run qwen2.5-coder:7b "Review this diff for bugs. Be brief."
curl -s https://example.com/status.html | ollama run llama3.2:3b "Is maintenance scheduled? Yes/no + quote."
What Happens: stdin content is appended as context; your argument is the instruction. The model never sees filenames — it sees exactly the bytes you pipe. Large pipes silently truncate at the context limit (context-and-tokens), so keep piped input focused.
Practical Scripting Patterns
# Batch-classify lines from a file
while IFS= read -r line; do
label=$(ollama run llama3.2:3b "Classify as bug|feature|question. Reply with ONE word: $line")
echo "$label,$line" >> triage.csv
done < tickets.txt
# Commit message helper
git commit -m "$(git diff --cached | ollama run qwen2.5-coder:7b \
'Write a conventional-commit message (max 72 chars subject) for this diff.')"
Tip for batch jobs: use a small model (3B), set
temperature 0, and add"stream": falsethinking if you move to the API (ollama-api). Each CLI invocation pays a cold-load cost if the model isn't resident — increaseOLLAMA_KEEP_ALIVEwhen looping.
4. Model Parameters You Should Know
Set interactively (/set parameter) or in Modelfiles:
| Parameter | Default | Effect |
|---|---|---|
temperature |
0.8 | Randomness of sampling. 0–0.3 = factual/extraction; 0.7+ = creative |
top_p |
0.9 | Nucleus sampling cutoff |
top_k |
40 | Candidate token pool size |
repeat_penalty |
1.1 | Penalizes token repetition |
num_ctx |
model default | Context window tokens (RAM grows with it!) |
num_predict |
-1 | Max tokens generated (-1 = unlimited) |
num_gpu |
auto | Layers offloaded to GPU |
num_thread |
auto | CPU threads |
Sanity-check a generation speed change live:
>>> /set verbose # prints timing stats after each reply
>>> Explain RAID 10 vs RAID 6.
# total duration: ... load: ... prompt eval count: ...
# eval count: 214 tokens / 5.2 seconds → ~41 tokens/second
5. Custom Models via Modelfile
The equivalent of a Dockerfile: bake your system prompt and defaults into a named model.
# File: Modelfile.sysadmin
FROM llama3.1:8b
PARAMETER temperature 0.3
PARAMETER num_ctx 8192
SYSTEM """
You are FogServ's on-call assistant.
Rules:
- Prefer exact commands over prose.
- Always warn before destructive operations.
- If unsure about a hostname, say so instead of guessing.
"""
Build and use it:
ollama create sysadmin-assistant -f Modelfile.sysadmin
ollama run sysadmin-assistant
>>> check disk usage on web01
What Happens: create doesn't copy weights — it stores a new manifest pointing at the same blobs plus your parameters (like a Docker image tag over shared layers). Costs kilobytes, not gigabytes.
Version them like code:
ollama cp sysadmin-assistant sysadmin-assistant:v1 # snapshot before tweaking
ollama list
Prompt versioning matters more than people think — changing a system prompt can silently break downstream behavior (a top pitfall flagged in the course README).
6. Quick Reference Card
# Lifecycle
ollama pull llama3.1:8b && ollama list && ollama ps && ollama rm oldmodel
# One-shots
ollama run m "prompt" # direct
cat f.txt | ollama run m "instruction" # stdin context
# Tuning
/set parameter temperature 0 # session
ollama create mymodel -f Modelfile # persistent
# Diagnostics
ollama show m # capabilities/quant
/set verbose # timing per response
journalctl -u ollama -f # server-side logs
🛠️ Troubleshooting & Common Issues
| Symptom | Cause | Fix |
|---|---|---|
| Piped file seems ignored at the end | Exceeded context window; tail truncated | Check size vs num_ctx (ollama show); chunk input or raise num_ctx |
| Every answer starts identically/repetitive | Low temperature + repeat_penalty too low | Raise temperature slightly or repeat_penalty to ~1.15 |
/set parameter lost next session |
Session-scoped by design | Bake into a Modelfile (ollama create) |
| Slow loop script | Cold-load per invocation | Keep-alive longer (OLLAMA_KEEP_ALIVE=1h) or switch to API with streaming off |
| Custom model behaves exactly like base | SYSTEM block malformed (quotes/newlines) | Rebuild; verify with ollama show mymodel → SYSTEM section |
Error: model requires more system memory |
num_ctx raised too far for RAM | Lower num_ctx; see context-and-tokens |
🔗 Related
- Prev: model-selection-guide
- Next Phase: ollama-api — talk to Ollama over HTTP instead of the CLI
- Also: python-llm-integration, prompt-engineering
- Shell foundations: basics/bash-scripting
📝 Change Log
- 2026-08-14 — Initial publication. Covers README outline: ollama run, list/pull/rm, stdin prompts, model parameters, system prompts.