Skip to main content

Linux Essentials

File & Directory Operations

# List with permissions and sizes
ls -lah

# Recursive listing
ls -laR /path

# Find files by name
find / -name "*.conf" 2>/dev/null

# Find files by permission
find / -perm -4000 2>/dev/null   # SUID files
find / -perm -2000 2>/dev/null   # SGID files
find / -perm -o+w 2>/dev/null    # World-writable

# Copy recursively
cp -r /src /dst

# Move / rename
mv old.txt new.txt

# Create directory tree
mkdir -p /a/b/c

File Permissions

# Change permissions (symbolic)
chmod u+x file.sh
chmod g-w file.txt
chmod o=r file.txt

# Change permissions (octal)
chmod 755 file.sh    # rwxr-xr-x
chmod 644 file.txt   # rw-r--r--
chmod 600 key.pem    # rw-------

# Change owner
chown user:group file.txt
chown -R user:group /dir

# Special bits
chmod u+s file    # Set SUID
chmod g+s dir     # Set SGID
chmod +t /tmp     # Sticky bit

Process Management

# List processes
ps aux
ps -ef

# Interactive process viewer
top
htop

# Kill by PID
kill -9 1234

# Kill by name
pkill -9 nginx
killall nginx

# Background / foreground
command &       # Run in background
Ctrl+Z          # Suspend
bg              # Resume in background
fg              # Bring to foreground
jobs            # List background jobs

# Nohup (survive logout)
nohup command &

Networking

# Show interfaces
ip a
ifconfig

# Show routing table
ip r
route -n

# Active connections
ss -tulnp
netstat -tulnp

# Test connectivity
ping -c 4 8.8.8.8
traceroute 8.8.8.8

# DNS lookup
nslookup domain.com
dig domain.com
host domain.com

# Download files
wget https://example.com/file
curl -O https://example.com/file
curl -L -o output.file https://example.com/redirect

Text Processing

# Search in files
grep -r "pattern" /path
grep -i "case insensitive" file.txt
grep -n "show line numbers" file.txt
grep -v "invert match" file.txt

# Stream editor
sed 's/old/new/g' file.txt
sed -i 's/old/new/g' file.txt   # In-place edit

# Print columns
awk '{print $1, $3}' file.txt
awk -F: '{print $1}' /etc/passwd

# Sort & unique
sort file.txt
sort -u file.txt
sort -rn numbers.txt   # Reverse numeric

# Count lines/words/chars
wc -l file.txt
wc -w file.txt

User Management

# Add user
useradd -m -s /bin/bash username
adduser username   # Interactive (Debian)

# Set password
passwd username

# Add to group
usermod -aG sudo username
usermod -aG docker username

# Switch user
su - username
sudo -u username command

# Who is logged in
who
w
last

System Info

# OS info
uname -a
cat /etc/os-release

# CPU / memory
lscpu
free -h
cat /proc/cpuinfo

# Disk usage
df -h
du -sh /path/*

# Kernel modules
lsmod
modprobe module_name

# System logs
journalctl -f
journalctl -u service-name
tail -f /var/log/syslog

File Transfer

# SCP
scp file.txt user@host:/remote/path
scp -r /local/dir user@host:/remote/

# Rsync
rsync -avz /src/ user@host:/dst/
rsync -avz --delete /src/ /dst/

# Python HTTP server (quick share)
python3 -m http.server 8080
python3 -m http.server 8080 --bind 0.0.0.0