Linux commands worth knowing by heart

Forty commands, twenty of them tagged “basics” — enough to get by on any machine.

Bookmarks

  1. Copy

    5/5

    cp -r for a whole directory; cp -a keeps the permissions and dates too.

  2. Connect to a machine

    5/5

    ssh user@host, with -i to point at a key that is not the default one.

  3. Move or rename

    5/5

    mv does both: renaming is moving to a different name.

  4. Follow a log live

    5/5

    tail -f file.log prints the lines as they arrive.

  5. Delete

    5/5

    rm -r for a directory. There is no trash here: what goes does not come back.

  6. List a directory

    5/5

    ls -lh for readable sizes, ls -la to see the hidden files as well.

  7. Drive a service

    5/5

    systemctl status, start, restart. The status also says why it failed.

  8. Print a file

    5/5

    cat for the short ones. Past twenty lines, less does a far better job.

  9. Read a big file

    5/5

    less keeps you in charge of the scrolling, and slash searches inside it.

  10. Search inside files

    5/5

    grep -rn pattern . gives the file and the line, which plain grep does not.

  11. Find a file

    5/5

    find . -name access.log -mtime -7: by name, and touched in the last seven days.

  12. Read the manual

    5/5

    man command, and above all slash pattern to jump to the option you came for.

  13. Run something as root

    5/5

    sudo for one command, sudo -i for a whole shell when there are ten to get through.

  14. Change directory

    5/5

    cd with no argument goes home, cd - goes back to where you just were.

  15. See the processes

    5/5

    ps aux for a snapshot, then a pipe into grep to pick out your own.

  16. Stop a process

    5/5

    kill asks politely, kill -9 does not ask at all. Try the polite one first.

  17. Know where you are

    5/5

    pwd prints the absolute path, and heads off half the mistakes waiting to happen.

  18. Create a directory

    5/5

    mkdir -p a/b/c builds the whole chain at once, and says nothing if it exists.

  19. Find a command you ran before

    4/5

    history | grep pattern, or Ctrl+R, which searches as you type.

  20. Change permissions

    4/5

    chmod 644 for a file, 755 for a directory or an executable.

  21. Measure free space

    4/5

    df -h, filesystem by filesystem. First command to run when a write fails.

  22. Find what is filling the disk

    4/5

    du -sh * in a directory, then walk down into the biggest one. Step by step.

  23. Archive

    4/5

    tar -czf archive.tar.gz directory to build one, -xzf to unpack it.

  24. See the first or last lines

    4/5

    head -n 20 and tail -n 50. On an unknown file, head shows its shape at once.

  25. Replace inside a file

    4/5

    sed -i s/old/new/g file. Without -i, the result only reaches the screen.

  26. Read the system logs

    4/5

    journalctl -u service -f follows one service rather than the whole machine.

  27. Watch things live

    4/5

    htop sorts by CPU or memory, and kills a process without you leaving it.

  28. Copy to another machine

    4/5

    rsync -av source/ server:/target/ resumes where it stopped, which scp cannot.

  29. Call a URL

    4/5

    curl -i shows the response headers, which settles half of the guesswork.

  30. See what is listening on a port

    4/5

    ss -ltnp lists the listening ports with the process behind each one.

  31. Find out who holds a file

    3/5

    lsof file names the process that is stopping you unmounting or deleting it.

  32. Schedule a job

    3/5

    crontab -e to edit, -l to read back. Five fields: minute, hour, day, month, weekday.

  33. Create a symbolic link

    3/5

    ln -s target link. The target is worth giving as an absolute path.

  34. Compare two files

    3/5

    diff -u old new gives the format every other tool knows how to read.

  35. Pull out a column

    3/5

    awk with {print $2} takes the second field, however wide the spacing is.

  36. Act on every result

    3/5

    find … | xargs -n1 command runs the command once per result.

  37. Sort and deduplicate

    3/5

    sort | uniq -c counts the occurrences; uniq only ever works on sorted input.

  38. Check an address

    3/5

    ip a replaces ifconfig; ip r shows which way the packets leave.

  39. Test a machine

    3/5

    ping says whether it answers, not whether the service runs — two questions.

  40. Repeat a command

    3/5

    watch -n 2 command reruns it every two seconds, in the same spot on screen.