~/docs/bash-cheatsheet.html

Bash Nav & Manip

Navigation · File Ops · Text Processing · Permissions · Redirects · Search

📁 Navigation cd · ls · pwd
pwd Print current working directory
cd dir Change to directory
cd .. Go up one level
cd ~ Go to home directory
cd - Go to previous directory
ls flags
ls List directory contents
ls -l Long format (permissions, size, date)
ls -a Show hidden files (dotfiles)
ls -la Long format + hidden files
ls -lh Human-readable file sizes
ls -lt Sort by modification time
ls -lS Sort by file size
ls -R List recursively
ls -1 One file per line
🗂 File Operations cp · mv · rm · mkdir
cp — copy
cp src dst Copy file
cp -r src dst Copy directory recursively
cp -i src dst Prompt before overwriting
cp -u src dst Copy only if src is newer
cp -v src dst Verbose — show what's copied
mv — move / rename
mv src dst Move or rename file
mv -i src dst Prompt before overwriting
mv -n src dst Never overwrite existing
rm — remove
rm file Remove file
rm -r dir Remove directory recursively
rm -f file Force remove, no prompt
rm -rf dir Force recursive remove ⚠️
rm -i file Prompt before each removal
mkdir / rmdir
mkdir dir Create directory
mkdir -p a/b/c Create nested dirs (no error if exists)
rmdir dir Remove empty directory
touch file Create empty file / update timestamp
ln -s src lnk Create symbolic link
📄 Text Viewing & Processing cat · grep · sort · awk · sed
cat — concatenate/print
cat file Print file to stdout
cat -n file Number all lines
cat -A file Show special chars (tabs, EOL)
cat f1 f2 Concatenate two files
less / more — paging
less file Scrollable file viewer
less -N file Show line numbers
head -n 20 file First 20 lines
tail -n 20 file Last 20 lines
tail -f file Follow file (live updates)
sort
sort file Sort lines alphabetically
sort -r Reverse sort
sort -n Numeric sort
sort -u Sort and remove duplicates
sort -k2 Sort by 2nd field
sort -o file Write output back to file (safe in-place)
uniq
uniq file Remove adjacent duplicate lines
uniq -c Prefix count of occurrences
uniq -d Print only duplicate lines
uniq -u Print only unique lines
sed — stream editor
sed 's/a/b/' Replace first match per line
sed 's/a/b/g' Replace all matches (global)
sed -i 's/a/b/g' Edit file in-place
sed -n '5p' Print only line 5
sed '5d' Delete line 5
awk
awk '{print $1}' Print first field of each line
awk -F: '{print $1}' Use : as field separator
awk NR==5 Print line number 5
awk '{sum+=$1} END{print sum}' Sum a column
cut / tr / wc
cut -d, -f2 Cut field 2 using , delimiter
cut -c1-5 Cut characters 1 through 5
tr 'a-z' 'A-Z' Translate lowercase to upper
tr -d '\n' Delete newlines
wc -l Count lines
wc -w Count words
wc -c Count bytes/chars
🔒 Permissions & Ownership chmod · chown
chmod — change mode
chmod +x file Make executable
chmod -x file Remove executable
chmod 755 file rwxr-xr-x (owner full, others read/exec)
chmod 644 file rw-r--r-- (owner rw, others read)
chmod 600 file rw------- (owner only)
chmod -R 755 dir Apply recursively
chmod u+x Add exec for user (owner)
chmod g-w Remove write for group
chmod o=r Set others to read only
chown — change owner
chown user file Change file owner
chown user:grp file Change owner and group
chown -R user dir Recursive ownership change
chgrp grp file Change group only
reading permissions: rwxrwxrwx
r=4 w=2 x=1 Octal values to combine
u=user g=group o=other Symbolic notation targets
Redirects & Pipes > · | · < · >>
output redirection
> file Redirect stdout to file (overwrite)
>> file Redirect stdout to file (append)
2> file Redirect stderr to file
2>> file Append stderr to file
&> file Redirect both stdout and stderr
2>&1 Redirect stderr to same place as stdout
input redirection
< file Read stdin from file
<< EOF Here-document (multiline string input)
<<< "str" Here-string (single string to stdin)
pipes
cmd1 | cmd2 Pipe stdout of cmd1 to stdin of cmd2
cmd1 |& cmd2 Pipe stdout + stderr to cmd2
cmd | tee file Write to file AND pass through to stdout
cmd | tee -a file tee in append mode
special devices
> /dev/null Discard output entirely
2> /dev/null Suppress error messages
Command Control & Chaining && · || · ; · &
cmd1 && cmd2 Run cmd2 only if cmd1 succeeds
cmd1 || cmd2 Run cmd2 only if cmd1 fails
cmd1 ; cmd2 Run cmd2 regardless of cmd1 result
cmd & Run in background
! cmd Negate exit status
(cmd1; cmd2) Run in subshell
{ cmd1; cmd2; } Run in current shell (group)
job control
jobs List background jobs
fg %1 Bring job 1 to foreground
bg %1 Resume job 1 in background
Ctrl+Z Suspend current job
Ctrl+C Kill current process
kill %1 Kill job by job number
kill -9 PID Force kill process by PID
💾 Disk & System Info df · du · ps
disk usage
df -h Disk space, human-readable
df -T Show filesystem type
du -h dir Directory size, human-readable
du -sh dir Single summary total
du -ah All files and dirs
du --max-depth=1 Only one level deep
processes
ps aux All running processes
ps -ef Full process listing
top Live process monitor
htop Enhanced live monitor
pgrep name Find PID by process name
pkill name Kill process by name
system
uname -a OS and kernel info
whoami Current username
hostname Machine hostname
uptime System uptime and load
env List environment variables
echo $VAR Print environment variable
export VAR=val Set environment variable