Linux Command Line Cheat Sheet
cheatsheet linux bash command
Bash Commands
| Command | Description |
|---|
uname -a | Show system and kernel |
head -n1 /etc/issue | Show distribution |
mount | Show mounted filesystems |
date | Show system date |
uptime | Show uptime |
whoami | Show your username |
man command | Show manual for command |
Bash Shortcuts
| Shortcut | Description |
|---|
CTRL-c | Stop current command |
CTRL-z | Sleep program |
CTRL-a | Go to start of line |
CTRL-e | Go to end of line |
CTRL-u | Cut from start of line |
CTRL-k | Cut to end of line |
CTRL-r | Search history |
!! | Repeat last command |
!abc | Run last command starting with abc |
!abc:p | Print last command starting with abc |
!$ | Last argument of previous command |
ALT-. | Last argument of previous command |
!* | All arguments of previous command |
^abc^123 | Run previous command, replacing abc with 123 |
Bash Variables
| Command | Description |
|---|
env | Show environment variables |
echo $NAME | Output value of the $NAME variable |
Bash Variables (cont.)
| Command | Description |
|---|
export NAME=value | Set $NAME to value |
$PATH | Executable search path |
$HOME | Home directory |
$SHELL | Current shell |
IO Redirection
| Command | Description |
|---|
cmd < file | Input of cmd from file |
cmd1 <(cmd2) | Output of cmd2 as file input to cmd1 |
cmd > file | Redirect stdout of cmd to file |
cmd > /dev/null | Discard stdout of cmd |
cmd >> file | Append stdout to file |
cmd 2> file | Redirect stderr of cmd to file |
cmd 1>&2 | Send stdout to the same place as stderr |
cmd 2>&1 | Send stderr to the same place as stdout |
cmd &> file | Redirect every output of cmd to file |
Pipes
| Command | Description |
|---|
cmd1 | cmd2 | Pipe stdout of cmd1 to cmd2 |
cmd1 |& cmd2 | Pipe stderr of cmd1 to cmd2 |
Command Lists
| Command | Description |
|---|
cmd1 ; cmd2 | Run cmd1 then cmd2 |
cmd1 && cmd2 | Run cmd2 if cmd1 is successful |
| `cmd1 | |
cmd & | Run cmd in a subshell |
Directory Operations
| Command | Description |
|---|
pwd | Show current directory |
mkdir dir | Create directory named dir |
cd dir | Change directory to dir |
cd .. | Go up one directory |
ls | List files |
ls Options
| Option | Description |
|---|
-a | Show all files (including hidden) |
-R | Recursive listing |
-r | Reverse order |
-t | Sort by last modified |
-S | Sort by file size |
-l | Long listing format |
-1 | One file per line |
-m | Comma-separated output |
-Q | Quoted output |
Search Files
| Command | Description |
|---|
grep pattern files | Search for pattern in files |
grep -i | Case insensitive search |
grep -r | Recursive search |
grep -v | Inverted search |
grep -o | Show only the matched part of file |
find /dir/ -name name* | Find files starting with name in dir |
Search Files (cont.)
| Command | Description |
|---|
find /dir/ -user name | Find files owned by name in dir |
find /dir/ -mmin num | Find files modified less than num minutes ago in dir |
whereis command | Find binary, source, or manual for command |
locate file | Quick search for file using the system index |
File Operations
| Command | Description |
|---|
touch file1 | Create file file1 |
cat file1 file2 | Concatenate files and output them |
less file1 | View and paginate file1 |
file file1 | Determine the file type of file1 |
cp file1 file2 | Copy file1 to file2 |
mv file1 file2 | Move or rename file1 to file2 |
rm file1 | Delete file1 |
head file1 | Show the first 10 lines of file1 |
tail file1 | Show the last 10 lines of file1 |
tail -F file1 | Continuously display new lines from file1 |
Text Processing
| Command | Description |
|---|
sort file | Sort lines alphabetically |
sort -n file | Sort numerically |
sort -r file | Sort in reverse |
sort -u file | Sort and remove duplicates |
uniq | Filter adjacent duplicate lines |
uniq -c | Count occurrences |
wc file | Count lines, words, bytes |
wc -l file | Count lines only |
cut -d: -f1 file | Cut first field (delimiter :) |
tr 'a-z' 'A-Z' | Translate lowercase to uppercase |
tr -d '\n' | Delete newlines |
sed (Stream Editor)
| Command | Description |
|---|
sed 's/old/new/' file | Replace first occurrence per line |
sed 's/old/new/g' file | Replace all occurrences |
sed -i 's/old/new/g' file | Edit file in place |
sed -n '5,10p' file | Print lines 5-10 |
sed '/pattern/d' file | Delete lines matching pattern |
awk
| Command | Description |
|---|
awk '{print $1}' file | Print first column |
awk -F: '{print $1}' file | Print first column (delimiter :) |
awk '/pattern/ {print}' file | Print lines matching pattern |
awk '{sum+=$1} END {print sum}' | Sum first column |
awk 'NR==5' file | Print line 5 |
awk 'NF>0' file | Print non-empty lines |
Networking Commands
| Command | Description |
|---|
ip addr | Show IP addresses |
ip -4 addr | Show IPv4 only |
ip route | Show routing table |
ip link | Show network interfaces |
ss -tuln | Show listening ports (TCP/UDP) |
ss -tunap | Show all connections with process |
ss -s | Socket statistics summary |
Network Diagnostics
| Command | Description |
|---|
ping -c 4 host | Send 4 ICMP packets |
traceroute host | Trace route to host |
mtr host | Continuous traceroute |
dig domain | DNS lookup |
dig +short domain | DNS lookup (short output) |
nslookup domain | DNS lookup (alternative) |
host domain | DNS lookup (simple) |
curl -I url | Get HTTP headers only |
curl -o file url | Download to file |
curl -X POST -d 'data' url | POST request |
wget url | Download file |
wget -r url | Recursive download |
Network Configuration
| Command | Description |
|---|
nmcli device status | NetworkManager device status |
nmcli connection show | Show connections |
hostnamectl | View/set hostname |
resolvectl status | DNS resolver status |
System Administration
systemd (Service Management)
| Command | Description |
|---|
systemctl status service | Check service status |
systemctl start service | Start service |
systemctl stop service | Stop service |
systemctl restart service | Restart service |
systemctl enable service | Enable at boot |
systemctl disable service | Disable at boot |
systemctl list-units --type=service | List all services |
systemctl list-units --failed | List failed units |
journalctl -u service | View service logs |
journalctl -f | Follow system logs |
journalctl --since "1 hour ago" | Logs from last hour |
Disk Usage
| Command | Description |
|---|
df -h | Disk space (human readable) |
df -i | Inode usage |
du -sh dir | Directory size |
du -h --max-depth=1 | Size of immediate subdirs |
lsblk | List block devices |
fdisk -l | List disk partitions |
mount | column -t | Show mounts formatted |
User Management
| Command | Description |
|---|
id | Current user info |
id username | User info for username |
groups | Show group membership |
useradd username | Create user |
usermod -aG group user | Add user to group |
passwd username | Change password |
su - username | Switch user |
sudo -i | Root shell |
last | Recent logins |
w | Who is logged in |
Watch a Command
| Command | Description |
|---|
watch -n 5 'ntpq -p' | Run the ntpq -p command every 5 seconds and display its output |
Process Management
| Command | Description |
|---|
ps | Display a snapshot of current processes |
ps aux | All processes with details |
ps aux | grep name | Find process by name |
top | Show real-time processes |
htop | Interactive process viewer |
kill pid | Terminate process with ID pid |
kill -9 pid | Force kill process |
pkill name | Terminate process with name name |
killall name | Terminate all processes starting with name |
pgrep name | Get PID by name |
nohup cmd & | Run command immune to hangups |
jobs | List background jobs |
fg | Bring job to foreground |
bg | Resume job in background |
Nano Shortcuts
Files
| Shortcut | Description |
|---|
Ctrl-R | Read file |
Ctrl-O | Save file |
Ctrl-X | Close file |
Cut and Paste
| Shortcut | Description |
|---|
ALT-A | Start marking text |
CTRL-K | Cut marked text or line |
CTRL-U | Paste text |
Navigate File
| Shortcut | Description |
|---|
ALT-/ | Go to end of file |
CTRL-A | Go to beginning of line |
CTRL-E | Go to end of line |
CTRL-C | Show line number |
CTRL-_ | Go to a specific line number |
Search File
| Shortcut | Description |
|---|
CTRL-W | Find |
ALT-W | Find next |
CTRL-\ | Search and replace |
More info: nano-editor.org
Screen Shortcuts
| Command | Description |
|---|
screen | Start a screen session |
screen -r | Resume a screen session |
screen -list | List current screen sessions |
Screen Shortcuts (cont.)
| Shortcut | Description |
|---|
CTRL-A | Activate screen commands |
CTRL-A c | Create a new terminal instance |
CTRL-A n | Switch to the next terminal instance |
CTRL-A p | Switch to the previous terminal instance |
CTRL-A " | Display a list of terminal instances |
CTRL-A A | Rename the current terminal instance |
More info: GNU Screen Manual
tmux Shortcuts
| Command | Description |
|---|
tmux | Start new session |
tmux new -s name | New named session |
tmux ls | List sessions |
tmux attach -t name | Attach to session |
tmux kill-session -t name | Kill session |
Inside tmux (prefix: CTRL-b)
| Shortcut | Description |
|---|
CTRL-b c | New window |
CTRL-b n | Next window |
CTRL-b p | Previous window |
CTRL-b % | Split vertical |
CTRL-b " | Split horizontal |
CTRL-b arrow | Switch pane |
CTRL-b d | Detach |
CTRL-b [ | Scroll mode (q to exit) |
File Permissions
| Command | Description |
|---|
chmod 775 file | Change mode of file to 775 |
chmod -R 600 folder | Recursively change permissions of folder to 600 |
chown user:group file | Change owner and group of file |
File Permission Numbers
Calculate permission digits by summing the following values:
- 4 – read (r)
- 2 – write (w)
- 1 – execute (x)
Example: For permissions rwxr-xr--, calculate:
- Owner: 4+2+1 = 7
- Group: 4+0+1 = 5
- Others: 4+0+0 = 4
Resulting in
754.
Common Permission Patterns
| Permission | Meaning |
|---|
644 | Owner read/write, others read (files) |
755 | Owner full, others read/execute (scripts, dirs) |
600 | Owner read/write only (private files) |
700 | Owner full only (private dirs) |
777 | Everyone full access (avoid on production) |
Archive and Compression
| Command | Description |
|---|
tar -cvf archive.tar dir/ | Create tar archive |
tar -xvf archive.tar | Extract tar archive |
tar -czvf archive.tar.gz dir/ | Create gzipped tar |
tar -xzvf archive.tar.gz | Extract gzipped tar |
tar -tvf archive.tar | List contents |
gzip file | Compress file |
gunzip file.gz | Decompress file |
zip -r archive.zip dir/ | Create zip |
unzip archive.zip | Extract zip |