Linux command line

Level recap · Linux command line · Level 2

Search, pipes and permissions

Everything this level taught, class by class: the idea, the code and what it shows. Read it before the exam, or keep it as a PDF.

What this level teaches: Find the lines that matter, count them, join commands with pipes and set who may do what.

10 classesLevel exam

Take the level exam
  1. 1Find the lines

    grep.

    The idea

    A server log can have thousands of lines. grep word file prints only the lines that contain the word. grep -i word file ignores capitals, so it finds Error, ERROR and error.

    Save what it finds the usual way: grep -i error app.log > errors.txt.

    The code

    grep -i error app.log > errors.txt

    What it shows

    PASS errors.txt has every error line
  2. 2Count it

    wc -l and grep -c.

    The idea

    Your boss doesn't want the lines, just a number. wc -l file counts the lines in a file. grep -c word file counts the lines that have the word.

    These are real answers worked out from the file, so they stay right when the file changes.

    The code

    cat visits.log | wc -l > total.txt
    grep -c /cart visits.log > cart.txt

    What it shows

    PASS total.txt has the number of visits
  3. 3Pipes

    Join commands with |.

    The idea

    The | sign is a pipe. It sends what one command prints straight into the next one. cat names.txt | sort puts the names in A to Z order.

    uniq drops a line when it's the same as the line just above it, so put sort first: same lines end up next to each other. cat names.txt | sort | uniq.

    The code

    cat names.txt | sort | uniq > list.txt

    What it shows

    PASS list.txt has each name once, in order
  4. 4Add to the end

    >> keeps what's there.

    The idea

    > replaces a file. >> adds to the end of it and keeps what was there. That's how logs grow: each new line goes at the bottom.

    So one file can collect results from many commands: the first one with >, the rest with >>.

    The code

    grep error web.log > all-errors.txt
    grep error mail.log >> all-errors.txt

    What it shows

    PASS all-errors.txt has web errors, then mail errors
  5. 5Top three

    sort, uniq -c, sort -rn, head.

    The idea

    Here's a pipe the whole team uses: who shows up the most in a log? uniq -c counts each run of the same line. sort -rn puts the biggest numbers first. head -n 3 keeps the top three.

    Read it left to right: take the names, sort them, count them, put the biggest first, keep three.

    The code

    sort pages.log | uniq -c | sort -rn | head -n 3 > top.txt

    What it shows

    PASS top.txt has the 3 busiest pages
  6. 6Find files

    find -name.

    The idea

    ls only looks in one folder. find folder -name "*.log" looks in that folder and every folder inside it, all the way down. . means the folder you're in.

    Put the name in quotes, so the shell doesn't use the * too early.

    The code

    find app -name "*.log" > logs.txt

    What it shows

    PASS logs.txt lists every .log file
  7. 7Who can do what

    ls -l and chmod.

    The idea

    Every file says who may read it (r), change it (w) and run it (x), three times: for its owner, its group and everyone else. ls -l shows it, like rw-r--r--.

    chmod +x run.sh lets the script run. Numbers work too: each group is read 4 + write 2 + run 1, so chmod 600 key.txt means only the owner can read and change it, and nobody else can even look.

    The code

    ls -l
    chmod +x backup.sh
    chmod 600 password.txt

    What it shows

    PASS backup.sh can run
  8. 8Ask for admin

    sudo.

    The idea

    Some files belong to root, the computer's admin. Try to change them and you get "Permission denied". That's on purpose: it stops mistakes.

    When the job really needs it, put sudo in front of the command. It runs that one command as root. Use it only when you have to, and never with a command you don't understand.

    The code

    cp /etc/shop.conf /etc/shop.conf.bak
    sudo cp /etc/shop.conf /etc/shop.conf.bak
    sudo rm /var/log/shop-old.log

    What it shows

    PASS shop.conf is backed up
  9. 9Take it to a real machine

    Your own Linux terminal, for free.

    The idea

    Everything in this course runs on a practice server in your browser. The same commands work on a real Linux machine, and you can have one on your own computer for free.

    On Windows 10 or 11, open PowerShell as administrator, type wsl --install and restart. Then open Ubuntu from the Start menu: it is a real Linux terminal that runs next to Windows. On a Mac, open Terminal. It isn't Linux, but ls, cd, cat, grep, chmod and the rest of this course work the same way.

    The code

    mkdir practice
    cd practice
    whoami > me.txt
    pwd >> me.txt

    What it shows

    PASS practice/me.txt says who and where you are
  10. 10On your own: the morning report

    Search, count and save.

    The idea

    Every morning the team wants a short report from the night's log. Nobody wrote the commands for you this time. Plan it: what do you search for, what do you count, and how do you put it all in one file?

    Make report.txt from night.log: first line, the number of lines with ERROR; then every ERROR line under it.

    The code

    grep -c ERROR night.log > report.txt
    grep ERROR night.log >> report.txt

    What it shows

    PASS report.txt has the count, then the lines

Ready for the exam? The level exam is one real task that uses what this recap covers.

Take the level exam