Skip to main content
โšก Calmops

Linux Shell Commands: A Practical Reference

Introduction

This is a working reference for the Linux shell commands used most frequently in development, DevOps, and system administration. Commands are organized by task, with practical examples and the flags you’ll actually use.

Process Management

Find and Kill a Process

# Find a process by name
ps aux | grep nginx
pgrep nginx          # just the PID(s)
pidof nginx          # same, cleaner output

# Kill by PID
kill 1234            # SIGTERM โ€” graceful shutdown
kill -9 1234         # SIGKILL โ€” force kill (last resort)
kill -HUP 1234       # SIGHUP โ€” reload config (nginx, sshd)

# Kill by name
pkill nginx          # SIGTERM to all matching processes
pkill -9 nginx       # SIGKILL to all matching
killall nginx        # same as pkill

# Kill by port
fuser -k 8080/tcp    # kill whatever is using port 8080

Script pattern โ€” kill by name safely:

PROCESS_NAME="myapp"
pid=$(pgrep -x "$PROCESS_NAME")
if [ -n "$pid" ]; then
    kill "$pid"
    echo "Sent SIGTERM to $PROCESS_NAME (PID $pid)"
else
    echo "Process $PROCESS_NAME not found"
fi

Run a Process in the Background

# nohup: keep running after logout, output to nohup.out
nohup ./server &

# nohup with custom log file
nohup ./server > /var/log/server.log 2>&1 &

# Disown: detach from shell (already running process)
./server &
disown %1

# screen / tmux: persistent terminal sessions (preferred)
screen -S myapp
tmux new -s myapp

Monitor Processes

# Interactive process viewer
top
htop          # better UI, install with: apt install htop

# One-shot process list
ps aux                          # all processes, BSD format
ps -ef                          # all processes, UNIX format
ps aux --sort=-%cpu | head -10  # top 10 by CPU
ps aux --sort=-%mem | head -10  # top 10 by memory

# Process tree
pstree
pstree -p     # show PIDs

# Watch a command every 2 seconds
watch -n 2 'ps aux | grep myapp'

Network Operations

Check Open Ports and Connections

# ss (modern, preferred over netstat)
ss -tlnp          # TCP listening ports with process names
ss -ulnp          # UDP listening ports
ss -antp          # all TCP connections
ss -s             # summary statistics

# netstat (older, may need: apt install net-tools)
netstat -tlnp     # TCP listening
netstat -antp     # all TCP

# What's using port 8080?
ss -tlnp | grep :8080
lsof -i :8080
fuser 8080/tcp

Test Connectivity

# ICMP ping
ping google.com
ping -c 4 google.com    # send exactly 4 packets

# TCP port test (nc = netcat)
nc -zv google.com 443   # test if port is open
nc -zv -w 3 host 80     # with 3s timeout

# HTTP test
curl -I https://example.com              # headers only
curl -o /dev/null -s -w "%{http_code}\n" https://example.com  # status code only
curl -v https://example.com             # verbose (debug)

# Trace route
traceroute google.com
mtr google.com          # continuous traceroute (install: apt install mtr)

DNS Lookup

# dig โ€” detailed DNS info
dig example.com
dig example.com A        # A record only
dig example.com MX       # mail records
dig @8.8.8.8 example.com # use specific DNS server (Google)
dig +short example.com   # just the IP

# nslookup โ€” simpler
nslookup example.com

# host โ€” quick lookup
host example.com

Network Interface Info

ip addr                  # show all interfaces and IPs
ip addr show eth0        # specific interface
ip route                 # routing table
ip route get 8.8.8.8     # which interface reaches this IP

# Older commands (still common)
ifconfig
route -n

File Operations

Find Files

# find โ€” real-time search
find /var/log -name "*.log"                    # by name
find /home -name "*.py" -mtime -7              # modified in last 7 days
find /tmp -size +100M                          # larger than 100MB
find . -type f -name "*.conf" -exec grep -l "port" {} \;  # find then grep

# locate โ€” fast indexed search (update index: sudo updatedb)
locate nginx.conf
locate -i "*.log"    # case-insensitive

# which / whereis โ€” find executables
which python3
whereis nginx

Disk Usage

df -h                    # disk space by filesystem
df -h /var               # specific path
du -sh /var/log          # size of directory
du -sh /var/log/*        # size of each item
du -sh * | sort -h       # sorted by size

# Find large files
find / -size +1G -type f 2>/dev/null
du -ah /var | sort -rh | head -20

File Permissions

chmod 755 script.sh      # rwxr-xr-x
chmod +x script.sh       # add execute for all
chmod 600 ~/.ssh/id_rsa  # rw------- (private key)
chmod -R 644 /var/www    # recursive

chown user:group file
chown -R www-data:www-data /var/www

Archives

# tar
tar -czf archive.tar.gz directory/    # create gzip archive
tar -cjf archive.tar.bz2 directory/   # create bzip2 archive
tar -xzf archive.tar.gz               # extract gzip
tar -xzf archive.tar.gz -C /target/   # extract to specific dir
tar -tzf archive.tar.gz               # list contents

# zip
zip -r archive.zip directory/
unzip archive.zip
unzip archive.zip -d /target/

Text Processing

grep โ€” Search Text

grep "error" /var/log/syslog
grep -i "error" file.txt          # case-insensitive
grep -r "TODO" ./src/             # recursive
grep -n "error" file.txt          # show line numbers
grep -v "debug" file.txt          # invert (exclude matches)
grep -c "error" file.txt          # count matches
grep -A 3 "error" file.txt        # 3 lines after match
grep -B 3 "error" file.txt        # 3 lines before match
grep -E "error|warn" file.txt     # extended regex (OR)
grep -P "\d{4}-\d{2}-\d{2}" file  # Perl regex

sed โ€” Stream Editor

sed 's/old/new/' file.txt          # replace first occurrence per line
sed 's/old/new/g' file.txt         # replace all occurrences
sed -i 's/old/new/g' file.txt      # in-place edit
sed -n '10,20p' file.txt           # print lines 10-20
sed '/^#/d' file.txt               # delete comment lines
sed 's/[[:space:]]*$//' file.txt   # remove trailing whitespace

awk โ€” Column Processing

awk '{print $1}' file.txt          # print first column
awk '{print $1, $3}' file.txt      # print columns 1 and 3
awk -F: '{print $1}' /etc/passwd   # use : as delimiter
awk '{sum += $1} END {print sum}' numbers.txt  # sum a column
awk 'NR > 1' file.txt              # skip first line (header)
awk '$3 > 100 {print $1, $3}' data.txt  # filter by column value
ps aux | awk '{print $2, $11}' | head -10  # PID and command

sort, uniq, wc

sort file.txt                      # alphabetical sort
sort -n numbers.txt                # numeric sort
sort -rn numbers.txt               # reverse numeric
sort -k2 file.txt                  # sort by column 2
sort -u file.txt                   # sort and deduplicate

uniq file.txt                      # remove adjacent duplicates (sort first)
uniq -c file.txt                   # count occurrences
sort file.txt | uniq -c | sort -rn # frequency count

wc -l file.txt                     # line count
wc -w file.txt                     # word count
wc -c file.txt                     # byte count

System Information

# System
uname -a                 # kernel version and architecture
hostname                 # hostname
uptime                   # uptime and load average
lscpu                    # CPU info
nproc                    # number of CPU cores

# Memory
free -h                  # memory usage (human-readable)
cat /proc/meminfo        # detailed memory info

# Disk I/O
iostat -x 1              # I/O stats every second (apt install sysstat)
iotop                    # interactive I/O monitor

# System logs
journalctl -f            # follow system journal
journalctl -u nginx      # logs for specific service
journalctl --since "1 hour ago"
dmesg | tail -20         # kernel messages

Package Management

# Debian/Ubuntu (apt)
sudo apt update                    # refresh package list
sudo apt upgrade                   # upgrade all packages
sudo apt install nginx             # install
sudo apt remove nginx              # remove (keep config)
sudo apt purge nginx               # remove + config
sudo apt autoremove                # remove unused dependencies
apt search nginx                   # search packages
apt show nginx                     # package info

# RHEL/CentOS/Fedora (dnf)
sudo dnf install nginx
sudo dnf update
sudo dnf remove nginx
dnf search nginx

# Check what provides a file
apt-file search libssl.so          # Debian/Ubuntu
dnf provides libssl.so             # RHEL/Fedora

Quick Reference

Task Command
Find process by name pgrep nginx
Kill process gracefully kill $(pgrep nginx)
Check listening ports ss -tlnp
Test TCP port nc -zv host port
Find large files du -ah / | sort -rh | head -20
Search in files grep -r "pattern" ./
Replace in file sed -i 's/old/new/g' file
Count lines wc -l file
Follow logs journalctl -u service -f
Disk usage df -h

Resources

Comments