🐧 Linux Command Line Cheatsheet

Your comprehensive guide to mastering the Linux terminal

🏠 Home
🔍
🌙
🚀 Getting Started
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 process
  • Ctrl+D - Exit shell (EOF)
  • Ctrl+L - Clear screen (same as clear)
  • Ctrl+A - Move to beginning of line
  • Ctrl+E - Move to end of line
  • Ctrl+R - Search command history
  • Tab - Auto-complete commands/files
Essential foundation
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 ls
Command 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
Know your way around
📁 Navigation & File Management
Navigation Commands

pwd - Print Working Directory

pwd
# Output: /home/user/documents

cd - Change Directory

# Go to directory
cd /var/log

# Go home
cd
cd ~

# Go to previous directory
cd -

# Go up one level
cd ..

# Go up two levels
cd ../..

# Go to user's home
cd ~username

ls - List Directory Contents

# Basic list
ls

# Long format (permissions, owner, size, date)
ls -l

# Show hidden files
ls -a

# Long format + hidden
ls -la

# Human-readable sizes
ls -lh

# Sort by modification time
ls -lt

# Reverse sort
ls -lr

# Recursive list
ls -R

# One file per line
ls -1

# Color output
ls --color=auto
💡 Tip: Create an alias: alias ll='ls -lah' for quick detailed listings
Master navigation
File Operations

touch - Create/Update Files

# Create empty file
touch newfile.txt

# Create multiple files
touch file1.txt file2.txt file3.txt

# Update timestamp
touch existingfile.txt

cp - Copy Files

# Copy file
cp source.txt destination.txt

# Copy to directory
cp file.txt /home/user/backup/

# Copy directory recursively
cp -r dir1 dir2

# Preserve attributes
cp -p file.txt backup.txt

# Interactive (ask before overwrite)
cp -i file.txt existing.txt

# Verbose output
cp -v file.txt backup.txt

mv - Move/Rename Files

# Rename file
mv oldname.txt newname.txt

# Move file
mv file.txt /home/user/documents/

# Move multiple files
mv file1.txt file2.txt /destination/

# Interactive
mv -i file.txt existing.txt

# No overwrite
mv -n file.txt existing.txt

rm - Remove Files

# Remove file
rm file.txt

# Remove multiple files
rm file1.txt file2.txt

# Remove directory (empty)
rmdir emptydir

# Remove directory recursively
rm -r directory

# Force remove (no prompts)
rm -rf directory

# Interactive
rm -i file.txt

# Verbose
rm -v file.txt
⚠️ Warning: rm -rf is permanent and dangerous! Always double-check before running.
File manipulation
Directory Operations

mkdir - Make Directory

# Create directory
mkdir newdir

# Create multiple directories
mkdir dir1 dir2 dir3

# Create parent directories
mkdir -p parent/child/grandchild

# Set permissions
mkdir -m 755 newdir

# Verbose
mkdir -v newdir

rmdir - Remove Directory

# Remove empty directory
rmdir emptydir

# Remove parent directories if empty
rmdir -p parent/child/grandchild

tree - Display Directory Tree

# Show tree (may need to install)
tree

# Limit depth
tree -L 2

# Show hidden files
tree -a

# Show only directories
tree -d

# Show with sizes
tree -h
Directory management
Viewing Files

cat - Concatenate & Display

# Display file
cat file.txt

# Display multiple files
cat file1.txt file2.txt

# Number lines
cat -n file.txt

# Show tabs as ^I
cat -T file.txt

# Combine files
cat file1.txt file2.txt > combined.txt

less - Page Through File

# View file (can scroll)
less file.txt

# Navigation in less:
# Space - Next page
# b - Previous page
# / - Search forward
# ? - Search backward
# n - Next match
# N - Previous match
# g - Go to beginning
# G - Go to end
# q - Quit

head - View Beginning

# First 10 lines (default)
head file.txt

# First n lines
head -n 20 file.txt
head -20 file.txt

# First n bytes
head -c 100 file.txt

tail - View End

# Last 10 lines (default)
tail file.txt

# Last n lines
tail -n 20 file.txt

# Follow file (live updates)
tail -f /var/log/syslog

# Follow with retry
tail -F logfile.log

more - Simple Pager

# View file page by page
more file.txt

# Space to continue, q to quit
File viewing
✏️ Text Processing
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.txt
Text 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 -c
Data manipulation
💻 System Information
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 @1234567890
System 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 -t
Hardware 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
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 pid
Process 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/log
Resource monitoring
🌐 Networking
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 established
Network 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.txt
Network 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
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
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
visudo
User 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 groupname
Group administration
📦 Package Management
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/file
Debian/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/file
RHEL/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.flatpakrepo
Universal packages
🗜️ Archives & Compression
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 archive
  • x - Extract archive
  • t - List contents
  • v - Verbose output
  • f - File name
  • z - Gzip compression
  • j - Bzip2 compression
  • J - Xz compression
💡 Tip: Remember "eXtract Zee File" for tar xzf
Archive mastery
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
Compression tools
📈 Performance & Monitoring
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" command
Performance 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.log
Log 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.timer
Task scheduling
📝 Shell Scripting
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 process
Script 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