Skip to content

📋 Linux Commands Cheat Sheet

Quick reference for essential Linux commands from The Linux Terminal guide.


📖 Getting Help

CommandDescription
man <cmd>Open manual page for command
man -k <keyword>Search all man pages
apropos <keyword>Same as man -k
whatis <cmd>One-line command description
info <cmd>GNU-style manual (more detailed)
<cmd> --helpQuick syntax reference
help <builtin>Help for shell built-ins
type <cmd>Show command type (alias/builtin/executable)

📁 Navigation & File Operations

CommandDescription
pwdPrint working directory
cd <dir>Change directory
cd ~ or cdGo to home directory
cd -Go to previous directory
cd ..Go up one directory
lsList directory contents
ls -lLong format listing
ls -laInclude hidden files
ls -lhHuman-readable sizes
ls -lShSort by size

File Operations

CommandDescription
cp <src> <dest>Copy file
cp -r <src> <dest>Copy directory recursively
mv <src> <dest>Move or rename
rm <file>Remove file
rm -r <dir>Remove directory recursively
rm -f <file>Force remove (no prompt)
mkdir <dir>Create directory
mkdir -p <path>Create nested directories
touch <file>Create empty file / update timestamp

📄 Viewing & Editing Files

CommandDescription
cat <file>Display entire file
less <file>Page through file
head <file>First 10 lines
head -n 20 <file>First 20 lines
tail <file>Last 10 lines
tail -n 20 <file>Last 20 lines
tail -f <file>Follow file (live updates)
tail -F <file>Follow with retry (log rotation)
wc <file>Line, word, byte count
wc -l <file>Line count only

🔍 Finding Files

Quick Lookup

CommandDescription
which <cmd>Find executable in PATH
which -a <cmd>Find all executables in PATH
whereis <cmd>Find binary, man page, source
locate <pattern>Search filename database (fast)
locate -i <pattern>Case-insensitive
sudo updatedbUpdate locate database
bash
find <path> [options] [expression]
OptionDescription
-name "pattern"Match filename (case-sensitive)
-iname "pattern"Match filename (case-insensitive)
-type fFiles only
-type dDirectories only
-size +100MLarger than 100MB
-size -1kSmaller than 1KB
-mtime -7Modified in last 7 days
-mtime +30Modified over 30 days ago
-user <name>Owned by user
-perm 644Exact permissions
-emptyEmpty files/directories
-maxdepth nLimit search depth
-exec cmd {} \;Execute command on each match
-deleteDelete matching files

Examples:

bash
find /var/log -name "*.log"
find . -type f -size +100M 2>/dev/null
find . -mtime -1
find . -type d -empty -delete
find . -name "*.sh" -exec chmod +x {} \;

🔎 Searching Content (grep)

bash
grep [options] "pattern" <file(s)>
OptionDescription
-iCase-insensitive
-vInvert match (show non-matching)
-nShow line numbers
-cCount matches
-lList filenames with matches
-r / -RRecursive search
-wMatch whole words
-EExtended regex (egrep)
-A nShow n lines after match
-B nShow n lines before match
-C nShow n lines around match
-oShow only matching part
--colorHighlight matches

Examples:

bash
grep "error" logfile.txt
grep -in "warning" /var/log/syslog
grep -r "TODO" ./src/
grep -E "error|warning" log.txt
grep -C 3 "Exception" app.log
ps aux | grep nginx

Regular Expressions

PatternMeaning
.Any single character
*Zero or more of previous
+One or more (use -E)
^Start of line
$End of line
[abc]Any char in brackets
[^abc]Any char NOT in brackets
[a-z]Range of characters

🔀 Piping & Redirection

Piping

bash
command1 | command2 | command3
ExampleDescription
ls -la | lessBrowse long output
cat file | sort | uniqSort and dedupe
ps aux | grep nginxFind processes
history | tail -20Last 20 commands

Output Redirection

OperatorDescription
>Redirect stdout (overwrite)
>>Redirect stdout (append)
2>Redirect stderr
2>>Append stderr
&>Redirect both stdout and stderr
&>>Append both

Input Redirection

OperatorDescription
<Redirect stdin from file
<<Here document (heredoc)
<<<Here string

Special

CommandDescription
command 2>&1Redirect stderr to stdout
command > file 2>&1Both to file
command &> fileBoth to file (modern)
tee fileWrite to stdout AND file
tee -a fileAppend to file

📜 Command History

CommandDescription
historyDisplay command history
history 20Last 20 commands
history -d <n>Delete line n
history -cClear history

Bang Commands

SyntaxDescription
!!Repeat last command
!nExecute command #n
!-nExecute nth previous command
!stringLast command starting with string
!$Last argument of previous command
!*All arguments of previous command
^old^newReplace in last command
sudo !!Run last command with sudo

🔐 User & Privileges

CommandDescription
whoamiCurrent username
idUser ID, group ID, groups
groupsGroups you belong to
sudo <cmd>Execute as root
sudo -iInteractive root shell
sudo su -Switch to root
sudo -kInvalidate sudo cache
sudo -vExtend sudo timeout
sudo -lList your sudo privileges
sudo visudoSafely edit sudoers

🛠️ Text Processing

CommandDescription
sort <file>Sort lines alphabetically
sort -n <file>Sort numerically
sort -r <file>Sort reverse
sort -u <file>Sort and remove duplicates
sort -k2 <file>Sort by 2nd column
uniqRemove adjacent duplicates
uniq -cCount occurrences
uniq -dShow only duplicates
cut -d':' -f1 <file>Extract first field
cut -c1-10 <file>Extract characters 1-10
strings <binary>Extract readable text from binary

xargs — Build Commands

bash
find . -name "*.tmp" | xargs rm
cat urls.txt | xargs -P 4 -I {} curl {}
find . -name "*.txt" -print0 | xargs -0 cat

⌨️ Keyboard Shortcuts

ShortcutAction
Ctrl + ABeginning of line
Ctrl + EEnd of line
Alt + BBack one word
Alt + FForward one word

Editing

ShortcutAction
Ctrl + UDelete before cursor
Ctrl + KDelete after cursor
Ctrl + WDelete word before
Ctrl + YPaste deleted text
Ctrl + _Undo

Control

ShortcutAction
Ctrl + LClear screen
Ctrl + CInterrupt/kill process
Ctrl + ZSuspend process
Ctrl + DExit shell / EOF
Ctrl + RReverse history search

📊 Quick Examples

bash
# Find large files
find / -type f -size +100M 2>/dev/null

# Search recursively for pattern
grep -rn "TODO" ./src/

# Count lines in all Python files
find . -name "*.py" | xargs wc -l

# Last 20 commands containing "git"
history | grep git | tail -20

# Monitor log file live
tail -f /var/log/syslog

# Find and delete old temp files
find /tmp -type f -mtime +7 -delete

📝 For detailed explanations, see The Linux Terminal guide.

Released under the MIT License.