Linux Commands Cheat Sheet: Essential Commands for Beginners

2023/12/14

This Linux commands cheat sheet organizes all essential commands by function, covering file management, user management, permissions, package management, service management, networking, and firewall configuration — each with brief explanations and examples. Ideal for Linux beginners as a quick reference and for experienced users as a handy lookup table.

File and Directory Operations

These are the most commonly used commands in Linux — nearly every task involves these basic operations.

Directory Navigation and Listing

# Change to a specific directory
cd /home/benz/projects

# Go up one directory level
cd ..

# Go to the home directory
cd ~

# Print the current working directory path
pwd

# List all files and directories in the current directory
ls

# List contents of a specific path
ls /var/log

# List in long format (permissions, owner, size, date)
ls -l

# List all files including hidden ones (files starting with .)
ls -a

# Combine both options
ls -la

File Operations

# Copy a file
cp source.txt destination.txt

# Copy an entire directory (recursive)
cp -r /source/dir /destination/dir

# Move or rename a file
mv old_name.txt new_name.txt
mv file.txt /home/benz/documents/

# Delete a file (irreversible — use with caution)
rm file.txt

# Delete a directory and all its contents (extremely dangerous!)
rm -rf /path/to/directory

# Create an empty file (or update a file's timestamp)
touch new_file.txt

# Create a directory
mkdir my_folder

# Create nested directories (creates intermediate paths if they don't exist)
mkdir -p /home/benz/projects/new_project

Viewing File Contents

# Print the entire contents of a file
cat file.txt

# View the first 10 lines of a file
head file.txt

# View the first 20 lines of a file
head -n 20 file.txt

# View the last 10 lines of a file
tail file.txt

# Follow the end of a file in real time (commonly used for monitoring logs)
tail -f /var/log/nginx/access.log

# Browse a large file page by page (press q to quit)
less large_file.txt

Searching for Files

# Search for files by name in a specific directory
find /home/benz -name "*.txt"

# Find files larger than 100MB
find / -size +100M

# Find files modified within the last 7 days
find /var/log -mtime -7

# Use locate for fast searching (requires running updatedb first to build the index)
locate nginx.conf
sudo updatedb  # Update the locate index database

User Management

Linux is a multi-user system. Understanding user-related commands helps manage permissions and security.

# Display the current logged-in username
whoami

# Display detailed information about the current user and groups
id

# Display the current user from environment variables
echo $USER

# View all users on the system
cat /etc/passwd

# Temporarily switch to the root user (requires sudo privileges)
sudo su

# Switch to a specific user
su username

# Create a new user
sudo useradd username

# Create a new user and also create a home directory
sudo useradd -m username

# Set or change a user's password
sudo passwd username

# Add a user to a group (-aG means append to group without removing existing groups)
sudo usermod -aG sudo username
sudo usermod -aG docker username

# Change a user's login shell
sudo usermod -s /bin/bash username

Permission Management

Permission management is fundamental to Linux system security. For a detailed guide, see Linux Group Permission Management.

# View file permissions
ls -l file.txt
# Example output: -rw-r--r-- 1 benz staff 1024 Dec 14 10:00 file.txt

# Set permissions using numeric mode (755 = rwxr-xr-x)
chmod 755 script.sh

# Set permissions using symbolic mode
chmod u+x script.sh       # Add execute permission for the owner
chmod g-w file.txt        # Remove write permission for the group
chmod o=r file.txt        # Set others to read-only
chmod a+r file.txt        # Add read permission for everyone

# Recursively change permissions for an entire directory
chmod -R 755 /var/www/html

# Change the file owner
sudo chown benz file.txt

# Change both owner and group
sudo chown benz:developers file.txt

# Recursively change ownership of a directory
sudo chown -R benz:developers /home/benz/projects

# Change the group
sudo chgrp developers file.txt

Package Management

Different Linux distributions use different package management tools.

Ubuntu / Debian (using apt)

# Update the package list (recommended before installing)
apt update
# Or add -y to auto-confirm
sudo apt -y update

# Upgrade all installed packages
sudo apt -y upgrade

# Install a package
sudo apt install nginx
sudo apt -y install openssh-server

# Remove a package (keep configuration files)
sudo apt remove nginx

# Remove a package and delete its configuration files
sudo apt purge nginx

# Search for a package
apt search keyword

# View detailed package information
apt show nginx

Alpine Linux (using apk)

Alpine Linux is a lightweight Linux distribution commonly used in Docker containers.

# Update the package index
apk update

# Upgrade all installed packages
apk upgrade

# Install a package
apk add vim
apk add openssh-server
apk add net-tools  # Install network tools like ifconfig

# Remove a package
apk del package_name

# List all installed packages
apk info

# Search for a package
apk search keyword

Service Management

Ubuntu / Debian (using systemd)

Modern Linux distributions (Ubuntu 16.04+) generally use systemd to manage system services:

# Check the status of a service
systemctl status nginx

# Start a service
sudo systemctl start nginx

# Stop a service
sudo systemctl stop nginx

# Restart a service
sudo systemctl restart nginx

# Reload configuration (without interrupting the service)
sudo systemctl reload nginx

# Enable a service to start automatically on boot
sudo systemctl enable nginx

# Disable auto-start on boot
sudo systemctl disable nginx

# List all currently running services
systemctl list-units --type=service --state=running

# SSH service management example
sudo apt -y install openssh-server
sudo systemctl start sshd
sudo systemctl enable ssh

Alpine Linux (using OpenRC)

Alpine Linux uses OpenRC for service management:

# View all running services
rc-status

# List all services
rc-status -a

# List all services with their statuses
rc-status -s

# List all available service names
rc-service -l

# Start, stop, and restart a service
rc-service nginx start
rc-service nginx stop
rc-service nginx restart

# Add to startup
rc-update add nginx

# Remove from startup
rc-update del nginx

Networking

# View network interface information (legacy command)
ifconfig

# View network interface information (modern command)
ip addr

# View the routing table
ip route

# Test network connectivity (Ctrl+C to stop)
ping google.com

# Ping only 5 times
ping -c 5 google.com

# Trace the packet routing path
traceroute google.com

# Download a file or test an HTTP API
curl https://example.com
curl -o output.html https://example.com

# Download a file (wget is better suited for large file downloads)
wget https://example.com/file.tar.gz

# View current network connections (legacy)
netstat -tuln

# View current network connections (modern, recommended)
ss -tuln

# Show only listening TCP ports
ss -tlnp

Firewall (ufw)

UFW (Uncomplicated Firewall) is a convenient tool for managing iptables on Ubuntu:

# Enable the firewall
sudo ufw enable

# Disable the firewall
sudo ufw disable

# View firewall status (with rule numbers)
sudo ufw status numbered

# Allow a specific port (e.g., open HTTP port 80)
sudo ufw allow 80
sudo ufw allow 443
sudo ufw allow 22/tcp  # Allow SSH

# Allow by service name
sudo ufw allow ssh
sudo ufw allow http

# Deny a specific port
sudo ufw deny 8080

# Delete a rule (using the number from status numbered)
sudo ufw delete 2

# Allow a specific IP to access a specific port
sudo ufw allow from 192.168.1.100 to any port 22

# Reset all rules (dangerous!)
sudo ufw reset

Command History

# View command history (shows the most recent 500 entries by default)
history

# View the most recent 20 commands
history 20

# Search command history (very useful!)
history | grep nginx
history | grep apt

# Execute command number N from history
!42

# Re-execute the last command
!!

# Clear the current session's history
history -c

Frequently Asked Questions (FAQ)

Q1: Ubuntu uses apt, Alpine uses apk — what does CentOS / RHEL use?

Package managers for major Linux distributions:

DistributionPackage ManagerInstall Command Example
Ubuntu / Debianaptsudo apt install nginx
Alpineapkapk add nginx
CentOS / RHEL 7yumsudo yum install nginx
CentOS / RHEL 8+dnfsudo dnf install nginx
Arch Linuxpacmansudo pacman -S nginx

Q2: When do I need to use sudo?

sudo lets a regular user temporarily execute commands with root (superuser) privileges. You typically need sudo for:

  • Installing or removing system packages (sudo apt install ...)
  • Modifying system configuration files (/etc/, /var/)
  • Managing system services (sudo systemctl start ...)
  • Changing permissions on files or directories you don’t own
  • Managing user accounts (sudo useradd ...)

You generally don’t need sudo when working with files in your own home directory (/home/username/).


Further reading: