Bash Shortcuts & Commands Cheat Sheet

Essential keyboard shortcuts and commands for working faster in the terminal. Bookmark this page for quick reference.

Cursor Navigation

ShortcutDescription
Ctrl + AMove cursor to the beginning of the line
Ctrl + EMove cursor to the end of the line
Ctrl + FMove cursor one character forward
Ctrl + BMove cursor one character backward
Alt + FMove cursor one word forward
Alt + BMove cursor one word backward
Ctrl + XXToggle between current position and start of line

Text Editing

ShortcutDescription
Ctrl + WDelete the word before the cursor
Alt + DDelete the word after the cursor
Ctrl + UDelete from cursor to the beginning of the line
Ctrl + KDelete from cursor to the end of the line
Ctrl + YPaste (yank) the last deleted text
Ctrl + TSwap the last two characters before the cursor
Alt + TSwap the last two words before the cursor
Alt + UUppercase the word from cursor to end of word
Alt + LLowercase the word from cursor to end of word
Alt + CCapitalize the character at cursor and move to end of word
Ctrl + DDelete the character under the cursor (or exit shell if line is empty)
Ctrl + HDelete the character before the cursor (same as Backspace)

History

Shortcut / CommandDescription
Ctrl + RReverse search through command history
Ctrl + GCancel history search and restore original line
Ctrl + PPrevious command in history (same as Up arrow)
Ctrl + NNext command in history (same as Down arrow)
!!Repeat the last command
!$Last argument of the previous command
!*All arguments of the previous command
!abcRun the most recent command starting with "abc"
!abc:pPrint (don't run) the most recent command starting with "abc"
^old^newReplace "old" with "new" in the previous command and run it
historyShow full command history with line numbers
history | grep <term>Search command history for a specific term
Alt + .Insert the last argument of the previous command

Process Control

Shortcut / CommandDescription
Ctrl + CKill the current foreground process (send SIGINT)
Ctrl + ZSuspend the current foreground process (send SIGTSTP)
Ctrl + DSend EOF — close the current shell or input stream
bgResume a suspended process in the background
fgBring a background process to the foreground
fg %nBring job number n to the foreground
jobsList all background and suspended jobs
command &Run a command in the background
kill %nKill job number n
kill -9 <pid>Force kill a process by PID (send SIGKILL)
nohup command &Run a command immune to hangups, even after logout

File Operations

CommandDescription
ls -laList all files including hidden, with details
cp <src> <dest>Copy a file from source to destination
cp -r <dir> <dest>Recursively copy a directory and its contents
mv <src> <dest>Move or rename a file or directory
rm <file>Remove a file
rm -rf <dir>Recursively force-remove a directory (use with caution)
mkdir -p <path>Create a directory and any missing parent directories
find . -name "*.log"Find files by name pattern in the current directory tree
find . -type f -mtime -7Find files modified in the last 7 days
chmod 755 <file>Set file permissions (owner rwx, group rx, others rx)
chown user:group <file>Change file owner and group
ln -s <target> <link>Create a symbolic link

Text Processing

CommandDescription
grep "pattern" <file>Search for a pattern in a file
grep -r "pattern" <dir>Recursively search for a pattern in all files
grep -i "pattern" <file>Case-insensitive search
sed 's/old/new/g' <file>Replace all occurrences of "old" with "new"
sed -i 's/old/new/g' <file>In-place replacement (modifies the file directly)
awk '{print $1}' <file>Print the first column of each line
awk -F: '{print $1}' /etc/passwdPrint first field using colon as delimiter
sort <file>Sort lines alphabetically
sort -n <file>Sort lines numerically
uniqRemove adjacent duplicate lines (use after sort)
wc -l <file>Count lines in a file
head -n 20 <file>Show the first 20 lines of a file
tail -n 20 <file>Show the last 20 lines of a file
tail -f <file>Follow a file in real time (useful for logs)
cut -d',' -f1,3 <file>Extract fields 1 and 3 from a comma-delimited file
tr 'a-z' 'A-Z'Translate lowercase to uppercase

Redirection & Pipes

Operator / CommandDescription
command > fileRedirect stdout to a file (overwrite)
command >> fileRedirect stdout to a file (append)
command < fileUse file as stdin for the command
command 2> fileRedirect stderr to a file
command 2>&1Redirect stderr to stdout
command &> fileRedirect both stdout and stderr to a file
cmd1 | cmd2Pipe stdout of cmd1 to stdin of cmd2
cmd1 |& cmd2Pipe both stdout and stderr of cmd1 to cmd2
command | tee fileWrite output to both stdout and a file
command | tee -a fileAppend output to both stdout and a file
command | xargs cmdPass output lines as arguments to another command
command | xargs -I{} cmd {}Use each input line as a placeholder in the command

Useful One-Liners

CommandDescription
sudo !!Re-run the last command with sudo
cd -Switch to the previous working directory
mkdir -p dir && cd $_Create a directory and cd into it
du -sh *Show disk usage of each item in the current directory
df -hShow disk space usage in human-readable format
watch -n 5 commandRun a command every 5 seconds and display output
alias ll='ls -la'Create a shortcut alias for a command
tar czf archive.tar.gz dir/Create a gzipped tar archive of a directory
tar xzf archive.tar.gzExtract a gzipped tar archive
curl -O <url>Download a file from a URL
diff <(cmd1) <(cmd2)Compare the output of two commands
column -t <file>Format output into aligned columns
yes | commandAutomatically answer "yes" to all prompts
Ctrl + LClear the terminal screen (same as the clear command)