Linux Command Cheat Sheet

Essential Linux Commands Every User Should Know

1. Navigation & File Management

  • pwd – Prints the current working directory.
  • ls – Lists files and directories in the current location.
    • ls -l → Long format (details like permissions, owner, size).
    • ls -a → Shows hidden files.
  • cd <directory> – Changes the current directory.
    • cd .. → Moves up one directory level.
    • cd / → Moves to the root directory.
    • cd ~ → Moves to the home directory.
  • mkdir <directory> – Creates a new directory.
  • rmdir <directory> – Removes an empty directory.
  • rm <file> – Deletes a file.
    • rm -r <directory> → Deletes a directory and its contents.

2. File Operations

  • cp <source> <destination> – Copies a file or directory.
    • cp -r <dir1> <dir2> → Recursively copies a directory.
  • mv <source> <destination> – Moves or renames a file/directory.
  • touch <file> – Creates an empty file or updates a file’s timestamp.
  • cat <file> – Displays the contents of a file.
  • head <file> – Shows the first 10 lines of a file.
    • head -n 20 <file> → Shows the first 20 lines.
  • tail <file> – Shows the last 10 lines of a file.
    • tail -f <file> → Continuously displays updates to the file (useful for log files).

3. User & Permissions

  • whoami – Displays the current logged-in user.
  • id – Shows user ID (UID) and group ID (GID).
  • chmod <permissions> <file> – Changes file permissions.
    • chmod 755 <file> → Read & execute for everyone, write for owner.
    • chmod +x <file> → Adds execute permission.
  • chown <user>:<group> <file> – Changes file ownership.
  • sudo <command> – Runs a command as a superuser.

4. Process Management

  • ps – Lists currently running processes.
    • ps aux → Shows all system processes.
  • top – Displays real-time CPU/memory usage.
  • htop – Interactive process viewer (if installed).
  • kill <PID> – Terminates a process by ID.
  • killall <process> – Kills all processes by name.
  • pkill <name> – Kills processes by matching name.

5. Disk & System Info

  • df -h – Shows disk space usage in human-readable format.
  • du -sh <directory> – Displays the total size of a directory.
  • free -h – Shows memory usage.
  • uptime – Displays how long the system has been running.
  • uname -a – Displays system information.

6. Networking

  • ip a – Displays network interfaces and IP addresses.
  • ping <host> – Tests network connectivity to a server.
  • wget <URL> – Downloads a file from a URL.
  • curl <URL> – Fetches a web page’s contents.
  • netstat -tulnp – Lists open ports and listening services.

7. Searching & Text Processing

  • grep "<pattern>" <file> – Searches for a pattern in a file.
    • grep -i → Case-insensitive search.
    • grep -r "<pattern>" <directory> → Recursively searches in a directory.
  • find <directory> -name "<filename>" – Finds files by name.
  • awk '{print $1}' <file> – Extracts the first column from a file.
  • sed 's/old/new/g' <file> – Replaces text in a file.

8. Package Management

  • Debian-based (Ubuntu, Debian):
    • apt update → Updates package lists.
    • apt upgrade → Upgrades all installed packages.
    • apt install <package> → Installs a package.
    • apt remove <package> → Uninstalls a package.
  • Red Hat-based (CentOS, Fedora):
    • dnf install <package> → Installs a package.
    • dnf remove <package> → Uninstalls a package.

9. Archiving & Compression

  • tar -cvf archive.tar <directory> – Creates a tar archive.
  • tar -xvf archive.tar – Extracts a tar archive.
  • tar -czvf archive.tar.gz <directory> – Creates a compressed tarball.
  • tar -xzvf archive.tar.gz – Extracts a compressed tarball.
  • zip -r archive.zip <directory> – Compresses a directory into a ZIP file.
  • unzip archive.zip – Extracts a ZIP file.

10. System Control

  • shutdown -h now – Shuts down the system immediately.
  • reboot – Reboots the system.
  • systemctl status <service> – Checks the status of a service.
  • systemctl start <service> – Starts a service.
  • systemctl stop <service> – Stops a service.
  • systemctl restart <service> – Restarts a service.