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
1Find the lines
grep.
The idea
A server log can have thousands of lines.
grep word fileprints only the lines that contain the word.grep -i word fileignores 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.txtWhat it shows
PASS errors.txt has every error line
2Count it
wc -l and grep -c.
The idea
Your boss doesn't want the lines, just a number.
wc -l filecounts the lines in a file.grep -c word filecounts 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.txtWhat it shows
PASS total.txt has the number of visits
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 | sortputs the names in A to Z order.uniqdrops a line when it's the same as the line just above it, so putsortfirst: same lines end up next to each other.cat names.txt | sort | uniq.The code
cat names.txt | sort | uniq > list.txtWhat it shows
PASS list.txt has each name once, in order
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.txtWhat it shows
PASS all-errors.txt has web errors, then mail errors
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 -ccounts each run of the same line.sort -rnputs the biggest numbers first.head -n 3keeps 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.txtWhat it shows
PASS top.txt has the 3 busiest pages
6Find files
find -name.
The idea
lsonly 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.txtWhat it shows
PASS logs.txt lists every .log file
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 -lshows it, likerw-r--r--.chmod +x run.shlets the script run. Numbers work too: each group is read 4 + write 2 + run 1, sochmod 600 key.txtmeans 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.txtWhat it shows
PASS backup.sh can run
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
sudoin 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.logWhat it shows
PASS shop.conf is backed up
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 --installand 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, butls,cd,cat,grep,chmodand the rest of this course work the same way.The code
mkdir practice cd practice whoami > me.txt pwd >> me.txtWhat it shows
PASS practice/me.txt says who and where you are
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.txtfromnight.log: first line, the number of lines withERROR; then everyERRORline under it.The code
grep -c ERROR night.log > report.txt grep ERROR night.log >> report.txtWhat 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