Table of Contents
- File Commands
- Text Processing & Filtering
- Process Management
- System Info
- Compression
- Network Related
- Package Management
- Permissions & Ownership
- Users & Groups
- I/O Redirection & Pipes
- Disk & Filesystem
- Scheduling & Automation
- Terminal Multiplexers
- Vim Quick Reference
- Bash Scripting Basics
- Regex Basics
File Commands
| Command | Description | Example |
|---|---|---|
ls |
List directory contents | ls |
ls -al |
List all files with details | ls -al |
ls -lt |
List by modification time | ls -lt |
cd dir |
Change directory | cd /var/log |
cd |
Go to home | cd |
pwd |
Print current directory | pwd |
mkdir dir |
Make a directory | mkdir new_folder |
cat >file |
Create a file | cat > notes.txt |
more file |
View file page by page | more notes.txt |
head file |
First 10 lines | head notes.txt |
tail file |
Last 10 lines | tail notes.txt |
tail -f file |
Monitor file live | tail -f /var/log/syslog |
touch file |
Create empty file | touch empty.txt |
rm file |
Delete a file | rm notes.txt |
rm -r dir |
Delete a directory | rm -r old_folder |
rm -f file |
Force delete | rm -f locked.txt |
rm -rf dir |
Force delete dir | rm -rf /tmp/cache |
find path -name pattern |
Search for files | find . -name "*.log" |
find path -type f -mtime -1 |
Find recently modified files | find . -type f -mtime -1 |
locate name |
Find file via index (fast) | locate nginx.conf |
file name |
Detect file type | file archive.tar.gz |
stat file |
Detailed file metadata | stat notes.txt |
wc file |
Count lines/words/bytes | wc -l notes.txt |
diff file1 file2 |
Compare two files | diff old.txt new.txt |
nano file |
Simple terminal editor | nano notes.txt |
vim file |
Modal terminal editor | vim notes.txt |
Text Processing & Filtering
| Command | Description | Example |
|---|---|---|
grep pattern file |
Search text matching a pattern | grep "error" app.log |
grep -r pattern dir |
Recursive search | grep -r "TODO" src/ |
grep -i pattern file |
Case-insensitive search | grep -i "warning" app.log |
grep -v pattern file |
Invert match (exclude lines) | grep -v "^#" config.conf |
sed 's/a/b/' file |
Stream editor, find & replace | sed 's/foo/bar/g' file.txt |
awk '{print $1}' file |
Pattern scanning / column extraction | awk '{print $1, $3}' data.txt |
sort file |
Sort lines | sort names.txt |
sort -r file |
Sort in reverse | sort -r names.txt |
sort -n file |
Numeric sort | sort -n scores.txt |
uniq file |
Remove adjacent duplicate lines | sort file.txt | uniq |
uniq -c file |
Count occurrences | sort file.txt | uniq -c |
cut -d, -f1 file |
Extract columns/fields | cut -d, -f1,3 data.csv |
tr 'a' 'b' |
Translate/replace characters | echo hi | tr 'a-z' 'A-Z' |
xargs command |
Build args from stdin | find . -name "*.tmp" | xargs rm |
less file |
Paginated file viewer | less bigfile.log |
Process Management
| Command | Description | Example |
|---|---|---|
cp file1 file2 |
Copy a file | cp a.txt b.txt |
cp -r dir1 dir2 |
Copy directory | cp -r src/ backup/ |
mv file1 file2 |
Move/rename file | mv a.txt b.txt |
ln -s file link |
Symbolic link | ln -s /var/log/syslog loglink |
ps |
Show running processes | ps aux |
top |
Real-time process list | top |
kill pid |
Kill by PID | kill 1234 |
killall proc |
Kill by name | killall firefox |
pkill pattern |
Kill by pattern | pkill python |
bg |
Resume job in background | bg |
fg |
Foreground last job | fg |
fg n |
Foreground job n | fg %1 |
jobs |
List background jobs | jobs -l |
command & |
Run in background | long_script.sh & |
nohup command & |
Run immune to hangups | nohup python app.py & |
disown |
Detach job from shell | disown -h %1 |
htop |
Interactive process viewer | htop |
nice -n 10 cmd |
Run with lower priority | nice -n 10 backup.sh |
renice n pid |
Change priority of a running process | renice 10 -p 1234 |
System Info
| Command | Description | Example |
|---|---|---|
date |
Current date/time | date |
cal |
Monthly calendar | cal |
uptime |
System uptime | uptime |
w |
Who is logged in | w |
whoami |
Current user | whoami |
finger user |
User info | finger alice |
uname -a |
System info | uname -a |
cat /proc/cpuinfo |
CPU info | cat /proc/cpuinfo |
cat /proc/meminfo |
Memory info | cat /proc/meminfo |
man command |
Manual page | man ls |
df |
Disk usage | df -h |
du |
Directory usage | du -sh /home/user/ |
free |
Memory/swap usage | free -m |
whereis app |
App locations | whereis python |
which app |
Path to command | which python |
hostname |
Show hostname | hostname |
lscpu |
CPU architecture info | lscpu |
lsblk |
List block devices | lsblk |
dmesg |
Kernel ring buffer/boot logs | dmesg | tail |
history |
Command history | history | grep git |
alias |
Create command shortcut | alias ll='ls -al' |
env |
Show environment variables | env |
export VAR=val |
Set environment variable | export PATH=$PATH:/opt/bin |
echo $VAR |
Print variable value | echo $HOME |
shutdown |
Shut down system | shutdown -h now |
reboot |
Restart system | reboot |
systemctl status svc |
Check service status | systemctl status nginx |
systemctl start svc |
Start a service | systemctl start nginx |
systemctl enable svc |
Enable at boot | systemctl enable nginx |
journalctl |
View systemd logs | journalctl -u nginx -f |
Compression
| Command | Description | Example |
|---|---|---|
tar cf file.tar file |
Create tar | tar cf archive.tar file.txt |
tar xf file.tar |
Extract tar | tar xf archive.tar |
tar czf file.tar.gz files |
Create gzipped tar | tar czf archive.tar.gz *.txt |
tar xzf file.tar.gz |
Extract gzipped tar | tar xzf archive.tar.gz |
tar cjf file.tar.bz2 |
Create bzip2 archive | tar cjf archive.tar.bz2 *.log |
tar xjf file.tar.bz2 |
Extract bzip2 | tar xjf archive.tar.bz2 |
gzip file |
Compress file | gzip notes.txt |
gzip -d file.gz |
Decompress | gzip -d notes.txt.gz |
Network Related
| Command | Description | Example |
|---|---|---|
ping host |
Ping host | ping google.com |
whois domain |
Domain info | whois openai.com |
dig domain |
DNS info | dig openai.com |
dig -x host |
Reverse DNS | dig -x 8.8.8.8 |
wget file |
Download file | wget https://example.com/file.txt |
wget -c file |
Resume download | wget -c file.txt |
curl url |
Transfer data from/to a URL | curl -O https://example.com/file.txt |
curl -I url |
Fetch headers only | curl -I https://example.com |
ip a |
Show IP addresses | ip a |
ifconfig |
Show/configure interfaces (legacy) | ifconfig |
netstat -tulpn |
Show listening ports/sockets | netstat -tulpn |
ss -tulpn |
Modern socket stats | ss -tulpn |
traceroute host |
Trace network path | traceroute google.com |
nslookup domain |
Query DNS | nslookup openai.com |
scp file user@host:path |
Secure copy over SSH | scp file.txt user@server:/tmp/ |
ssh user@host |
Remote login | ssh user@192.168.1.10 |
ssh-keygen |
Generate SSH key pair | ssh-keygen -t ed25519 |
rsync -avz src dst |
Sync files/directories | rsync -avz src/ user@host:/backup/ |
Package Management
| Command | Description | Example |
|---|---|---|
apt update |
Refresh package index (Debian/Ubuntu) | sudo apt update |
apt install pkg |
Install a package | sudo apt install curl |
apt remove pkg |
Remove a package | sudo apt remove curl |
apt upgrade |
Upgrade installed packages | sudo apt upgrade |
yum install pkg |
Install package (RHEL/CentOS) | sudo yum install httpd |
dnf install pkg |
Install package (Fedora) | sudo dnf install httpd |
pacman -S pkg |
Install package (Arch) | sudo pacman -S vim |
dpkg -l |
List installed .deb packages | dpkg -l | grep nginx |
Permissions & Ownership
| Command | Description | Example |
|---|---|---|
chmod 755 file |
Set permissions | chmod 755 script.sh |
chmod +x file |
Add execute permission | chmod +x deploy.sh |
chmod u+w,g-w file |
Symbolic permission change | chmod u+w,g-w notes.txt |
su |
Become root | su |
su - user |
Switch user (with login shell) | su - alice |
sudo command |
Run as root | sudo apt update |
sudo -i |
Root login shell | sudo -i |
chown user file |
Change owner | chown alice notes.txt |
chown user:group file |
Change owner and group | chown alice:staff notes.txt |
chgrp group file |
Change group | chgrp staff notes.txt |
umask |
Show/set default permission mask | umask 022 |
Users & Groups
| Command | Description | Example |
|---|---|---|
whoami |
Current username | whoami |
id |
Show UID/GID/groups | id alice |
useradd name |
Create a new user | sudo useradd alice |
userdel name |
Delete a user | sudo userdel alice |
passwd name |
Set/change a password | sudo passwd alice |
groupadd name |
Create a new group | sudo groupadd staff |
usermod -aG group user |
Add user to a group | sudo usermod -aG sudo alice |
who |
List logged-in users | who |
last |
Login history | last -n 10 |
I/O Redirection & Pipes
| Operator | Description | Example |
|---|---|---|
> |
Redirect stdout (overwrite) | ls > files.txt |
>> |
Redirect stdout (append) | echo "log" >> app.log |
< |
Redirect stdin from a file | sort < names.txt |
2> |
Redirect stderr | cmd 2> errors.log |
&> |
Redirect stdout and stderr | cmd &> all.log |
| |
Pipe output to next command | ps aux | grep node |
&& |
Run next command if previous succeeds | make && make install |
|| |
Run next command if previous fails | ping -c1 host || echo down |
; |
Run commands sequentially | cd /tmp; ls |
tee file |
Split output to file and stdout | ls | tee out.txt |
$(command) |
Command substitution | echo "Today: $(date)" |
Disk & Filesystem
| Command | Description | Example |
|---|---|---|
mount device dir |
Mount a filesystem | mount /dev/sdb1 /mnt/usb |
umount dir |
Unmount a filesystem | umount /mnt/usb |
fdisk -l |
List/manage disk partitions | sudo fdisk -l |
mkfs.ext4 device |
Format a partition (ext4) | mkfs.ext4 /dev/sdb1 |
fsck device |
Check/repair filesystem | fsck /dev/sda1 |
blkid |
Show block device UUIDs/types | blkid |
df -h |
Disk space per filesystem | df -h |
du -sh * |
Size of each item in current dir | du -sh * |
cat /etc/fstab |
View filesystem mount table | cat /etc/fstab |
Scheduling & Automation
| Command | Description | Example |
|---|---|---|
crontab -e |
Edit current user's cron jobs | crontab -e |
crontab -l |
List current user's cron jobs | crontab -l |
* * * * * cmd |
Cron schedule (min hr dom mon dow) | 0 2 * * * /home/user/backup.sh |
at time |
Schedule a one-time job | echo "backup.sh" | at 22:00 |
watch command |
Repeat a command periodically | watch -n 5 df -h |
sleep n |
Pause for n seconds | sleep 10 && echo done |
Terminal Multiplexers
| Command | Description | Example |
|---|---|---|
tmux |
Start a new session | tmux new -s work |
tmux attach |
Reattach to a session | tmux attach -t work |
tmux ls |
List sessions | tmux ls |
Ctrl+B, D |
Detach from tmux session | ā |
screen |
Start a new screen session | screen -S work |
screen -r |
Resume a detached session | screen -r work |
Vim Quick Reference
| Key/Command | Action |
|---|---|
i |
Enter insert mode |
Esc |
Return to normal mode |
:w |
Save file |
:q |
Quit |
:wq |
Save and quit |
:q! |
Quit without saving |
dd |
Delete current line |
yy |
Copy (yank) current line |
p |
Paste |
/pattern |
Search forward for pattern |
u |
Undo |
Ctrl+R |
Redo |
gg / G |
Go to first / last line |
Bash Scripting Basics
| Construct | Description | Example |
|---|---|---|
#!/bin/bash |
Shebang line for a script | first line of script.sh |
VAR="value" |
Define a variable | NAME="Kalvin" |
if [ cond ]; then ... fi |
Conditional | if [ -f file.txt ]; then echo yes; fi |
for i in list; do ... done |
For loop | for i in 1 2 3; do echo $i; done |
while [ cond ]; do ... done |
While loop | while true; do echo hi; sleep 1; done |
function name() { ... } |
Define a function | greet() { echo "hi $1"; } |
$1, $2, ... |
Positional script arguments | echo "First arg: $1" |
$? |
Exit status of last command | echo $? |
chmod +x script.sh && ./script.sh |
Make executable and run | chmod +x deploy.sh && ./deploy.sh |
Regex Basics
Common patterns used with grep, sed, and awk.
| Pattern | Meaning | Example |
|---|---|---|
^ |
Start of line | grep "^Error" |
$ |
End of line | grep "done$" |
. |
Any single character | grep "h.t" |
* |
Zero or more of previous char | grep "ab*c" |
+ |
One or more (with -E/egrep) |
grep -E "ab+c" |
[abc] |
Character class | grep "[aeiou]" |
[^abc] |
Negated character class | grep "[^0-9]" |
\d |
Digit (with -P PCRE) |
grep -P "\d+" |
| |
Alternation (with -E) |
grep -E "cat|dog" |