2.6 Command Quick Reference Page
Type: Reference page, no required sequential reading
Scope: Bash/Zsh common interactive operations; examples prioritized for GNU/Linux validation, PowerShell syntax differs
Identity and Command Source
| Target | Command | Note |
|---|---|---|
| Current user | whoami | Output varies with the account |
| Current Shell process | ps -p $$ -o comm= | $SHELL is typically the login Shell |
| Command type and all candidates | type -a name | Can view aliases, functions, and built-in commands |
| First callable command | command -v name | Usually more portable than which |
| Bash built-in help | help cd | Applies only to Bash built-in commands |
Navigation
| Goal | Command |
|---|---|
| Current directory | pwd |
| List entries | ls |
| Include hidden entries and details | ls -la |
| Enter a directory | cd path |
| Parent directory | cd .. |
| Previous directory | cd - |
| Home directory | cd |
~ typically expands to the home directory, and . is the current directory. Quote path variables: cd "$dir".
Files and Directories
| Goal | Command | Note |
|---|---|---|
| Temporary directory | workdir=$(mktemp -d) | Record and clean up after use |
| Create directory | mkdir -p a/b | -p also creates missing parent directories |
| Create empty file/update time | touch file | Existing content is not cleared |
| Write exactly one line | printf '%s\n' text > file | > will overwrite |
| Append a line | printf '%s\n' text >> file | >> appends |
| Copy a file | cp -- src dst | -- ends option parsing |
| Move or rename | mv -- old new | May overwrite existing targets, depending on options |
| Delete a file | rm -- file | Typically does not go to the trash |
| Delete an empty directory | rmdir dir | Refuses if the directory is not empty |
| Recursive copy | cp -R -- src dst | Metadata retention depends on options and platform |
Environment and PATH
| Goal | Command |
|---|---|
| Current shell variable | NAME=value |
| Export to new sub-processes | export NAME |
| Print a variable literally | printf '%s\n' "$NAME" |
| Temporarily add tool directory | PATH="$dir:$PATH"; export PATH |
| Delete variable | unset NAME |
The startup file varies depending on Bash/Zsh, login/interactive mode, and the system. Do not blindly overwrite or append ~/.bashrc, ~/.profile, and ~/.zshrc.
Wildcards and Quoting
| Syntax | Meaning |
|---|---|
*.log | Any .log name |
file-?.txt | ? matches one character |
file-[13].txt | Matches 1 or 3 |
"$path" | Parameters remain a single parameter after expansion |
-- "$path" | Prevent paths starting with - from being treated as options |
The behavior of unmatched wildcards varies depending on the shell and options. Preview the targets before a batch operation. Changing a file extension does not convert its format.
Standard Streams, Pipes, and Redirection
| Goal | Syntax |
|---|---|
| Redirect standard output | command >out.log |
| Append standard output | command >>out.log |
| Save standard error separately | command 2>err.log |
| Merge output and error | command >all.log 2>&1 |
| Connect two programs | `left |
| See failures in any part of a pipeline | set -o pipefail |
Redirection occurs from left to right, and 2>&1 >out.log is not equivalent to >out.log 2>&1. Pipes default to connecting standard output.
One Minute Before Deletion
pwd
ls -ld -- "$target"
find "$target" -printSet $target to the path you intend to inspect before using this read-only snippet. Before deletion, confirm that the variable is nonempty, the path is correct, and the scope is limited. Do not execute dangerous commands to test whether the system will protect you.
When Details Are Required
- GNU Bash: Shell Expansions explains the order of brace expansion, variables, commands, filenames, and process substitution.
- GNU Bash: Redirections provides the complete rules for file descriptors and the left-to-right processing of redirections.
- GNU Findutils: Safe File Name Handling explains why
-print0can preserve special filenames. - Linux
pgrep(1)lists the options implemented by procps; macOS uses its own man pages to check for differences.