funwithlinux guide

Essential Linux Commands Every User Should Know

Linux, the open-source operating system powering servers, desktops, smartphones, and even IoT devices, is renowned for its flexibility and control. While graphical user interfaces (GUIs) simplify everyday tasks, the **command-line interface (CLI)** remains the backbone of Linux power users, sysadmins, and developers. Mastering basic Linux commands unlocks efficiency, automation, and troubleshooting capabilities that GUIs can’t match. Whether you’re a beginner setting up your first Linux machine or a seasoned user looking to refresh your skills, this guide covers the most essential Linux commands. We’ll break them down by use case, explain their syntax, and provide practical examples to help you apply them immediately. Let’s dive in!

Table of Contents

  1. File and Directory Navigation

  2. File and Directory Operations

  3. Viewing File Contents

  4. File Permissions

  5. System Information

  6. Package Management

  7. Networking Commands

  8. Process Management

  9. User Management

  10. Advanced Tips: Aliases, Pipes, and Redirection

  11. Conclusion

  12. Reference

1. File and Directory Navigation

Navigating the Linux filesystem is the first step to mastering the CLI. These commands help you orient yourself and move between directories.

pwd: Print Working Directory

Purpose: Displays the full path of your current directory.
Syntax:

pwd  

Example:

user@linux:~/Documents$ pwd  
/home/user/Documents  

ls: List Directory Contents

Purpose: Lists files and directories in the current (or specified) directory.
Common Options:

  • -l: Long format (shows permissions, owner, size, timestamp).
  • -a: Show hidden files (those starting with .).
  • -h: Human-readable sizes (e.g., 1K, 2M).
  • -t: Sort by modification time (newest first).

Syntax:

ls [options] [directory]  

Examples:

  • List all files in long format:
    ls -l  
  • List hidden files in the /etc directory:
    ls -a /etc  

cd: Change Directory

Purpose: Navigate to a different directory.
Shortcuts:

  • cd: Go to your home directory.
  • cd ~: Same as above (shorthand for home).
  • cd ..: Move up one directory.
  • cd -: Switch to the previous directory.

Syntax:

cd [directory-path]  

Examples:

  • Go to /var/log:
    cd /var/log  
  • Move up two directories:
    cd ../../  

2. File and Directory Operations

These commands let you create, copy, move, and delete files/directories.

mkdir: Create a Directory

Purpose: Make a new directory (folder).
Option: -p: Create parent directories if they don’t exist.

Syntax:

mkdir [options] directory-name  

Example:

  • Create a nested directory project/src/js:
    mkdir -p project/src/js  

touch: Create an Empty File

Purpose: Create a new empty file or update the timestamp of an existing file.

Syntax:

touch filename  

Example:

  • Create notes.txt:
    touch notes.txt  

cp: Copy Files/Directories

Purpose: Copy files or directories from one location to another.
Options:

  • -r: Recursively copy directories (required for folders).
  • -v: Verbose mode (show what’s being copied).

Syntax:

cp [options] source destination  

Examples:

  • Copy file.txt to backup/:
    cp file.txt backup/  
  • Copy the docs directory to archive/:
    cp -r docs archive/  

mv: Move or Rename Files/Directories

Purpose: Move files/directories to a new location or rename them.

Syntax:

mv [source] [destination]  

Examples:

  • Rename old.txt to new.txt:
    mv old.txt new.txt  
  • Move report.pdf to ~/Downloads:
    mv report.pdf ~/Downloads  

rm: Delete Files/Directories

Purpose: Remove (delete) files or directories.
Caution: rm is irreversible! Always double-check paths.
Options:

  • -r: Recursively delete directories (required for folders).
  • -f: Force deletion (ignore nonexistent files, no prompts).
  • -v: Verbose mode.

Syntax:

rm [options] file/directory  

Examples:

  • Delete temp.txt:
    rm temp.txt  
  • Delete the old-files directory and its contents:
    rm -r old-files  

⚠️ Warning: rm -rf / (force delete everything) will irreversibly destroy your system. Never run this!

3. Viewing File Contents

Need to read a file? These commands help you inspect text files efficiently.

cat: Concatenate and Print Files

Purpose: Display the entire contents of a file (best for small files).
Options:

  • -n: Number lines.
  • -b: Number non-blank lines.

Syntax:

cat [options] filename  

Example:

  • Display todo.txt with line numbers:
    cat -n todo.txt  

more/less: Paginate Through Large Files

Purpose: View large files one page at a time (avoids flooding the screen).

  • more: Basic pagination (scroll down with Enter; exit with q).
  • less: Advanced (scroll up/down with arrow keys; search with /keyword; exit with q).

Syntax:

more filename  
less filename  

Example:

  • Read a large log file:
    less /var/log/syslog  

head/tail: View First/Last Lines of a File

Purpose:

  • head: Show the first N lines (default: 10).
  • tail: Show the last N lines (default: 10).
    Option: -n: Specify the number of lines (e.g., -n 20 for 20 lines).

Syntax:

head [options] filename  
tail [options] filename  

Examples:

  • Show the first 5 lines of data.csv:
    head -n 5 data.csv  
  • Show the last 3 lines of app.log:
    tail -n 3 app.log  
  • Live-stream a log file (update as new lines are added):
    tail -f /var/log/apache2/access.log  

4. File Permissions

Linux uses a permission system to control access to files/directories. Use these commands to modify permissions.

chmod: Change File Permissions

Purpose: Modify read (r), write (w), and execute (x) permissions for the owner, group, and others.

Permission Syntax:
Permissions are represented as 3 sets of 3 characters (e.g., rwxr-xr--):

  • First 3: Owner permissions.
  • Middle 3: Group permissions.
  • Last 3: Others permissions.

Numeric Shortcuts: Each permission has a value: r=4, w=2, x=1. Sum them for combinations (e.g., rwx=7, r-x=5).

Syntax:

chmod [permissions] filename  

Examples:

  • Give the owner read/write/execute, group read/execute, others read-only (rwxr-xr--):
    chmod 754 script.sh  
  • Add execute permission for everyone:
    chmod +x script.sh  

chown: Change File Owner/Group

Purpose: Transfer ownership of a file/directory to a user or group.

Syntax:

chown [user]:[group] filename  

Examples:

  • Change owner to john and group to developers for project.txt:
    chown john:developers project.txt  
  • Change only the group to admin:
    chown :admin report.pdf  

5. System Information

Monitor your system’s health and configuration with these commands.

uname: Print System Information

Purpose: Display kernel and system details.
Option: -a: Show all information (kernel version, hostname, architecture, etc.).

Syntax:

uname [options]  

Example:

uname -a  
# Output: Linux my-pc 5.4.0-91-generic #102-Ubuntu SMP Fri Nov 5 16:31:28 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux  

top/htop: Monitor Running Processes

Purpose: View real-time system processes, CPU/memory usage, and more.

  • top: Basic built-in tool.
  • htop: Improved version (color-coded, mouse support; install with sudo apt install htop).

Example:

htop  

df/du: Check Disk Space

  • df: Report free disk space on mounted filesystems.
    Option: -h: Human-readable format (GB, MB).
  • du: Estimate file/directory sizes.
    Option: -sh: Summarize size of a directory in human-readable format.

Examples:

  • Check free space on all drives:
    df -h  
  • Find the size of the Downloads folder:
    du -sh ~/Downloads  

6. Package Management

Install, update, and remove software packages (varies by Linux distribution).

apt (Debian/Ubuntu)

Common Commands:

  • Update package lists:
    sudo apt update  
  • Install a package (e.g., git):
    sudo apt install git  
  • Upgrade all packages:
    sudo apt upgrade  
  • Remove a package:
    sudo apt remove git  

yum/dnf (RHEL/CentOS/Fedora)

  • yum is legacy; dnf is the modern replacement (used in Fedora/RHEL 8+).

Common Commands:

  • Update package lists:
    sudo dnf check-update  
  • Install a package:
    sudo dnf install git  
  • Upgrade packages:
    sudo dnf upgrade  

7. Networking Commands

Troubleshoot network issues and manage connections.

ping: Test Network Connectivity

Purpose: Send ICMP packets to a host to check if it’s reachable.

Syntax:

ping [hostname/IP]  

Example:

ping google.com  

ip: Configure Network Interfaces

Purpose: Replace ifconfig (deprecated) to manage IP addresses, routes, and interfaces.

Common Subcommands:

  • Show all interfaces and IPs:
    ip addr show  
  • Bring an interface up/down:
    sudo ip link set eth0 up  
    sudo ip link set eth0 down  

curl/wget: Download Files

Purpose: Retrieve files from the internet via CLI.

  • curl: Transfer data (supports HTTP, FTP, etc.).
  • wget: Non-interactive downloader (great for background downloads).

Examples:

  • Download a file with curl:
    curl -O https://example.com/file.zip  
  • Download with wget:
    wget https://example.com/file.zip  

8. Process Management

Control running programs and system processes.

ps: List Running Processes

Purpose: Display active processes.
Common Options:

  • -aux: Show all processes (user, PID, CPU/memory usage).

Syntax:

ps [options]  

Example:

ps aux | grep "chrome"  # Find all Chrome processes  

kill: Terminate Processes

Purpose: Stop a process using its PID (Process ID).

Common Signals:

  • -15 (SIGTERM): Gracefully terminate (default).
  • -9 (SIGKILL): Force kill (use as a last resort).

Syntax:

kill [signal] PID  

Example:

  • Force kill a process with PID 1234:
    kill -9 1234  

bg/fg: Manage Background/Foreground Processes

Purpose: Control jobs running in the background.

  • Start a process in the background: Append & (e.g., sleep 100 &).
  • bg: Move a suspended process to the background.
  • fg: Bring a background process to the foreground.

Example:

  • Suspend a running process with Ctrl+Z, then send it to the background:
    sleep 100   # Press Ctrl+Z to suspend  
    bg          # Resume in background  
    fg          # Bring back to foreground  

9. User Management

Create and manage user accounts (requires sudo for admin actions).

useradd: Create a User

Purpose: Add a new user to the system.

Syntax:

sudo useradd [options] username  

Example:

  • Create a user jane with a home directory:
    sudo useradd -m jane  

passwd: Change User Password

Purpose: Update the password for a user (requires current password for non-sudo users).

Syntax:

passwd [username]  # Omit username to change your own password  

Example:

sudo passwd jane  # Admin changes Jane's password  

whoami: Check Current User

Purpose: Display the username of the current logged-in user.

Syntax:

whoami  

10. Advanced Tips: Aliases, Pipes, and Redirection

Boost productivity with these pro tricks:

  • Aliases: Create shortcuts for long commands. Add to ~/.bashrc to persist:

    alias ll='ls -la'  # Now "ll" runs "ls -la"  
  • Pipes (|): Combine commands by sending output of one to another:

    ls -la | grep ".txt"  # List only .txt files  
  • Redirection: Save output to a file or discard errors:

    echo "Hello" > output.txt  # Overwrite file  
    echo "More" >> output.txt  # Append to file  
    command 2> errors.log      # Send errors to a log  

11. Conclusion

Mastering these essential Linux commands is the first step to becoming proficient with the CLI. Start small—practice navigating directories, creating files, and checking system info. Over time, explore advanced topics like scripting and automation to unlock the full power of Linux.

12. Reference