Where distros differ, each one gets its own command. Alpine examples assume you're root, because Alpine doesn't include sudo by default, and some options differ on Alpine's built-in tools.

Tools for this guide: chmod calculator, Cron expression builder.

Files and folders #

  • List files, including hidden ones, with sizesLinux, macOS
    ls -lah
  • Show the current folderLinux, macOS
    pwd
  • Go to your home folderLinux, macOS
    cd ~
  • Create a folder and any missing parent foldersLinux, macOS
    mkdir -p projects/site/assets
  • Copy a folder and everything in itLinux, macOS
    cp -r source/ backup/
  • Move or rename a fileLinux, macOS
    mv old-name.txt new-name.txt
  • Delete a fileLinux, macOS
    rm file.txt
  • Delete a folder and everything in itLinux, macOS
    rm -rf folder/

    Warning: Deletes without asking and can't be undone. Check the path before you run it.

  • Create a shortcut (symbolic link) to a fileLinux, macOS
    ln -s /path/to/target link-name
  • Show a file's permissions, size and timestampsLinux, macOS
    stat file.txt
  • Find files by nameLinux, macOS
    find . -name "*.log"
  • Find files changed in the last dayLinux, macOS
    find . -type f -mtime -1
  • Find files larger than 100 MBLinux, macOS
    find / -type f -size +100M 2>/dev/null

Text #

  • Show a file's contentsLinux, macOS
    cat file.txt
  • Scroll through a long fileLinux, macOS
    less file.txt
  • Show the first 20 lines of a fileLinux, macOS
    head -n 20 file.txt
  • Show the last 50 lines of a fileLinux, macOS
    tail -n 50 file.txt
  • Search for text in a folderLinux, macOS
    grep -rn "error" .
  • Search for text, ignoring caseLinux, macOS
    grep -i "error" app.log
  • Show lines that don't contain some textLinux, macOS
    grep -v "debug" app.log
  • Replace text in a fileLinux
    sed -i 's/old/new/g' file.txt

    Warning: Changes the file straight away, with no backup.

  • Count lines in a fileLinux, macOS
    wc -l file.txt
  • Sort lines and remove duplicatesLinux, macOS
    sort file.txt | uniq
  • Count how often each line appearsLinux, macOS
    sort file.txt | uniq -c | sort -rn
  • Print the first column of each lineLinux, macOS
    awk '{print $1}' access.log
  • Compare two filesLinux, macOS
    diff -u old.txt new.txt
  • Edit a file in the terminalLinux
    nano file.txt

Permissions #

  • Make a script runnableLinux, macOS
    chmod +x script.sh
  • Let only the owner read and write a fileLinux, macOS
    chmod 600 secrets.env
  • Set standard folder permissionsLinux, macOS
    chmod 755 folder/
  • Change a file's owner and groupLinux, macOS
    sudo chown alice:alice file.txt
  • Change the owner of a folder and everything in itLinux, macOS
    sudo chown -R alice:alice folder/
  • Show your user ID and groupsLinux, macOS
    id
  • Open a shell as the root userLinux, macOS
    sudo -i

Users and groups #

  • Show which user you areLinux, macOS
    whoami
  • Show who is logged inLinux, macOS
    who
  • Create a user with a home folder
    Debian, Ubuntu
    sudo adduser alice
    Red Hat, Fedora
    sudo useradd -m alice && sudo passwd alice
    Alpine
    adduser alice
  • Change a user's passwordLinux
    sudo passwd alice
  • Give a user admin rights
    Debian, Ubuntu
    sudo usermod -aG sudo alice
    Red Hat, Fedora
    sudo usermod -aG wheel alice
    Alpine
    addgroup alice wheel
  • Add a user to a group
    Debian, Ubuntu, Red Hat, Fedora
    sudo usermod -aG docker alice
    Alpine
    addgroup alice docker
  • Show a user's groupsLinux, macOS
    groups alice
  • Delete a user and their home folder
    Debian, Ubuntu
    sudo deluser --remove-home alice
    Red Hat, Fedora
    sudo userdel -r alice
    Alpine
    deluser --remove-home alice

    Warning: Deletes the user's files and can't be undone.

Packages #

  • Refresh the list of available packages
    Debian, Ubuntu
    sudo apt update
    Red Hat, Fedora
    sudo dnf check-update
    Alpine
    apk update
  • Install a package
    Debian, Ubuntu
    sudo apt install nginx
    Red Hat, Fedora
    sudo dnf install nginx
    Alpine
    apk add nginx
  • Update all installed packages
    Debian, Ubuntu
    sudo apt update && sudo apt upgrade
    Red Hat, Fedora
    sudo dnf upgrade
    Alpine
    apk update && apk upgrade
  • Remove a package
    Debian, Ubuntu
    sudo apt remove nginx
    Red Hat, Fedora
    sudo dnf remove nginx
    Alpine
    apk del nginx
  • Search for a package
    Debian, Ubuntu
    apt search nginx
    Red Hat, Fedora
    dnf search nginx
    Alpine
    apk search nginx
  • List installed packages
    Debian, Ubuntu
    apt list --installed
    Red Hat, Fedora
    dnf list --installed
    Alpine
    apk info
  • Find which package a file came from
    Debian, Ubuntu
    dpkg -S /usr/bin/curl
    Red Hat, Fedora
    dnf provides /usr/bin/curl
    Alpine
    apk info --who-owns /usr/bin/curl
  • Remove packages that are no longer needed
    Debian, Ubuntu
    sudo apt autoremove
    Red Hat, Fedora
    sudo dnf autoremove

Services #

  • Check a service's status
    Debian, Ubuntu, Red Hat, Fedora
    systemctl status nginx
    Alpine
    rc-service nginx status
  • Start a service
    Debian, Ubuntu, Red Hat, Fedora
    sudo systemctl start nginx
    Alpine
    rc-service nginx start
  • Stop a service
    Debian, Ubuntu, Red Hat, Fedora
    sudo systemctl stop nginx
    Alpine
    rc-service nginx stop
  • Restart a service
    Debian, Ubuntu, Red Hat, Fedora
    sudo systemctl restart nginx
    Alpine
    rc-service nginx restart
  • Start a service now and at every boot
    Debian, Ubuntu, Red Hat, Fedora
    sudo systemctl enable --now nginx
    Alpine
    rc-update add nginx default && rc-service nginx start
  • Stop a service starting at boot
    Debian, Ubuntu, Red Hat, Fedora
    sudo systemctl disable nginx
    Alpine
    rc-update del nginx default
  • List running services
    Debian, Ubuntu, Red Hat, Fedora
    systemctl list-units --type=service --state=running
    Alpine
    rc-status
  • Reload service files after editing themDebian, Ubuntu, Red Hat, Fedora
    sudo systemctl daemon-reload

Processes #

  • Show all running processesLinux, macOS
    ps aux
  • Find a process by nameLinux
    pgrep -a nginx
  • Watch CPU and memory use liveLinux, macOS
    top
  • Stop a process by IDLinux, macOS
    kill 1234
  • Force a process to stopLinux, macOS
    kill -9 1234

    Warning: The process can't clean up, so unsaved data may be lost.

  • Stop every process with a nameLinux, macOS
    pkill nginx
  • Keep a command running after you log outLinux, macOS
    nohup ./script.sh &
  • Show what is using a portLinux, macOS
    sudo lsof -i :443

Logs #

  • Follow a log file as it growsLinux, macOS
    tail -f app.log
  • Read a service's recent logsDebian, Ubuntu, Red Hat, Fedora
    journalctl -u nginx -n 50
  • Follow a service's logs liveDebian, Ubuntu, Red Hat, Fedora
    journalctl -u nginx -f
  • Show logs since the last bootDebian, Ubuntu, Red Hat, Fedora
    journalctl -b
  • Show errors since the last bootDebian, Ubuntu, Red Hat, Fedora
    journalctl -p err -b
  • Read the system logAlpine
    tail -n 100 /var/log/messages
  • Show recent kernel messagesLinux
    sudo dmesg | tail -n 50

System #

  • Show which distro and version is installedLinux
    cat /etc/os-release
  • Show the kernel versionLinux, macOS
    uname -r
  • Show how long the system has been runningLinux, macOS
    uptime
  • Show memory useLinux
    free -h
  • Show CPU detailsLinux
    lscpu
  • Show the hostnameLinux, macOS
    hostname
  • Change the hostnameDebian, Ubuntu, Red Hat, Fedora
    sudo hostnamectl set-hostname web01
  • Show the date, time and time zoneDebian, Ubuntu, Red Hat, Fedora
    timedatectl
  • Set the time zoneDebian, Ubuntu, Red Hat, Fedora
    sudo timedatectl set-timezone Europe/Dublin
  • Restart the systemLinux
    sudo reboot

    Warning: Disconnects everyone and stops running services.

Storage #

  • Check free disk spaceLinux, macOS
    df -h
  • See what is using disk spaceLinux, macOS
    du -sh * | sort -h
  • Find the biggest folders on the systemLinux, macOS
    sudo du -h -d 1 / 2>/dev/null | sort -h
  • List disks and partitionsLinux
    lsblk
  • Show disk IDs and filesystem typesLinux
    sudo blkid
  • Mount a partitionLinux
    sudo mount /dev/sdb1 /mnt/data
  • Unmount a partitionLinux
    sudo umount /mnt/data
  • List mounted filesystemsLinux
    findmnt
  • Format a partition as ext4Linux
    sudo mkfs.ext4 /dev/sdb1

    Warning: Erases everything on the partition. Check the device name with lsblk first.

Network #

  • Show this machine's IP addressesLinux
    ip -br addr
  • Show the routing tableLinux
    ip route
  • List listening portsLinux
    sudo ss -tulpn
  • Check whether a host is reachableLinux, macOS
    ping -c 4 example.com
  • Trace the route to a hostLinux, macOS
    traceroute example.com
  • Look up a domain's IP addressLinux, macOS
    dig +short example.com
  • Look up a domain's mail serversLinux, macOS
    dig +short MX example.com
  • Show the DNS resolver settingsLinux
    cat /etc/resolv.conf
  • See only the response headers from a siteLinux, macOS
    curl -I https://example.com
  • Download a fileLinux, macOS
    curl -LO https://example.com/file.tar.gz
  • Test whether a port is openLinux, macOS
    nc -zv example.com 443
  • Show your public IP addressLinux, macOS
    curl -s https://ifconfig.me
  • Show network connectionsRed Hat, Fedora
    nmcli connection show
  • Restart networkingAlpine
    rc-service networking restart

Firewall #

  • Show the firewall rules
    Debian, Ubuntu
    sudo ufw status verbose
    Red Hat, Fedora
    sudo firewall-cmd --list-all
  • Allow SSH through the firewall
    Debian, Ubuntu
    sudo ufw allow OpenSSH
    Red Hat, Fedora
    sudo firewall-cmd --permanent --add-service=ssh && sudo firewall-cmd --reload
  • Allow a port
    Debian, Ubuntu
    sudo ufw allow 443/tcp
    Red Hat, Fedora
    sudo firewall-cmd --permanent --add-port=443/tcp && sudo firewall-cmd --reload
  • Remove a port rule
    Debian, Ubuntu
    sudo ufw delete allow 443/tcp
    Red Hat, Fedora
    sudo firewall-cmd --permanent --remove-port=443/tcp && sudo firewall-cmd --reload
  • Turn on the firewall
    Debian, Ubuntu
    sudo ufw enable
    Red Hat, Fedora
    sudo systemctl enable --now firewalld

    Warning: Allow SSH first if you're connected remotely, or you'll lock yourself out.

SSH #

  • Connect to a serverLinux, macOS
    ssh user@host
  • Connect on a custom portLinux, macOS
    ssh -p 2222 user@host
  • Run one command on a serverLinux, macOS
    ssh user@host 'uptime'
  • Create a new SSH keyLinux, macOS
    ssh-keygen -t ed25519 -C "label"
  • Copy your key to a serverLinux, macOS
    ssh-copy-id user@host
  • Copy a file to a serverLinux, macOS
    scp file.txt user@host:/tmp/
  • Copy a file from a serverLinux, macOS
    scp user@host:/var/log/app.log .
  • Sync a folder to a serverLinux, macOS
    rsync -avz dist/ user@host:/var/www/site/
  • Reach a server's port through SSHLinux, macOS
    ssh -L 8080:localhost:80 user@host
  • Check the SSH server config for errorsLinux
    sudo sshd -t
  • Restart the SSH server
    Debian, Ubuntu
    sudo systemctl restart ssh
    Red Hat, Fedora
    sudo systemctl restart sshd
    Alpine
    rc-service sshd restart

Archives #

  • Compress a folderLinux, macOS
    tar -czf backup.tar.gz folder/
  • Extract a .tar.gz archiveLinux, macOS
    tar -xzf backup.tar.gz
  • Extract a .tar.gz archive into a folderLinux, macOS
    tar -xzf backup.tar.gz -C /path/to/folder
  • List what is inside a .tar.gz archiveLinux, macOS
    tar -tzf backup.tar.gz
  • Create a zip fileLinux, macOS
    zip -r archive.zip folder/
  • Extract a zip fileLinux, macOS
    unzip archive.zip

Scheduled tasks #

  • Edit your scheduled tasksLinux, macOS
    crontab -e
  • List your scheduled tasksLinux, macOS
    crontab -l
  • Run a script every day at 2am (add this line with crontab -e)Linux, macOS
    0 2 * * * /home/alice/backup.sh
  • List scheduled systemd timersDebian, Ubuntu, Red Hat, Fedora
    systemctl list-timers