cluster deboistech.in Get started
Cheatsheet

Linux Fundamentals

The shell commands you reach for every day: moving around, files, text, permissions, processes and pipes.

Practice this path

Move around

  • pwd

    Print the directory you are in

  • ls -lah

    List everything, with sizes and permissions, in a readable form

  • cd ~  |  cd -  |  cd ..

    Go home, go back to the previous directory, go up one level

  • find . -name "*.log" -mtime -1

    Find files named *.log changed in the last day

  • which python3  |  type ls

    Show which program a command name runs

  • man tar  |  tar --help

    Read the manual page, or the short help

  • history | grep ssh

    Search the commands you typed before (Ctrl+R searches as you type)

Files and folders

  • mkdir -p app/logs/archive

    Create a folder and any missing parents

  • touch notes.txt

    Create an empty file, or update its time

  • cp -r src/ backup/

    Copy a folder with everything in it

  • mv old.txt new.txt

    Rename or move a file

  • rm -r build/

    Delete a folder and its contents (there is no trash can)

  • ln -s /var/log/app app-logs

    Make a symbolic link, a shortcut to another path

  • stat notes.txt

    Show size, owner, permissions and all the timestamps

  • du -sh * | sort -h

    Size of each item here, smallest to largest

  • df -h

    Free and used space on each disk

Read and search text

  • less +F app.log

    Page through a file; press Ctrl+C then F to follow it like tail -f

  • head -n 20 file  |  tail -n 20 file

    First or last 20 lines

  • tail -f /var/log/syslog

    Follow a log as it grows

  • grep -rn "error" /var/log/app/

    Search a folder recursively and show line numbers

  • grep -v "debug" app.log

    Show the lines that do NOT match

  • wc -l file

    Count lines

  • cut -d: -f1 /etc/passwd

    Take the first field of each line, split on ":"

  • awk '{print $1, $NF}' file

    Print the first and the last column of each line

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

    Replace every old with new (add -i to edit the file in place)

  • sort data.txt | uniq -c | sort -nr

    Count how often each line occurs, most frequent first

  • diff -u a.conf b.conf

    Show what differs between two files

Pipes and redirection

The output of one command can feed the next, or go to a file.

  • cmd > out.txt  |  cmd >> out.txt

    Write the output to a file, replacing it or appending to it

  • cmd 2> err.txt

    Send only the error messages to a file

  • cmd > all.txt 2>&1

    Send normal output and errors to the same file

  • ls | wc -l

    Pipe: feed the output of ls to wc

  • cmd | tee out.txt

    Show the output and also save it

  • find . -name "*.tmp" | xargs rm

    Turn lines of input into arguments of another command

  • cmd1 && cmd2  |  cmd1 || cmd2

    Run cmd2 only if cmd1 succeeded, or only if it failed

  • echo "Today is $(date +%F)"

    Put the output of a command inside another

Permissions and ownership

r=4, w=2, x=1. Three groups: owner, group, others.

  • chmod 640 file

    Owner read+write, group read, others nothing

  • chmod u+x script.sh

    Let the owner execute the file

  • chmod -R g+w shared/

    Give the group write access to a whole folder

  • chown alice:devs file

    Change owner and group

  • chmod g+s shared/

    setgid on a folder: new files inherit its group

  • chmod +t /srv/dropbox

    Sticky bit: only a file owner can delete their files in the folder

  • umask 027

    New files get 640 and new folders 750 by default

  • sudo -l

    List what you are allowed to run with sudo

Processes and services

  • ps aux | grep nginx

    Find a running process

  • top  |  htop

    Live view of CPU and memory use

  • kill PID  |  kill -9 PID

    Ask a process to stop, or force it

  • pkill -f "python app.py"

    Stop processes by matching their command line

  • sleep 300 &  then  jobs, fg, bg

    Run in the background and manage shell jobs

  • nohup ./job.sh > job.log 2>&1 &

    Keep a job running after you log out

  • systemctl status nginx

    See if a service is running (also start, stop, restart, enable)

  • journalctl -u nginx -f

    Follow a service's logs

  • free -h  |  uptime

    Memory in use, and the load average

Archives and the network

  • tar -czf backup.tar.gz folder/

    Make a compressed archive

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

    Unpack an archive into a folder

  • curl -I https://example.com

    Fetch only the response headers

  • curl -s https://example.com | head

    Fetch a page quietly and look at the start

  • ss -tulpn

    List listening TCP/UDP ports and the programs that own them

  • ping -c 3 example.com

    Check that a host answers

Cheatsheet for the Linux Fundamentals path · Deboistech · All cheatsheets