Terminal Commands: A Practical Reference
This reference keeps common terminal commands in a toolbox organized by job, so you can open the right drawer instead of searching an alphabetical list.
Use The Terminal: A Practical Guide to learn the underlying shell concepts, then use Using AI Coding Agents in the Terminal to apply those commands while supervising an agent. Each entry gives the command's job, a minimal example, and the expected result.
Navigation
Start in the navigation drawer whenever a command depends on where you are. Relative paths begin in the working directory, so confirm that location before changing files.
An absolute path starts at the filesystem root, a relative path starts at the working directory, and ~ represents your home directory. The special path . means the current directory, while .. means its parent.
pwd: Print the Working Directory
pwd prints the full path of the directory the shell is currently using.
pwd
# /Users/alex/projects/weather-app ls: List Directory Contents
ls lists visible files and directories in the current location, while ls -la includes hidden entries and details.
ls
# README.md src tests cd: Change the Working Directory
cd enters another directory; cd .. moves to its parent, and cd without a path returns home.
cd src
pwd
# /Users/alex/projects/weather-app/src Files & Directories
File commands normally accept a source path, a destination path, or both. Run pwd and ls first when the target is unclear.
cp and mv can replace an existing destination, depending on the command and options. On some systems, adding -i makes the command ask before overwriting. Read man cp or man mv before moving a directory tree; man is explained in the Environment & Help drawer below.
mkdir: Create a Directory
mkdir creates a directory; -p also creates missing parent directories and does not fail when the path already exists.
mkdir -p reports/daily
ls reports
# daily cp: Copy a File
cp copies the source to the destination and leaves the original in place.
cp report.txt report-backup.txt
ls report*
# report-backup.txt report.txt mv: Move or Rename a File
mv moves a file to another directory or gives it a new name.
mv draft.txt final.txt
ls
# final.txt rm: Remove a File
rm removes a named file without sending it to the desktop Trash. Successful removal is silent, so list the directory to confirm the result.
rm old-report.txt
ls old-report.txt
# ls: old-report.txt: No such file or directory Removing a directory and its contents requires recursive behavior such as rm -r. That option expands the amount of data affected, so verify the complete path and avoid adding force options merely to silence an error.
touch: Create or Update a File
touch creates an empty file when it does not exist, or updates an existing file's modification time.
touch notes.txt
ls notes.txt
# notes.txt Reading & Searching Text
These commands inspect text without opening an editor. Choose one based on file length and whether you need every line, one end, or matching lines.
head and tail print ten lines by default. Set an explicit count when a script or explanation depends on a fixed result, and use less when the useful part lies between the two ends.
cat: Print a Short File
cat writes an entire file to standard output, which makes it useful for short files.
cat status.txt
# deployment ready less: Read a Long File
less opens a scrollable viewer for long files. Use the arrow keys or Page Up and Page Down, then press q to return to the prompt.
less server.log
# Opens server.log in the less viewer; press q to exit Inside less, type / followed by text and press Return to search forward. After a match appears, press n to move to the next one. The file is only being viewed, so leaving less does not change it.
head: Read the First Lines
head prints the beginning of a file; -n 2 requests exactly two lines.
head -n 2 servers.txt
# web-01
# web-02 tail: Read the Last Lines
tail prints the end of a file. Add -f to keep watching as a log grows, then press Ctrl+C to stop following it.
tail -n 2 deployment.log
# INFO health check passed
# INFO deployment complete grep: Find Matching Lines
grep prints lines containing a pattern; -i makes a text match case-insensitive.
grep "ERROR" deployment.log
# ERROR disk full
# ERROR backup failed Pipes & Redirection
Pipes and redirection control where output goes. They are the connectors in the toolbox: a pipe hands output to another command, while redirection sends it to a file.
Shell programs normally begin with three streams: standard input, standard output, and standard error. Their numeric descriptors are 0, 1, and 2. Ordinary > redirects standard output, while 2> selects the error stream.
|: Send Output to Another Command
The pipe operator sends standard output from the command on its left into standard input for the command on its right.
cat deployment.log | grep "ERROR"
# ERROR disk full
# ERROR backup failed >: Write Output to a File
> writes standard output to a file, creating it or replacing all existing contents.
echo "ready" > status.txt
cat status.txt
# ready >>: Append Output to a File
>> appends standard output to the end of a file without deleting the text already there.
echo "checked" >> status.txt
cat status.txt
# ready
# checked 2>: Redirect Error Output
2> sends standard error to a file while leaving ordinary standard output unchanged. The exact error wording shown by this example varies between operating systems.
ls missing-folder 2> errors.txt
cat errors.txt
# ls: missing-folder: No such file or directory Processes
This drawer is optional for beginners. The everyday takeaways are that ps lists what is running, kill stops a background command, and Ctrl+C stops the one in front of you.
Each running program is represented by at least one operating-system process. These commands help you see shell activity, stop a foreground task, or send a termination request to a process you started.
A process ID identifies an operating-system process. A job number such as %1 identifies work tracked by the current shell. Job numbers disappear with that shell session, while process IDs are visible to process tools across terminals when permissions allow.
ps: List Processes
ps reports a snapshot of the running processes visible to the current user. In this command, $$ is the current shell's ID, and -o pid=,comm= limits the output to the process ID and command-name columns. The number will differ, macOS may print the shell as a full path such as /bin/zsh, and the name column's format varies between systems.
ps -o pid=,comm= -p $$
# 48312 zsh kill: Send a Signal to a Process
kill sends a signal to a process. With no signal option, it requests normal termination rather than forcing an immediate stop. In this example, & runs sleep in the background, and $! holds the last background command's process ID.
sleep 60 &
process_id=$!
kill "$process_id"
# kill prints nothing itself kill prints nothing itself; the shell may report the job as terminated at the next prompt. Give a process time to shut down cleanly before considering a stronger signal. A forced signal can prevent cleanup work, so it is a diagnostic last step rather than the default form of kill.
Ctrl+C: Interrupt the Foreground Process
Ctrl+C usually sends an interrupt signal to the command currently controlling the terminal and returns you to the prompt.
sleep 60
# Press Ctrl+C
# ^C jobs: List Shell Jobs
jobs lists tasks started from the current shell with job numbers and states. The job number can be used as a target such as %1.
sleep 60 &
jobs
# [1]+ Running sleep 60 &
kill %1 The bracketed job number and status format differs slightly between bash and zsh.
Environment & Help
Environment variables store values used by the shell and launched programs. Help commands explain how the local version of a command interprets its options.
PATH is a colon-separated list of directories searched for executable commands. Use lookup commands to inspect the current result before changing PATH. The local manual remains the best match for the exact command version installed on the computer.
echo $VAR: Print an Environment Variable
Use echo "$VAR" to print the value stored in a selected environment variable. Keep the reference in quotes so spaces remain part of one value.
echo "$HOME"
# /Users/alex which: Locate a Command
command -v is the portable POSIX way to determine how a command name resolves. In zsh, which is a built-in that also reports shell built-ins and aliases, while Bash usually resolves which to an external binary.
which grep
# /usr/bin/grep command -v: Show How a Command Resolves
command -v is a shell-oriented lookup that can identify executables and shell built-ins.
command -v cd
# cd man: Open a Manual Page
man opens the installed manual for a command. Read its synopsis and options, then press q to exit.
man grep
# Opens the local grep manual page; press q to exit --help: Request Short Help
Many commands accept --help after the command name and print a short usage summary. Some macOS tools omit this GNU-style option, so use man when it is rejected.
grep --help Common Pitfalls & Debugging
This section is a quick diagnostic index. The terminal practical guide explains paths, shell behavior, and safe command habits in more depth.
Quick Diagnostic: A Removed File Is Missing
Symptom: a file removed with rm is absent from the desktop Trash. Cause: rm removes the directory entry directly. Fix: check pwd, list the exact target, and keep backups or version control before removal.
For the deeper safety explanation, see Removed Files Bypass the Trash in the terminal practical guide.
Quick Diagnostic: A Spaced Path Splits
Symptom: a command reports extra arguments or missing paths. Cause: the shell splits an unquoted path at each space. Fix: wrap the complete path in straight quotes, as in cd "$HOME/project files".
For more examples, see Spaces in Paths Need Quoting in the terminal practical guide.
Quick Diagnostic: A Command Will Not Resolve
Symptom: the shell rejects a requested name with command not found. Cause: the name is misspelled, the program is absent, or its directory is outside PATH. Fix: check the spelling and run command -v name before editing shell configuration.
For the deeper lookup explanation, see Command Not Found Points to PATH in the terminal practical guide.
Quick Diagnostic: Filename Case Mismatch
Symptom: a path works on one computer but fails on another. Cause: filename case rules differ across filesystems. Fix: match every letter's case exactly so commands and scripts behave consistently on macOS and Linux.
For the cross-system explanation, see Filename Case Can Change Between Systems in the terminal practical guide.
Frequently Asked Questions
What is the difference between > and >>?
The > operator creates or replaces the destination file with new standard output. The >> operator appends new output to the end of the destination while keeping its existing contents. Check the target before using either operator.
How can you check what a command will do before running it?
Open its manual page with man command-name or request a short summary with command-name --help when the program supports that option. Read the synopsis, options, and target paths before running a command that changes or removes files.
Do these commands behave differently in bash and zsh?
Most examples behave the same in bash and zsh, but built-ins, startup files, job-status messages, and command lookup can differ. When output or behavior changes, check the shell you are running and use its local help or manual.
Can you use these commands on Windows?
These examples target Unix-like shells rather than Command Prompt or PowerShell. Windows Subsystem for Linux provides a Linux environment where the same Bash commands and path conventions work. Native PowerShell uses different commands and syntax for several tasks.
Does grep support regular expressions, or only plain text?
Both. Plain text matching is the default, -E enables extended regular expressions for pattern matching, and -F forces the pattern to be treated as fixed text even if it contains characters that would otherwise mean something in a regex.
Can more than two commands be chained together with pipes?
Yes. Any number of commands can be chained with additional pipe operators, and each command's output feeds directly into the next command's input in sequence, from left to right.
What does the bracketed number before a job's status in jobs output mean?
It is the job's ID within that shell session, referenced as %1, %2, and so on with kill or fg. A plus sign marks the current job, the one a bare fg or bg command acts on without an argument, and a minus sign marks the previous job.
Does closing the terminal window stop a background job started with an ampersand?
Usually yes. Closing the terminal normally ends jobs tied to that session unless the process was started with a tool such as nohup or disown, or is running inside a persistent session like tmux or screen.
Related Terminal and DevOps Guides
- Learn the shell concepts behind this reference in The Terminal: A Practical Guide.
- Apply these commands while reviewing approvals and changes in Using AI Coding Agents in the Terminal.
- Return to the DevOps and server administration hub for server operations, observability, and recovery guides.
Sources
-
[1]
GNU Coreutils Manual(gnu.org)
-
[2]
Bash Reference Manual(gnu.org)
-
[3]
Linux man-pages(man7.org)
-
[4]
Terminal User Guide for Mac(support.apple.com)
Read Next
Learn how terminals and shells work, navigate files, use core Unix commands, connect commands with pipes, and supervise terminal-based coding agents.
Learn how to start terminal AI coding agents in the right project, supervise commands, review file changes, and set safe working boundaries.
Choose a DevOps guide for terminal basics, AI coding agents, server observability, contingency planning, or Squid proxy configuration.