Linux Bash Commands Cheat Sheet: Shell Keys and Everyday Tasks
A Bash session mixes two kinds of shortcut. Keys such as Ctrl + R and Alt + B belong to Readline, the library Bash uses to read the command line, so they work in any terminal and over SSH; ls, grep or ps are separate programs with their own manuals. This guide is ordered by task, with the pitfalls each reference page documents. Meta, written M- in the manual, is the key labelled Alt or Option; where it does nothing, press Esc and then the letter.
Table of Contents
Repair a long command without retyping it
The keys on the Bash reference page follow Emacs conventions; the cutting keys save text in a kill ring you can paste from.
| Shortcut | What it does |
|---|---|
| Ctrl + A | Go to the start of the line |
| Ctrl + E | Go to the end of the line |
| Alt + B | Back one word |
| Alt + F | Forward one word |
| Ctrl + U | Cut everything before the cursor |
| Ctrl + K | Cut from the cursor to the end of the line |
| Ctrl + W | Cut back to the previous white space |
| Alt + Backspace | Cut the word before the cursor |
| Ctrl + Y | Paste the most recently cut text |
| Ctrl + _ | Undo the last edit |
| Ctrl + X, Ctrl + E | Edit the line in your text editor |
The two word-cutting keys disagree about what a word is. At the end of a path such as /var/log/nginx, Alt + Backspace removes only nginx, because for it only letters and digits form words, while Ctrl + W stops only at white space and removes the whole path.
The kill ring lets you park a half-written command: from the end of the line, Ctrl + U cuts it, you run the one you forgot, and Ctrl + Y brings it back. Ctrl + X, Ctrl + E opens a long line in $VISUAL or $EDITOR; whatever you save is run. Alt + # turns the line into a comment and accepts it, keeping it in history without running it.
Find and reuse earlier commands
Ctrl + R searches history backward as you type, and pressing it again moves to older matches. Enter runs the match; Ctrl + G cancels and restores your line. Ctrl + S searches forward, but many terminals take it for flow control and freeze output; the Bash page explains the setting. With the shell integration of fzf loaded, Ctrl + R opens fzf's fuzzy history search, which puts the chosen command on the prompt line rather than running it.
Alt + . inserts the previous command's last argument; each further press steps one command back. !$ stands for the same word but is expanded only when the line runs. !! repeats the whole previous command.
^STRING1^STRING2^ reruns the previous command with STRING1 changed to STRING2; it must start the line and changes only the first match, whereas !!:gs/OLD/NEW/ changes all of them. shopt -s histverify loads each expanded line back for checking before it runs.
Pause, background and stop running jobs
Ctrl + C interrupts the foreground command, usually with SIGINT. Ctrl + Z stops it instead and returns the prompt, discarding pending output and typeahead. Then bg lets the job continue in the background, as if started with COMMAND &, and fg brings it back. In GNOME Terminal, Ctrl + C does not copy; copying takes Ctrl + Shift + C (see the Linux desktop page).
jobs -l lists jobs with their process IDs; + marks the current job, the one a bare fg or bg acts on. fg %2 resumes job 2. kill %1 sends SIGTERM, the default; kill -s SIGSPEC ID sends another signal, and kill -l lists their names.
When Bash receives a hangup, it passes SIGHUP on to its jobs. disown -h marks the current job so it is spared, while plain disown removes it from the job table.
Move between directories and copy files
cd - returns to the previous directory. For more places, pushd DIR changes directory and keeps the old one on a stack, popd goes back, and dirs -v lists the stack.
Tab fills in a word, and Alt + ? lists the candidates. Before deleting with a wildcard, press Ctrl + X, G after the pattern to list the file names it matches, or Ctrl + X, * to put that list on the line, where you can still edit it.
When copying with rsync, a trailing slash on the source changes the result. rsync -av /src/foo /dest creates /dest/foo, while rsync -avz foo:src/bar/ /data/tmp puts the contents of bar straight into /data/tmp. Add -n for a dry run, with -v or -i to see what would change, and do that before any --delete, which removes destination files missing from the source and needs a whole directory as source, not dir/*.
Search and change text
The sed manual gives sed -n '/hello/p' as the equivalent of a grep for hello: -n turns off automatic printing, so only matching lines are output. awk matches with awk '/li/ { print $0 }' mail-list and also splits lines into fields: awk -F: '{ print $1 }' /etc/passwd | sort lists login names in order. For tab-separated data keep the quotes in -F '\t'; unquoted, the shell strips the backslash and awk gets a plain t.
sed 's/hello/world/g' input.txt > output.txt replaces all occurrences; without the g flag, only the first on each line changes. sed -i 's/hello/world/' file.txt rewrites the file itself, with two documented traps: in sed -iE '...' FILE the E is taken as a backup suffix, leaving a copy named FILEE, and sed -ni 's/foo/bar/' FILE writes nothing back, so the file ends up empty.
Connect commands and redirect output
COMMAND1 | COMMAND2 feeds one command's output to the next, and COMMAND1 |& COMMAND2 sends error messages through too. COMMAND1 && COMMAND2 runs the second only if the first succeeded; $? holds the last exit status. > FILE empties the file before writing, >> FILE appends. Order matters with 2>&1: after > FILE it sends both streams to the file, before it only standard output goes there. &> FILE sends both without that question, and >| FILE overwrites even when the noclobber option is set.
Permissions and ownership
For a downloaded binary, the Linux install of the NGC CLI runs chmod u+x ngc-cli/ngc to make it executable and adds its directory to PATH. After a permission denied error, sudo !! repeats the previous command with privileges.
Copying can change permissions. With -p, rsync keeps the source permissions; without it, new files get the source modes masked by the receiver's umask. -o keeps the owner only when the receiving rsync runs as super-user; otherwise files belong to whoever runs the copy. Both are part of -a. --chmod=D2775,F664 sets one mode for directories and another for files, and --chown=USER:GROUP forces one owner and group.
Watch and stop processes
In htop, F4 filters the list by command line, Shift + P and Shift + M sort by CPU and memory with the keys top uses, and F5 shows the process tree. Tag processes with Space, or a whole family with C, and F9 sends the chosen signal to all tagged ones, or to the highlighted one if none is tagged. F7 raises a priority, which only the superuser may do. Started with --readonly, htop ignores the kill and priority keys.
Outside htop, the nginx documentation uses ps -ax | grep nginx to find the master and worker process IDs. For ports, ss takes over from netstat, whose own manual calls it mostly obsolete: ss -t -a lists TCP sockets including listening ones, -p adds the process behind each and -n keeps ports numeric. The older netstat -tulpn shows process IDs only when run as root.
Unpack archives and keep backups
The HPL build instructions unpack a compressed tarball in two steps, gunzip hpl-2.3.tar.gz; tar -xvf hpl-2.3.tar. tar can also stream: the kubectl reference pipes tar cf - /tmp/foo | kubectl exec -i -n my-namespace my-pod -- tar xf - -C /tmp/bar to copy a directory into a pod when kubectl cp cannot.
rsync's archive mode makes no archive file: -a copies a tree keeping symlinks, permissions, times, group, owner, and device and special files, but hard links need -H, and ACLs and extended attributes need -A and -X. For dated backups, --link-dest=DIR hard-links files unchanged since a previous copy instead of sending them again, and --backup-dir=DIR keeps replaced files; a relative DIR sits inside the destination.
Search shortcuts by platform interactively
Search all shortcutsWhere these details come from
Keys and shell syntax follow the GNU Bash Reference Manual for Bash 5.3; commands follow the manuals cited on each tool's page, such as rsync 3.5.1 and GNU sed 4.9. Bindings can be changed in an inputrc file, so bind -P shows what your shell uses.