🚀 Getting Started
Essential commands and concepts for beginning Linux users. Master the terminal, understand file paths, and learn basic shell operations.
The Shell & Terminal
What is a Shell?
A shell is a command-line interpreter that provides a user interface for Unix-like operating systems. The most common shell is Bash (Bourne Again Shell).
Common Shells
- bash - Default on most Linux distributions
- zsh - Popular alternative with advanced features
- fish - User-friendly with syntax highlighting
- sh - Original Unix shell
Check Your Shell
echo $SHELL # Output: /bin/bash # Check available shells cat /etc/shells
Terminal Shortcuts
Ctrl+C- Kill current processCtrl+D- Exit shell (EOF)Ctrl+L- Clear screen (same asclear)Ctrl+A- Move to beginning of lineCtrl+E- Move to end of lineCtrl+R- Search command historyTab- Auto-complete commands/files
Command Structure
Basic Syntax
command [options] [arguments]
Example Breakdown
ls -la /home/user │ │ └─ argument (directory to list) │ └──── option (long format + all files) └─────── command (list directory)
Options
- Short options:
-a -l -h - Combined:
-alh - Long options:
--all --list --human-readable
Getting Help
# Manual pages man ls # Quick help ls --help # Short description whatis ls # Search manuals apropos "list directory" # Info pages (more detailed) info lsCommand fundamentals
File System Hierarchy
Key Directories
/- Root directory (top of hierarchy)/home- User home directories/root- Root user's home directory/bin- Essential user binaries/sbin- System binaries (admin)/etc- Configuration files/var- Variable data (logs, caches)/tmp- Temporary files/usr- User programs and data/opt- Optional software/dev- Device files/proc- Process information/sys- System information
Path Types
# Absolute path (from root) cd /home/user/documents # Relative path (from current directory) cd documents cd ../downloads # Home directory shortcuts cd ~ # Go to your home cd ~/docs # Go to docs in home cd ~username # Go to username's home
Special Directories
.- Current directory..- Parent directory~- Home directory-- Previous directory
✏️ Text Processing
Powerful tools for viewing, searching, editing, and manipulating text files. Master grep, sed, awk, and other essential text utilities.
grep - Search Text
Basic Usage
# Search in file grep "pattern" file.txt # Case-insensitive grep -i "pattern" file.txt # Show line numbers grep -n "pattern" file.txt # Count matches grep -c "pattern" file.txt # Show only matching part grep -o "pattern" file.txt # Invert match (show non-matching) grep -v "pattern" file.txt
Advanced Options
# Recursive search in directory grep -r "pattern" /path/to/dir # Search multiple files grep "pattern" *.txt # Extended regex grep -E "pattern1|pattern2" file.txt # Show context (3 lines before/after) grep -C 3 "pattern" file.txt # Show only filenames grep -l "pattern" *.txt # Word boundary match grep -w "word" file.txt # Show files without match grep -L "pattern" *.txt
Regex Examples
# IP addresses
grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}" file.txt
# Email addresses
grep -E "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}" file.txt
# Lines starting with #
grep "^#" file.txt
# Lines ending with ;
grep ";$" file.txt
Powerful search
sed - Stream Editor
Basic Substitution
# Replace first occurrence sed 's/old/new/' file.txt # Replace all occurrences in line sed 's/old/new/g' file.txt # Replace and save to file sed -i 's/old/new/g' file.txt # Create backup before editing sed -i.bak 's/old/new/g' file.txt
Common Operations
# Delete lines containing pattern sed '/pattern/d' file.txt # Delete blank lines sed '/^$/d' file.txt # Print specific lines sed -n '5,10p' file.txt # Insert line before match sed '/pattern/i\New line' file.txt # Append line after match sed '/pattern/a\New line' file.txt # Multiple commands sed -e 's/old/new/g' -e '/pattern/d' file.txt
Advanced Examples
# Replace only on lines matching pattern sed '/pattern/s/old/new/g' file.txt # Case-insensitive replace sed 's/old/new/gi' file.txt # Use different delimiter sed 's|/old/path|/new/path|g' file.txtText transformation
awk - Text Processing
Basic Usage
# Print entire file
awk '{print}' file.txt
# Print specific column
awk '{print $1}' file.txt
# Print multiple columns
awk '{print $1, $3}' file.txt
# Print with custom separator
awk '{print $1 ":" $2}' file.txt
Field Separator
# Use custom delimiter
awk -F: '{print $1}' /etc/passwd
# Multiple delimiters
awk -F'[,:]' '{print $1, $2}' file.txt
# Tab delimiter
awk -F'\t' '{print $1}' file.txt
Patterns & Conditions
# Lines matching pattern
awk '/pattern/ {print}' file.txt
# Lines NOT matching
awk '!/pattern/ {print}' file.txt
# Conditional
awk '$3 > 100 {print $1, $3}' file.txt
# Multiple conditions
awk '$1 == "root" && $3 == 0' /etc/passwd
Built-in Variables
# NR - Line number
awk '{print NR, $0}' file.txt
# NF - Number of fields
awk '{print NF}' file.txt
# Print last field
awk '{print $NF}' file.txt
# FS - Field separator
awk 'BEGIN {FS=":"} {print $1}' /etc/passwd
Calculations
# Sum of column
awk '{sum += $3} END {print sum}' file.txt
# Average
awk '{sum += $3; count++} END {print sum/count}' file.txt
# Print with calculations
awk '{print $1, $2 * 2}' file.txt
Advanced processing
cut, sort, uniq
cut - Extract Columns
# Extract by character position cut -c 1-5 file.txt # Extract by field (default tab) cut -f 1,3 file.txt # Custom delimiter cut -d: -f1 /etc/passwd # Characters from position to end cut -c 5- file.txt
sort - Sort Lines
# Basic sort sort file.txt # Reverse sort sort -r file.txt # Numeric sort sort -n numbers.txt # Sort by column sort -k2 file.txt # Unique sort sort -u file.txt # Case-insensitive sort -f file.txt # Month sort sort -M dates.txt
uniq - Remove Duplicates
# Remove adjacent duplicates (requires sorted input) sort file.txt | uniq # Count occurrences sort file.txt | uniq -c # Show only duplicates sort file.txt | uniq -d # Show only unique lines sort file.txt | uniq -u # Ignore case sort file.txt | uniq -i
Common Combinations
# Most frequent lines sort file.txt | uniq -c | sort -rn # Extract unique emails cut -d: -f1 users.txt | sort | uniq # Count unique values in column cut -d, -f2 data.csv | sort | uniq -cData manipulation
💻 System Information
Discover system details including hardware, software, disk usage, and memory. Monitor your Linux system's health and configuration.
System Overview
uname - System Information
# Kernel name uname # All information uname -a # Kernel release uname -r # Machine hardware uname -m # Operating system uname -o
hostname - System Name
# Show hostname hostname # Show FQDN hostname -f # Show IP address hostname -I
uptime - System Uptime
# Show uptime and load uptime # Pretty format uptime -p # Since when running uptime -s
who/w - Logged In Users
# Show logged in users who # Detailed information w # Show only usernames who -q # Current user whoami
date - Date & Time
# Current date and time date # Custom format date +"%Y-%m-%d %H:%M:%S" # Unix timestamp date +%s # Date from timestamp date -d @1234567890System basics
Hardware Information
CPU Information
# Detailed CPU info lscpu # CPU info from /proc cat /proc/cpuinfo # Number of CPUs nproc # CPU model lscpu | grep "Model name"
Memory Information
# Memory usage free # Human-readable free -h # Show in MB free -m # Continuous updates free -h -s 5 # Detailed memory cat /proc/meminfo
Disk Information
# Disk usage by filesystem df # Human-readable df -h # Show filesystem type df -T # Inodes usage df -i # Specific filesystem df -h /home
PCI Devices
# List PCI devices lspci # Verbose lspci -v # Show tree lspci -t # Specific device lspci | grep VGA
USB Devices
# List USB devices lsusb # Verbose lsusb -v # Tree view lsusb -tHardware info
Disk Usage
du - Disk Usage
# Directory size du -sh /path/to/dir # All files and directories du -ah /path/to/dir # Depth limit du -h --max-depth=1 /home # Sort by size du -ah /path | sort -hr | head -20 # Exclude patterns du -sh --exclude="*.log" /var # Show total only du -s /path/to/dir
Finding Large Files
# Top 10 largest files
find /home -type f -exec du -h {} + | sort -rh | head -10
# Files larger than 100MB
find /home -type f -size +100M
# Find and list with sizes
find /var -type f -size +50M -exec ls -lh {} \;
ncdu - Interactive Disk Usage
# Interactive disk usage (if installed) ncdu /home # Navigate with arrows, delete with 'd'Disk analysis
⚙️ Process Management
Control running processes, manage system resources, and schedule tasks. Learn to monitor, kill, and prioritize processes effectively.
Viewing Processes
ps - Process Status
# Current shell processes ps # All processes ps aux # Process tree ps auxf # User's processes ps -u username # Process by name ps aux | grep processname # Custom format ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%cpu | head # Threads ps -eLf
top - Interactive Process Viewer
# Start top top # Shortcuts in top: # h - Help # k - Kill process # r - Renice process # M - Sort by memory # P - Sort by CPU # u - Filter by user # q - Quit # Batch mode (non-interactive) top -b -n 1 # Show specific user top -u username
htop - Enhanced Process Viewer
# Start htop (if installed) htop # Features: # - Color coded # - Mouse support # - Tree view (F5) # - Filter (F4) # - Search (F3) # - Kill (F9)
pstree - Process Tree
# Show process tree pstree # Show PIDs pstree -p # Show for specific user pstree username # Highlight process pstree -H pidProcess monitoring
Managing Processes
kill - Terminate Process
# Kill by PID kill 1234 # Force kill kill -9 1234 kill -KILL 1234 # Graceful termination kill -15 1234 kill -TERM 1234 # Hang up signal kill -HUP 1234 # List all signals kill -l
killall - Kill by Name
# Kill all processes by name killall firefox # Force kill killall -9 firefox # Interactive mode killall -i firefox # Kill by user killall -u username
pkill - Kill by Pattern
# Kill by pattern pkill -f "python script.py" # Kill by user pkill -u username # Signal to send pkill -TERM processname
Background & Foreground
# Run in background command & # Send to background (Ctrl+Z, then:) bg # Bring to foreground fg # List jobs jobs # Bring specific job to foreground fg %1 # Kill job kill %1 # Disown job (detach from shell) disown %1
nohup - No Hangup
# Run command immune to hangups nohup command & # Output to custom file nohup command > output.log 2>&1 &Process control
System Resources
vmstat - Virtual Memory Stats
# System stats vmstat # Update every 2 seconds vmstat 2 # With timestamps vmstat -t 2 # Disk statistics vmstat -d
iostat - I/O Statistics
# I/O stats iostat # Extended stats iostat -x # Update every 2 seconds iostat 2 # CPU only iostat -c # Disk only iostat -d
sar - System Activity Reporter
# CPU usage sar -u 1 10 # Memory usage sar -r 1 10 # I/O stats sar -b 1 10 # Network stats sar -n DEV 1 10 # Load average sar -q 1 10
lsof - List Open Files
# All open files lsof # Files opened by user lsof -u username # Files opened by process lsof -p 1234 # Network connections lsof -i # Specific port lsof -i :80 # Files in directory lsof +D /var/logResource monitoring
🌐 Networking
Network configuration, diagnostics, and tools for connectivity. From basic ping to advanced SSH and firewall management.
Network Configuration
ip - Network Configuration
# Show all interfaces ip addr show ip a # Show specific interface ip addr show eth0 # Show routes ip route show ip r # Show neighbors (ARP) ip neigh show # Bring interface up/down ip link set eth0 up ip link set eth0 down # Add IP address ip addr add 192.168.1.100/24 dev eth0 # Delete IP address ip addr del 192.168.1.100/24 dev eth0
ifconfig - Interface Configuration (Legacy)
# Show all interfaces ifconfig # Show specific interface ifconfig eth0 # Bring interface up/down ifconfig eth0 up ifconfig eth0 down # Assign IP ifconfig eth0 192.168.1.100 netmask 255.255.255.0
netstat - Network Statistics
# All connections netstat -a # Listening ports netstat -l # TCP connections netstat -t # UDP connections netstat -u # Numeric (no name resolution) netstat -n # Show program netstat -p # Common combination netstat -tulpn # Routing table netstat -r
ss - Socket Statistics (Modern)
# All sockets ss -a # Listening sockets ss -l # TCP sockets ss -t # UDP sockets ss -u # Show processes ss -p # Common combination ss -tulpn # Established connections ss state establishedNetwork setup
Network Testing
ping - Test Connectivity
# Ping host ping google.com # Count packets ping -c 4 google.com # Set interval ping -i 2 google.com # Flood ping (root only) ping -f host # Set packet size ping -s 1000 google.com
traceroute - Trace Route
# Trace route to host traceroute google.com # Use ICMP instead of UDP traceroute -I google.com # Max hops traceroute -m 15 google.com # No DNS resolution traceroute -n google.com
nslookup/dig - DNS Lookup
# DNS lookup nslookup google.com # Using dig (more detailed) dig google.com # Short answer dig +short google.com # Specific record type dig google.com MX dig google.com TXT # Reverse DNS dig -x 8.8.8.8 # Trace DNS path dig +trace google.com
wget/curl - Download Files
# Download file with wget wget https://example.com/file.txt # Continue interrupted download wget -c https://example.com/file.txt # Download with curl curl -O https://example.com/file.txt # Save with different name curl -o myfile.txt https://example.com/file.txt # Follow redirects curl -L https://example.com # Show headers only curl -I https://example.com
nc - Netcat
# Listen on port nc -l 1234 # Connect to host nc host 1234 # Port scan nc -zv host 20-80 # Transfer file # On receiver: nc -l 1234 > file.txt # On sender: nc host 1234 < file.txtNetwork diagnostics
Firewall & Security
ufw - Uncomplicated Firewall
# Enable firewall ufw enable # Disable firewall ufw disable # Check status ufw status # Allow port ufw allow 22 ufw allow ssh # Deny port ufw deny 80 # Delete rule ufw delete allow 80 # Allow from specific IP ufw allow from 192.168.1.100 # Reset firewall ufw reset
iptables - Advanced Firewall
# List rules iptables -L iptables -L -v -n # Allow incoming SSH iptables -A INPUT -p tcp --dport 22 -j ACCEPT # Block IP iptables -A INPUT -s 192.168.1.100 -j DROP # Delete rule iptables -D INPUT 1 # Save rules iptables-save > /etc/iptables/rules.v4 # Restore rules iptables-restore < /etc/iptables/rules.v4
ssh - Secure Shell
# Connect to host ssh user@hostname # Specify port ssh -p 2222 user@hostname # Execute command ssh user@hostname 'ls -la' # Copy SSH key ssh-copy-id user@hostname # Forward port ssh -L 8080:localhost:80 user@hostname # Reverse tunnel ssh -R 8080:localhost:80 user@hostname # Generate SSH key ssh-keygen -t rsa -b 4096
scp - Secure Copy
# Copy to remote scp file.txt user@hostname:/path/ # Copy from remote scp user@hostname:/path/file.txt . # Copy directory scp -r directory user@hostname:/path/ # Specify port scp -P 2222 file.txt user@hostname:/path/Network security
🔐 Permissions & Users
Manage file permissions, ownership, and user accounts. Understanding Linux security and access control fundamentals.
File Permissions
Understanding Permissions
# Format: -rwxrwxrwx # Position: -[owner][group][others] # # r = read (4) # w = write (2) # x = execute (1) # # Example: -rw-r--r-- # - = regular file # rw- = owner can read/write # r-- = group can read # r-- = others can read
chmod - Change Permissions
# Numeric mode chmod 755 file.txt # rwxr-xr-x chmod 644 file.txt # rw-r--r-- chmod 600 file.txt # rw------- chmod 777 file.txt # rwxrwxrwx (dangerous!) # Symbolic mode chmod u+x file.txt # Add execute for owner chmod g-w file.txt # Remove write for group chmod o=r file.txt # Set read only for others chmod a+r file.txt # Add read for all # Recursive chmod -R 755 directory/ # Reference another file chmod --reference=ref.txt file.txt
chown - Change Owner
# Change owner chown user file.txt # Change owner and group chown user:group file.txt # Change group only chown :group file.txt # Recursive chown -R user:group directory/ # Reference another file chown --reference=ref.txt file.txt
chgrp - Change Group
# Change group chgrp group file.txt # Recursive chgrp -R group directory/
umask - Default Permissions
# Show current umask umask # Set umask umask 022 # Default: 755 for dirs, 644 for files umask 077 # Restrictive: 700 for dirs, 600 for files # Calculate permissions: # Files: 666 - umask # Dirs: 777 - umask
💡 Common Permissions:
755 - Executables/directories
644 - Regular files
600 - Private files
700 - Private directories
Access control
755 - Executables/directories
644 - Regular files
600 - Private files
700 - Private directories
User Management
useradd/adduser - Add User
# Add user (basic) useradd username # Add user with home directory useradd -m username # Specify shell useradd -s /bin/bash username # Add with specific UID useradd -u 1500 username # Interactive (Debian/Ubuntu) adduser username
usermod - Modify User
# Change username usermod -l newname oldname # Change home directory usermod -d /new/home -m username # Change shell usermod -s /bin/zsh username # Add to group usermod -aG groupname username # Lock account usermod -L username # Unlock account usermod -U username
userdel - Delete User
# Delete user userdel username # Delete user and home directory userdel -r username
passwd - Change Password
# Change own password passwd # Change user's password (root) passwd username # Force password change on next login passwd -e username # Lock account passwd -l username # Unlock account passwd -u username # Show password status passwd -S username
su & sudo
# Switch user su username # Switch to root su - # Execute as root sudo command # Execute as different user sudo -u username command # Switch to root shell sudo -i sudo -s # Edit sudoers file visudoUser administration
Group Management
groupadd - Add Group
# Create group groupadd groupname # With specific GID groupadd -g 1500 groupname
groupmod - Modify Group
# Rename group groupmod -n newname oldname # Change GID groupmod -g 1600 groupname
groupdel - Delete Group
# Delete group groupdel groupname
View Groups
# Show user's groups groups # Show another user's groups groups username # List all groups cat /etc/group # Show group members getent group groupname # Show primary group id -gn # Show all groups with IDs id
Managing Group Members
# Add user to group gpasswd -a username groupname usermod -aG groupname username # Remove user from group gpasswd -d username groupname # Set group administrators gpasswd -A admin1,admin2 groupnameGroup administration
📦 Package Management
Install, update, and remove software packages. Master apt, yum, dnf, and other package managers across different Linux distributions.
apt (Debian/Ubuntu)
Basic Operations
# Update package list apt update # Upgrade all packages apt upgrade # Full upgrade (handle dependencies) apt full-upgrade # Install package apt install package-name # Remove package apt remove package-name # Remove package and config files apt purge package-name # Remove unused packages apt autoremove
Search & Information
# Search for package apt search keyword # Show package info apt show package-name # List installed packages apt list --installed # List upgradable packages apt list --upgradable
apt-cache
# Search package apt-cache search package # Show package details apt-cache show package # Show dependencies apt-cache depends package # Show reverse dependencies apt-cache rdepends package
dpkg - Low-level Package Manager
# Install .deb file dpkg -i package.deb # Remove package dpkg -r package # List installed packages dpkg -l # List package contents dpkg -L package # Check if package installed dpkg -s package # Find package owning file dpkg -S /path/to/fileDebian/Ubuntu packages
yum/dnf (RHEL/CentOS/Fedora)
dnf (Modern - Fedora 22+)
# Update package list dnf check-update # Update all packages dnf upgrade # Install package dnf install package-name # Remove package dnf remove package-name # Search package dnf search keyword # Show package info dnf info package-name # List installed packages dnf list installed # Clean cache dnf clean all
yum (Legacy - RHEL/CentOS 7)
# Update packages yum update # Install package yum install package-name # Remove package yum remove package-name # Search package yum search keyword # Show package info yum info package-name # List installed yum list installed # List available updates yum list updates # Clean cache yum clean all
rpm - Low-level Package Manager
# Install package rpm -ivh package.rpm # Update package rpm -Uvh package.rpm # Remove package rpm -e package # List installed packages rpm -qa # Query package info rpm -qi package # List package files rpm -ql package # Find package owning file rpm -qf /path/to/fileRHEL/Fedora packages
Snap & Flatpak
snap - Universal Packages
# Install snap snap install package-name # Remove snap snap remove package-name # List installed snaps snap list # Search for snaps snap find keyword # Update all snaps snap refresh # Update specific snap snap refresh package-name # Show snap info snap info package-name # Revert to previous version snap revert package-name
flatpak - Universal Packages
# Install flatpak flatpak install package-name # Remove flatpak flatpak uninstall package-name # List installed flatpak list # Search flatpak search keyword # Update all flatpak update # Run application flatpak run org.app.Name # Add repository (Flathub) flatpak remote-add --if-not-exists flathub \ https://flathub.org/repo/flathub.flatpakrepoUniversal packages
🗜️ Archives & Compression
Create, extract, and manage compressed archives. Work with tar, gzip, zip, and other compression formats for efficient file storage and transfer.
tar - Tape Archive
Create Archives
# Create tar archive tar -cf archive.tar directory/ # Create gzipped tar tar -czf archive.tar.gz directory/ # Create bzip2 tar tar -cjf archive.tar.bz2 directory/ # Create xz tar tar -cJf archive.tar.xz directory/ # Verbose output tar -cvzf archive.tar.gz directory/
Extract Archives
# Extract tar tar -xf archive.tar # Extract gzipped tar tar -xzf archive.tar.gz # Extract to specific directory tar -xzf archive.tar.gz -C /destination/ # Extract specific files tar -xzf archive.tar.gz file1.txt file2.txt # Verbose tar -xvzf archive.tar.gz
List & View
# List contents tar -tf archive.tar.gz # Verbose list tar -tvf archive.tar.gz # List specific pattern tar -tzf archive.tar.gz '*.txt'
Common Options
c- Create archivex- Extract archivet- List contentsv- Verbose outputf- File namez- Gzip compressionj- Bzip2 compressionJ- Xz compression
💡 Tip: Remember "eXtract Zee File" for
Archive mastery
tar xzf
Compression Tools
gzip/gunzip
# Compress file gzip file.txt # Creates: file.txt.gz (removes original) # Decompress gunzip file.txt.gz # Keep original gzip -k file.txt # Compress multiple files gzip file1.txt file2.txt # Best compression gzip -9 file.txt # View compressed file zcat file.txt.gz zless file.txt.gz
bzip2/bunzip2
# Compress file bzip2 file.txt # Creates: file.txt.bz2 # Decompress bunzip2 file.txt.bz2 # Keep original bzip2 -k file.txt # View compressed file bzcat file.txt.bz2 bzless file.txt.bz2
xz/unxz
# Compress file xz file.txt # Creates: file.txt.xz # Decompress unxz file.txt.xz # Keep original xz -k file.txt # View compressed file xzcat file.txt.xz xzless file.txt.xz
zip/unzip
# Create zip zip archive.zip file1.txt file2.txt # Zip directory zip -r archive.zip directory/ # Unzip unzip archive.zip # Unzip to directory unzip archive.zip -d /destination/ # List contents unzip -l archive.zip # Test integrity unzip -t archive.zip
Compression Comparison
- gzip: Fast, moderate compression
- bzip2: Slower, better compression
- xz: Slowest, best compression
- zip: Cross-platform, includes archiving
📈 Performance & Monitoring
Monitor system performance, track resource usage, and diagnose bottlenecks. Essential tools for maintaining optimal system health and performance.
Performance Tools
perf - Performance Analysis
# CPU profiling perf top # Record performance data perf record -a -g sleep 10 # View recorded data perf report # Stat mode perf stat command # CPU events perf stat -e cpu-cycles,instructions command
strace - System Call Trace
# Trace system calls strace command # Trace specific syscalls strace -e open,read command # Trace running process strace -p PID # Summary statistics strace -c command # Save to file strace -o output.txt command
ltrace - Library Call Trace
# Trace library calls ltrace command # Trace specific functions ltrace -e malloc,free command # Trace running process ltrace -p PID # Count calls ltrace -c command
time - Measure Execution
# Basic timing time command # Detailed timing /usr/bin/time -v command # Custom format /usr/bin/time -f "Time: %E Memory: %M KB" commandPerformance analysis
System Logs
journalctl - systemd Logs
# View all logs journalctl # Follow new messages journalctl -f # Since boot journalctl -b # Previous boot journalctl -b -1 # Specific service journalctl -u sshd # Time range journalctl --since "2024-01-01" --until "2024-01-31" journalctl --since "1 hour ago" # Priority level journalctl -p err # Kernel messages journalctl -k # Reverse order (newest first) journalctl -r
dmesg - Kernel Ring Buffer
# View kernel messages dmesg # Follow new messages dmesg -w # Human-readable timestamps dmesg -T # Show only errors dmesg -l err # Clear buffer (root) dmesg -C # Colored output dmesg --color=always
System Log Files
# Common log locations /var/log/syslog # System logs (Debian/Ubuntu) /var/log/messages # System logs (RHEL/CentOS) /var/log/auth.log # Authentication logs /var/log/kern.log # Kernel logs /var/log/boot.log # Boot logs /var/log/apache2/ # Apache logs /var/log/nginx/ # Nginx logs # View logs tail -f /var/log/syslog less /var/log/auth.logLog analysis
Scheduling Tasks
cron - Scheduled Tasks
# Edit crontab crontab -e # List crontab crontab -l # Remove crontab crontab -r # Edit for specific user (root) crontab -u username -e
Crontab Format
# Format: minute hour day month weekday command # * * * * * command # │ │ │ │ │ # │ │ │ │ └─── Day of week (0-7, Sun=0 or 7) # │ │ │ └───── Month (1-12) # │ │ └─────── Day of month (1-31) # │ └───────── Hour (0-23) # └─────────── Minute (0-59) # Examples: # Run every minute * * * * * /path/to/script.sh # Run every day at 2:30 AM 30 2 * * * /path/to/backup.sh # Run every Monday at 9 AM 0 9 * * 1 /path/to/weekly.sh # Run every 15 minutes */15 * * * * /path/to/task.sh # Run on first day of month 0 0 1 * * /path/to/monthly.sh
at - One-time Tasks
# Schedule command at 10:00 PM at> /path/to/command at> Ctrl+D # Schedule with time echo "/path/to/command" | at now + 1 hour echo "/path/to/command" | at 2:30 PM tomorrow # List scheduled jobs atq # Remove job atrm job_number # Show job details at -c job_number
systemd Timers
# List timers systemctl list-timers # Create timer (in /etc/systemd/system/) # mytask.timer: [Unit] Description=My Task Timer [Timer] OnCalendar=daily Persistent=true [Install] WantedBy=timers.target # Enable and start systemctl enable mytask.timer systemctl start mytask.timerTask scheduling
📝 Shell Scripting
Automate tasks with bash scripts. Learn variables, loops, conditionals, functions, and scripting best practices for powerful automation.
Script Basics
Script Structure
#!/bin/bash
# Shebang line (must be first)
# Comments start with #
# Variables
NAME="John"
AGE=30
# Use variables
echo "Hello, $NAME"
echo "Age: ${AGE}"
# Read input
read -p "Enter your name: " USERNAME
# Command substitution
CURRENT_DATE=$(date)
FILES=`ls -1`
# Exit codes
exit 0 # Success
exit 1 # Error
Making Scripts Executable
# Add execute permission chmod +x script.sh # Run script ./script.sh # Run with bash explicitly bash script.sh
Variables
# Assignment (no spaces!)
VAR="value"
# Use variables
echo $VAR
echo ${VAR}
# Command substitution
TODAY=$(date +%Y-%m-%d)
COUNT=`wc -l < file.txt`
# Arithmetic
NUM=$((5 + 3))
((NUM++))
let "NUM = 5 + 3"
# Arrays
ARRAY=(one two three)
echo ${ARRAY[0]}
echo ${ARRAY[@]} # All elements
echo ${#ARRAY[@]} # Length
Special Variables
# Script arguments $0 # Script name $1-$9 # Arguments 1-9 $@ # All arguments $# # Number of arguments $? # Exit status of last command $$ # Current process ID $! # PID of last background processScript fundamentals
Control Structures
if Statements
# Basic if
if [ condition ]; then
commands
fi
# if-else
if [ condition ]; then
commands
else
commands
fi
# if-elif-else
if [ condition1 ]; then
commands
elif [ condition2 ]; then
commands
else
commands
fi
# Examples
if [ "$NAME" = "John" ]; then
echo "Hello John"
fi
if [ $AGE -gt 18 ]; then
echo "Adult"
fi
if [ -f "file.txt" ]; then
echo "File exists"
fi
Test Conditions
# String comparisons [ "$a" = "$b" ] # Equal [ "$a" != "$b" ] # Not equal [ -z "$a" ] # Empty string [ -n "$a" ] # Not empty # Numeric comparisons [ $a -eq $b ] # Equal [ $a -ne $b ] # Not equal [ $a -gt $b ] # Greater than [ $a -ge $b ] # Greater or equal [ $a -lt $b ] # Less than [ $a -le $b ] # Less or equal # File tests [ -e file ] # Exists [ -f file ] # Regular file [ -d file ] # Directory [ -r file ] # Readable [ -w file ] # Writable [ -x file ] # Executable [ -s file ] # Non-empty # Logical operators [ cond1 ] && [ cond2 ] # AND [ cond1 ] || [ cond2 ] # OR ! [ condition ] # NOT
Loops
# for loop
for i in 1 2 3 4 5; do
echo $i
done
# C-style for
for ((i=0; i<5; i++)); do
echo $i
done
# Loop over files
for file in *.txt; do
echo "Processing $file"
done
# while loop
count=0
while [ $count -lt 5 ]; do
echo $count
((count++))
done
# until loop
count=0
until [ $count -ge 5 ]; do
echo $count
((count++))
done
# Read file line by line
while IFS= read -r line; do
echo "$line"
done < file.txt
case Statement
case "$1" in
start)
echo "Starting..."
;;
stop)
echo "Stopping..."
;;
restart)
echo "Restarting..."
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
;;
esac
Control flow
Functions & Advanced
Functions
# Define function
function greet() {
echo "Hello, $1"
}
# Alternative syntax
greet() {
echo "Hello, $1"
}
# Call function
greet "John"
# Return value
function add() {
local result=$(($1 + $2))
echo $result
}
sum=$(add 5 3)
echo $sum
# Function with return status
function check_file() {
if [ -f "$1" ]; then
return 0
else
return 1
fi
}
if check_file "file.txt"; then
echo "File exists"
fi
String Manipulation
# Length
${#VAR}
# Substring
${VAR:start:length}
${VAR:5} # From position 5
${VAR:5:3} # 3 chars from position 5
# Replace
${VAR/old/new} # Replace first
${VAR//old/new} # Replace all
# Remove pattern
${VAR#prefix} # Remove shortest prefix
${VAR##prefix} # Remove longest prefix
${VAR%suffix} # Remove shortest suffix
${VAR%%suffix} # Remove longest suffix
# Upper/Lower case
${VAR^^} # Upper case
${VAR,,} # Lower case
I/O Redirection
# Redirect output command > file # Overwrite command >> file # Append command 2> file # Redirect stderr command &> file # Redirect both command > file 2>&1 # Redirect stderr to stdout # Redirect input command < file # Here document cat << EOF Line 1 Line 2 EOF # Here string command <<< "string" # Pipe command1 | command2 # Tee (output to file and stdout) command | tee file.txt
Error Handling
# Exit on error
set -e
# Exit on undefined variable
set -u
# Print commands
set -x
# Trap errors
trap 'echo "Error on line $LINENO"' ERR
# Trap exit
trap 'echo "Cleaning up..."; rm temp.txt' EXIT
# Custom error handling
if ! command; then
echo "Error: command failed"
exit 1
fi
# Check exit status
command
if [ $? -ne 0 ]; then
echo "Command failed"
fi
Advanced scripting