TwiceToldby Develoker
ConceptsSystem designLabsCheat sheetsAbout

Develoker makes free tools for learning to build software. TwiceTold explains each idea twice, as an everyday analogy and the real thing; CloudArch lets you design cloud systems and put load on them.

About · Labs · Cheat sheets · Learning paths

← All cheat sheets · 🪟 Windows shell

🐧 Linux shell

The commands you reach for daily on Linux servers and in WSL: files, text, search, processes, disks, networking, packages and services.

80 commands in 12 sections

Move around and manage filesRead and compare textSearchSlice and transform textPermissions and usersProcesses and jobsDisks and systemNetworkRemote machinesArchivesPackages and servicesShell tricks

Move around and manage files

  • pwd

    Print the directory you are in

  • ls -lah

    List files with sizes, permissions and hidden files

    e.g. ls -lah /var/log

  • cd -

    Jump back to the previous directory

    cd with no argument goes home (~).

  • mkdir -p a/b/c

    Create a directory and any missing parents

  • cp -r src dest

    Copy a file or a whole directory

    e.g. cp -r site/ site-backup/

  • mv old new

    Move or rename a file or directory

  • rm -r dir

    Delete a directory and everything in it

    There is no recycle bin. Add -i to confirm each file.

  • touch file.txt

    Create an empty file, or update its timestamp

  • ln -s target link

    Make a symbolic link

    e.g. ln -s /opt/app/current app

  • tree -L 2

    Show a directory tree two levels deep

    Install with your package manager if missing.

Read and compare text

  • cat file

    Print a whole file

  • less file

    Page through a file: / searches, q quits

  • head -n 20 file

    Show the first 20 lines

  • tail -f app.log

    Follow a log as it grows

    e.g. tail -n 100 -f /var/log/syslog

  • wc -l file

    Count lines

  • diff -u old new

    Show the differences between two files

  • nano file

    Edit a file in a simple terminal editor

    In vim: i to type, Esc then :wq to save and quit.

Search

  • grep -rn 'TODO' src/

    Find text in files, with file names and line numbers

    e.g. grep -rni 'timeout' /etc/nginx

  • find . -name '*.log'

    Find files by name under a directory

  • find . -type f -mtime +7

    Find files changed more than 7 days ago

  • find . -type f -size +100M

    Find files bigger than 100 MB

  • which python3

    Show which program a command runs

  • history | grep ssh

    Search the commands you ran before

    Ctrl+R searches history as you type.

Slice and transform text

  • sort file | uniq -c | sort -rn

    Count repeated lines, most frequent first

    e.g. cut -d' ' -f1 access.log | sort | uniq -c | sort -rn | head

  • cut -d: -f1 /etc/passwd

    Take one field from each line, split on a delimiter

  • awk '{print $1}' file

    Print the first column of each line

    e.g. awk -F, '{print $2}' data.csv

  • sed -i 's/old/new/g' file

    Replace text in a file in place

    On macOS use sed -i '' (BSD sed).

  • tr 'a-z' 'A-Z'

    Translate characters, here to upper case

    e.g. echo hello | tr 'a-z' 'A-Z'

  • xargs

    Turn input lines into arguments for another command

    e.g. find . -name '*.tmp' -print0 | xargs -0 rm

  • jq '.items[].name' data.json

    Query JSON

    Install jq with your package manager.

Permissions and users

  • chmod +x script.sh

    Make a file executable

  • chmod 600 ~/.ssh/id_ed25519

    Owner can read and write; nobody else can do anything

    SSH refuses private keys that others can read.

  • chown -R user:group dir

    Change who owns files

  • sudo command

    Run one command as root

    e.g. sudo !!

    sudo !! reruns your last command with sudo.

  • whoami

    Print your user name

  • id

    Show your user id and groups

Processes and jobs

  • ps aux

    List every running process

    e.g. ps aux | grep '[n]ginx'

  • top

    Live view of CPU and memory use

    htop is friendlier if installed. q quits.

  • kill PID

    Ask a process to stop (SIGTERM)

  • kill -9 PID

    Force a process to stop (SIGKILL)

    Last resort: the process gets no chance to clean up.

  • pgrep -f name

    Find process ids by name or command line

    also pkill -f name

  • command &

    Run a command in the background

  • nohup command > out.log 2>&1 &

    Keep a command running after you log out

  • jobs

    List this shell's background jobs

    e.g. fg %1

    Ctrl+Z pauses the foreground job; bg resumes it in the background.

Disks and system

  • df -h

    Free space on each mounted disk

  • du -sh * | sort -h

    Size of each item here, biggest last

  • free -h

    Memory and swap in use

  • lsblk

    List disks and partitions

  • uname -a

    Kernel and architecture

  • cat /etc/os-release

    Which distribution and version this is

  • uptime

    How long the machine has run, and its load averages

Network

  • curl -I https://example.com

    Fetch only the response headers

  • curl -X POST -H 'Content-Type: application/json' -d '{"a":1}' URL

    Send JSON to an API

  • curl -LO URL

    Download a file, following redirects

    also wget URL

  • ss -tulpn

    Which ports are listening, and by which process

    Needs sudo to see other users’ processes.

  • lsof -i :8080

    Which process is using port 8080

  • ping -c 4 host

    Check a host answers, four times

  • dig +short example.com

    Look up a DNS record

    also nslookup example.com

  • ip addr

    Show network interfaces and IP addresses

Remote machines

  • ssh user@host

    Open a shell on another machine

    e.g. ssh -i ~/.ssh/key.pem ubuntu@203.0.113.10

  • scp file user@host:/path/

    Copy a file to another machine

    e.g. scp user@host:/var/log/app.log .

  • rsync -avz src/ user@host:/srv/app/

    Sync a directory, sending only what changed

    A trailing / on src copies its contents, not the folder itself.

  • ssh-keygen -t ed25519

    Create an SSH key pair

    also ssh-copy-id user@host

Archives

  • tar -czf site.tar.gz site/

    Pack a directory into a .tar.gz

  • tar -xzf site.tar.gz -C /tmp

    Unpack a .tar.gz into a directory

  • tar -tzf site.tar.gz

    List what is inside a .tar.gz

  • zip -r site.zip site/

    Pack a directory into a .zip

    also unzip site.zip

Packages and services

  • sudo apt update && sudo apt install -y nginx

    Install a package on Debian or Ubuntu

    also sudo dnf install -y nginx

  • sudo systemctl status nginx

    Is a service running, and its latest log lines

  • sudo systemctl restart nginx

    Restart a service

  • sudo systemctl enable --now nginx

    Start a service now and on every boot

  • journalctl -u nginx -f

    Follow a service's logs

    e.g. journalctl -u nginx --since "1 hour ago"

  • crontab -e

    Edit your scheduled jobs

    e.g. 0 3 * * * /usr/local/bin/backup.sh

    Fields: minute hour day-of-month month day-of-week.

Shell tricks

  • command > out.txt

    Send output to a file, replacing it

    also command >> out.txt

  • command 2>&1 | tee run.log

    Show output and errors and save them to a file

  • $(command)

    Use a command's output inside another

    e.g. echo "Today is $(date +%F)"

  • export NAME=value

    Set an environment variable for this shell and its programs

    e.g. echo $PATH

  • alias ll='ls -lah'

    Make a shortcut

    Put it in ~/.bashrc to keep it.

  • command --help

    Quick help for most commands

    also man command

  • watch -n 2 'df -h'

    Rerun a command every 2 seconds