Ubuntu & CLI Commands Reference
This page provides a comprehensive list of useful commands for Ubuntu and general command-line operations.
System Information
# View system information
uname -a # Kernel information
lsb_release -a # Ubuntu version
cat /etc/os-release # OS information
cat /proc/cpuinfo # CPU information
cat /proc/meminfo # Memory information
free -h # View memory usage with human-readable units
df -h # Disk usage
lsblk # List block devices
sudo lshw # Hardware information
inxi -F # System information (may need: sudo apt install inxi)
File Operations
# Navigation
pwd # Print working directory
ls # List files
ls -la # List all files with details
cd directory # Change directory
cd .. # Go up one directory
cd ~ # Go to home directory
cd - # Go to previous directory
# File manipulation
touch file.txt # Create empty file
mkdir directory # Create directory
mkdir -p dir1/dir2/dir3 # Create nested directories
cp file.txt backup.txt # Copy file
cp -r dir1 dir2 # Copy directory recursively
mv file.txt newname.txt # Rename/move file
mv file.txt directory/ # Move file to directory
rm file.txt # Delete file
rm -i file.txt # Delete with confirmation
rm -r directory # Delete directory recursively
rm -rf directory # Force delete directory
# File examination
cat file.txt # Display file contents
less file.txt # View file with pagination
head -n 10 file.txt # Display first 10 lines
tail -n 10 file.txt # Display last 10 lines
tail -f log.txt # Display and follow file updates
grep "text" file.txt # Search for text in file
find /path -name "*.txt" # Find files with .txt extension
User Management
# User info
whoami # Show current username
id # Show user ID and groups
who # Show logged-in users
w # Show who is logged in and what they're doing
# User/group management
sudo adduser username # Create new user (interactive)
sudo useradd username # Create new user (non-interactive)
sudo userdel username # Delete user
sudo passwd username # Change user password
sudo usermod -aG group user # Add user to group
sudo groupadd groupname # Create new group
sudo groupdel groupname # Delete group
Permissions
# View and modify
ls -l # List with permissions
chmod 755 file.txt # Change file permissions (rwx for owner, rx for others)
chmod -R 755 directory # Change permissions recursively
chmod u+x script.sh # Add execute permission for user
chown user:group file.txt # Change file owner and group
chown -R user:group dir # Change ownership recursively
# Common permission numbers
# 777 - rwxrwxrwx - Everyone can read, write, execute
# 755 - rwxr-xr-x - Owner can read, write, execute; others can read, execute
# 644 - rw-r--r-- - Owner can read, write; others can read
# 700 - rwx------ - Owner can read, write, execute; others have no permissions
Package Management
# APT commands
sudo apt update # Update package lists
sudo apt upgrade # Upgrade all installed packages
sudo apt dist-upgrade # Smart upgrade (handles dependencies)
sudo apt full-upgrade # Upgrade with removals if necessary
sudo apt install package # Install package
sudo apt remove package # Remove package
sudo apt purge package # Remove package and configuration
sudo apt autoremove # Remove unused dependencies
apt search keyword # Search for packages
apt show package # Show package details
apt list --installed # List installed packages
# Snap commands
snap list # List installed snaps
sudo snap install package # Install snap
sudo snap refresh package # Update snap
sudo snap remove package # Remove snap
snap find keyword # Search for snaps
# .deb files
sudo dpkg -i package.deb # Install .deb file
sudo apt install -f # Fix dependencies after dpkg
Process Management
# View processes
ps # Show running processes
ps aux # Show all processes
top # Interactive process viewer
htop # Enhanced process viewer (may need: sudo apt install htop)
pgrep process_name # Find process ID by name
# Process control
kill PID # Terminate process by ID
killall process_name # Terminate processes by name
pkill process_name # Pattern-kill processes
kill -9 PID # Force kill process
nice -n 10 command # Run command with lower priority
renice +10 -p PID # Change priority of running process
Networking
# Connectivity
ping google.com # Test network connectivity
ping -c 4 google.com # Ping 4 times only
wget url # Download file from web
curl url # Fetch URL content
curl -O url # Download file with original name
# Network information
ifconfig # Show network interfaces (deprecated)
ip addr # Show IP addresses
ip route # Show routing table
netstat -tuln # Show listening ports
ss -tuln # Show listening ports (newer alternative)
hostname -I # Show IP addresses
dig domain.com # DNS lookup
nslookup domain.com # DNS lookup
whois domain.com # Domain registration info
# Network configuration
sudo dhclient -r # Release DHCP address
sudo dhclient # Renew DHCP address
sudo ip link set eth0 up # Bring interface up
sudo ip link set eth0 down # Bring interface down
SSH and Remote Access
# SSH commands
ssh user@hostname # Connect to remote host
ssh -p 2222 user@hostname # Connect on specific port
ssh-keygen -t rsa # Generate SSH key pair
ssh-copy-id user@hostname # Copy SSH key to remote host
scp file.txt user@host:/path # Copy file to remote host
scp user@host:/path/file.txt . # Copy from remote to local
rsync -av dir/ user@host:/path # Sync directory to remote
# Screen and tmux for persistent sessions
screen # Start screen session
screen -r # Reattach to screen session
tmux # Start tmux session
tmux ls # List sessions
tmux attach -t 0 # Attach to session 0
Text Processing
# Text manipulation
grep pattern file.txt # Search for pattern
grep -r pattern directory # Recursive search
grep -i pattern file.txt # Case-insensitive search
grep -v pattern file.txt # Invert match (lines without pattern)
sed 's/old/new/g' file.txt # Replace text
awk '{print $1}' file.txt # Print first column
sort file.txt # Sort lines
uniq file.txt # Remove duplicate lines
wc -l file.txt # Count lines
wc -w file.txt # Count words
cut -d, -f1 file.csv # Extract first column from CSV
System Services
# Systemd service management
systemctl status service # Check service status
sudo systemctl start service # Start service
sudo systemctl stop service # Stop service
sudo systemctl restart service # Restart service
sudo systemctl enable service # Enable at boot
sudo systemctl disable service # Disable at boot
sudo systemctl list-units --type=service # List services
journalctl -u service # View service logs
Disk and File Systems
# Disk operations
sudo fdisk -l # List disk partitions
sudo mkfs.ext4 /dev/sdX1 # Format partition as ext4
sudo mount /dev/sdX1 /mnt # Mount filesystem
sudo umount /mnt # Unmount filesystem
sudo blkid # List block device attributes
sudo e2fsck -f /dev/sdX1 # Check filesystem
sudo smartctl -a /dev/sda # Disk health (needs smartmontools)
# Disk usage
du -sh directory # Directory size (summarized, human-readable)
du -h --max-depth=1 / # Size of top-level directories
ncdu # Interactive disk usage analyzer
Archive and Compression
# tar operations
tar -cvf archive.tar files/ # Create tar archive
tar -xvf archive.tar # Extract tar archive
tar -czvf archive.tar.gz files/ # Create compressed tar archive
tar -xzvf archive.tar.gz # Extract compressed tar archive
tar -cjvf archive.tar.bz2 files/ # Create bzip2 compressed archive
tar -xjvf archive.tar.bz2 # Extract bzip2 compressed archive
# Other compression tools
zip -r archive.zip directory # Create zip archive
unzip archive.zip # Extract zip archive
gzip file.txt # Compress file with gzip
gunzip file.txt.gz # Decompress gzip file
bzip2 file.txt # Compress file with bzip2
bunzip2 file.txt.bz2 # Decompress bzip2 file
System Monitoring and Maintenance
# System monitoring
uptime # Show system uptime
free -m # Memory usage in MB
vmstat # Virtual memory statistics
iostat # I/O statistics
mpstat # CPU statistics
sar # System activity reporter (needs sysstat)
dmesg # Kernel ring buffer
# System maintenance
sudo apt clean # Clean apt cache
sudo apt autoclean # Remove outdated packages
sudo update-grub # Update GRUB bootloader
sudo update-initramfs -u # Update initramfs
sudo dpkg-reconfigure package # Reconfigure package
Cron Jobs and Scheduling
# Cron commands
crontab -l # List user's cron jobs
crontab -e # Edit user's cron jobs
sudo crontab -u user -l # List another user's cron jobs
# Format: minute hour day-of-month month day-of-week command
# Example: 0 5 * * * /path/to/script.sh # Run at 5am every day
Firewall
# UFW (Uncomplicated Firewall)
sudo ufw status # Check firewall status
sudo ufw enable # Enable firewall
sudo ufw disable # Disable firewall
sudo ufw allow 22 # Allow SSH port
sudo ufw deny 80 # Deny HTTP port
sudo ufw delete deny 80 # Remove rule
sudo ufw allow from 192.168.1.0/24 # Allow from subnet
Useful Shortcuts
Ctrl+C # Interrupt (kill) current process
Ctrl+Z # Suspend current process
Ctrl+D # Exit current shell
Ctrl+L # Clear the screen
Ctrl+A # Move to beginning of line
Ctrl+E # Move to end of line
Ctrl+U # Cut from cursor to beginning of line
Ctrl+K # Cut from cursor to end of line
Ctrl+W # Cut the word before the cursor
Ctrl+Y # Paste from the kill buffer
Ctrl+R # Search command history
Tab # Auto-complete commands/filenames
Up/Down arrows # Navigate through command history
System Logs
# Viewing logs
cat /var/log/syslog # System logs
cat /var/log/auth.log # Authentication logs
cat /var/log/kern.log # Kernel logs
journalctl # View systemd journal logs
journalctl -f # Follow journal logs
journalctl --since today # Logs from today
journalctl -b # Logs from current boot
Advanced Tools
# Advanced system utilities
strace command # Trace system calls
ltrace command # Trace library calls
lsof # List open files
lsof -i :80 # Show process using port 80
sudo iotop # I/O monitoring (may need: sudo apt install iotop)
sudo powertop # Power usage monitoring (may need: sudo apt install powertop)
Shell Scripting Basics
#!/bin/bash # Shebang line
# Variables
NAME="Ubuntu"
echo $NAME
# Conditionals
if [ "$NAME" = "Ubuntu" ]; then
echo "This is Ubuntu"
else
echo "This is not Ubuntu"
fi
# Loops
for i in {1..5}; do
echo $i
done
# Functions
hello() {
echo "Hello, $1!"
}
hello "World"
This reference covers the most commonly used commands but is by no means exhaustive. Remember that most commands have a man page (man command
) and help option (command --help
) for more detailed information.