💻
Bash and terminal cheatsheet covering navigation, file operations, text processing, pipes, process management, permissions, and network tools.
Updated September 15, 2026
bashshelllinuxterminalcommand-line
Navigation
| Command | Action |
|---|
pwd | Print current directory |
cd ~ | Go home |
cd - | Go to previous directory |
cd .. | Go up one level |
ls -la | List all files with details |
ls -lh | Human-readable file sizes |
ls -lt | Sort by modification time |
pushd <dir> | Push dir onto stack and cd |
popd | Pop dir from stack and cd |
dirs | Show directory stack |
File Operations
| Command | Action |
|---|
cp file dest | Copy file |
cp -r dir dest | Copy directory recursively |
mv file dest | Move or rename |
rm file | Delete file |
rm -rf dir | Delete directory recursively |
mkdir -p a/b/c | Create nested directories |
touch file | Create empty file or update timestamp |
ln -s target link | Create symbolic link |
find . -name "*.py" | Find files by name pattern |
find . -type f -mtime -7 | Files modified in last 7 days |
find . -size +10M | Files larger than 10MB |
du -sh * | Directory sizes |
df -h | Disk usage by filesystem |
stat file | Detailed file metadata |
Text Processing
| Command | Action |
|---|
cat file | Print file contents |
less file | Paginate file (q to quit) |
head -n 20 file | First 20 lines |
tail -n 20 file | Last 20 lines |
tail -f file | Follow file (live updates) |
grep "pattern" file | Search for pattern |
grep -r "pattern" . | Recursive search |
grep -i "pattern" | Case-insensitive |
grep -v "pattern" | Invert: lines NOT matching |
grep -n "pattern" | Show line numbers |
grep -c "pattern" | Count matching lines |
wc -l file | Count lines |
wc -w file | Count words |
sort file | Sort lines alphabetically |
sort -n | Numeric sort |
sort -r | Reverse sort |
uniq | Remove duplicate adjacent lines |
uniq -c | Count occurrences |
cut -d: -f1 file | Cut field 1 with : delimiter |
awk '{print $1}' file | Print first field |
awk -F: '{print $1, $3}' | Custom delimiter, multiple fields |
sed 's/old/new/g' file | Replace text (in output) |
sed -i 's/old/new/g' file | Replace text in-place |
tr 'a-z' 'A-Z' | Translate characters |
Pipes and Redirection
| Syntax | Action |
|---|
cmd1 | cmd2 | Pipe stdout of cmd1 to cmd2 |
cmd > file | Redirect stdout to file (overwrite) |
cmd >> file | Redirect stdout to file (append) |
cmd 2> file | Redirect stderr to file |
cmd 2>&1 | Redirect stderr to stdout |
cmd &> file | Redirect both stdout and stderr |
cmd < file | Use file as stdin |
cmd | tee file | Print and also save to file |
cmd1 && cmd2 | Run cmd2 only if cmd1 succeeds |
cmd1 || cmd2 | Run cmd2 only if cmd1 fails |
cmd1 ; cmd2 | Run cmd2 regardless of cmd1 result |
Process Management
| Command | Action |
|---|
cmd & | Run command in background |
jobs | List background jobs |
fg %1 | Bring job 1 to foreground |
bg %1 | Resume job 1 in background |
Ctrl+Z | Suspend current process |
Ctrl+C | Kill current process |
ps aux | List all processes |
ps aux | grep name | Find process by name |
top | Interactive process viewer |
htop | Better interactive viewer (if installed) |
kill <pid> | Send SIGTERM to process |
kill -9 <pid> | Send SIGKILL (force) |
killall name | Kill all processes by name |
pkill name | Kill processes matching name |
nohup cmd & | Run command immune to hangup |
Permissions
| Command | Action |
|---|
chmod 755 file | rwxr-xr-x (owner rwx, others r-x) |
chmod 644 file | rw-r--r-- |
chmod +x file | Make executable |
chmod -R 755 dir | Recursive |
chown user:group file | Change owner and group |
chown -R user dir | Recursive ownership change |
umask 022 | Default permission mask |
Permission bits: 4 = read, 2 = write, 1 = execute. Add for each group (owner/group/others).
Archives
tar -czf archive.tar.gz dir/ # create gzip archive
tar -xzf archive.tar.gz # extract gzip archive
tar -cjf archive.tar.bz2 dir/ # create bzip2 archive
tar -xjf archive.tar.bz2 # extract bzip2
tar -tf archive.tar.gz # list contents without extracting
gzip file # compress file -> file.gz
gunzip file.gz # decompress
zip -r archive.zip dir/ # zip directory
unzip archive.zip # unzip
Network
| Command | Action |
|---|
curl https://url | Fetch URL |
curl -O url | Download file keeping name |
curl -L url | Follow redirects |
curl -H "Auth: token" url | Custom header |
curl -X POST -d '{}' url | POST with body |
wget url | Download file |
ssh user@host | SSH login |
ssh -p 2222 user@host | SSH on custom port |
scp file user@host:/path | Copy file to remote |
scp user@host:/path file | Copy file from remote |
ping host | Test connectivity |
traceroute host | Trace network path |
netstat -tulnp | Open ports and listeners |
ss -tulnp | Faster alternative to netstat |
nmap -sn 192.168.1.0/24 | Scan local network |
Bash Shortcuts
| Shortcut | Action |
|---|
Ctrl+A | Jump to line start |
Ctrl+E | Jump to line end |
Ctrl+K | Delete from cursor to end |
Ctrl+U | Delete from cursor to start |
Ctrl+W | Delete word before cursor |
Alt+. | Insert last argument of previous command |
Ctrl+R | Reverse search command history |
!! | Repeat last command |
!$ | Last argument of previous command |
!string | Run last command starting with string |
Ctrl+L | Clear screen |
Special Variables
| Variable | Value |
|---|
$? | Exit code of last command (0 = success) |
$! | PID of last background process |
$$ | PID of current shell |
$0 | Script name |
$1, $2 | Script arguments |
$@ | All arguments as separate words |
$# | Number of arguments |
$HOME | Home directory |
$PATH | Executable search path |
$PWD | Current directory |