Unit 3: Finding Things
Ten thousand files and no scrolling.
Unit 3 of 8 in Linux and the command line for kids. Its 6 lessons are How Big Is It?, Searching Inside, Grep's Switches, Wildcards, Finding Files and The Missing Homework — below is everything each one explains, and a question or two from it to try.
Every terminal session on this page was replayed before it shipped, on a machine whose every message is tested against real bash and coreutils.
This unit opens with a fortnight’s trial of everything — no card needed — or with a family plan, bought in the iPhone app. The first two units of every track are free for ever. Try it in the app.
📏 How Big Is It?
Counting without reading
wc counts. On its own it gives three numbers: lines, words, characters, in that order.
A flag picks just one. -l for lines is the one you will use nine times out of ten.
Terminal
kid@alguni:~$ wc story.txt 2 6 23 story.txt kid@alguni:~$ wc -l story.txt 2 story.txt kid@alguni:~$ wc -w story.txt 6 story.txt
How big is that folder?
ls -l is ls in long form: one line per thing, with a lot more on it.
The number in the middle is the size in characters. There is plenty more on that line — you will read the rest of it in unit 5, which is entirely about the first ten letters.
Terminal
kid@alguni:~$ ls -l total 12 -rw-r--r-- 1 kid kid 292 Jan 1 09:00 big.txt drwxr-xr-x 2 kid kid 4096 Jan 1 09:00 folder -rw-r--r-- 1 kid kid 6 Jan 1 09:00 small.txt
Try it yourself
How many lines?
Terminal
wc -l colours.txt
Answer it in the app
🔦 Searching Inside
The best command there is
grep prints every line that has your word in it, and ignores the rest.
Give it the word first, then the file. On a file of ten thousand lines it takes no longer than on ten.
Terminal
kid@alguni:~$ grep Turing people.txt Alan Turing 1912 kid@alguni:~$ grep 19 people.txt Alan Turing 1912 Grace Hopper 1906 Katherine Johnson 1918
Its odd name
grep is not a word. It comes from an instruction in an ancient editor: g/re/p, meaning "globally search for a pattern and print it".
Nobody would name it that today. Everybody uses it anyway, and now you know why it looks like a cat walked over the keyboard.
Nothing found
When nothing matches, grep says nothing at all. No "0 results", no message.
That silence *is* the answer, and it takes a bit of getting used to. Unix commands that worked and had nothing to say keep quiet.
Terminal
kid@alguni:~$ grep pineapple fruit.txt kid@alguni:~$ grep apple fruit.txt apple
Try it yourself
What comes out?
Terminal
grep apple menu.txt
Answer it in the app
🎛️ Grep's Switches
Four flags worth knowing
-i — ignore capitals, so cat finds Cat-n — show which line number each one was-v — turn it inside out: the lines that do not match-c — just count them
You can stack them: grep -in does both.
Terminal
kid@alguni:~$ grep cat pets.txt cat food kid@alguni:~$ grep -i cat pets.txt Cat cat food kid@alguni:~$ grep -n -i cat pets.txt 1:Cat 3:cat food kid@alguni:~$ grep -c -i cat pets.txt 2
Everything except
-v is the odd one and it is enormously useful. It gives you every line that does not match — which is how you throw away the boring lines and keep the rest.
Terminal
kid@alguni:~$ grep -v "#" config.txt real line another real line
Try it yourself
Which flag shows the lines that do not match?
- -v
- -i
- -n
- -c
Answer it in the app
⭐ Wildcards
Standing for anything
A * in a name means "anything at all, however long".
*.txt means every name ending in .txt. photo* means every name starting with photo. And ? is the same idea for exactly one character.
Terminal
kid@alguni:~$ ls a.txt b.txt notes.txt photo1.jpg photo2.jpg song.mp3 kid@alguni:~$ ls *.txt a.txt b.txt notes.txt kid@alguni:~$ ls photo* photo1.jpg photo2.jpg kid@alguni:~$ ls ?.txt a.txt b.txt
The command never sees the star
Here is the part that surprises everybody. ls knows nothing about *.
The shell looks at *.txt, works out which files match, and hands ls the finished list. By the time ls wakes up it has been given three plain names.
Watch echo prove it: echo cannot search for files, and yet it prints them all.
Terminal
kid@alguni:~$ echo *.txt a.txt b.txt c.txt kid@alguni:~$ echo "*.txt" *.txt
When nothing matches
If no file matches, the shell gives up and hands the command the pattern exactly as you typed it.
So ls *.zip in a folder with no zip files complains about a file literally called *.zip. That message confuses everybody once. Now it will not confuse you.
Terminal
kid@alguni:~$ ls *.zip ls: cannot access '*.zip': No such file or directory
Try it yourself
Who turns *.txt into a list of filenames?
- The shell, before the command runs
- The command itself
- Linux, when the file is saved
- Nobody — `*` is just part of the name
Answer it in the app
🧭 Finding Files
Looking everywhere at once
A wildcard only looks in one folder. find goes all the way down, into every folder inside every folder.
Say where to start, then what you are looking for. find . -name "*.txt" means "starting here, everything ending in .txt".
Terminal
kid@alguni:~$ find school school school/art school/art/paint.jpg school/maths school/maths/sums.txt school/timetable.txt kid@alguni:~$ find school -name "*.txt" school/maths/sums.txt school/timetable.txt
Files or folders
-type f says files only. -type d says directories only.
And notice the quotes round "*.txt". Without them the shell would expand the star itself, before find ever saw it — which is exactly what the last lesson warned about.
Terminal
kid@alguni:~$ find a -type f a/b/two.txt a/one.txt kid@alguni:~$ find a -type d a a/b
Try it yourself
What is the difference between ls *.txt and find . -name "*.txt"?
- find looks inside every folder underneath, ls only looks here
- They do exactly the same thing
- find is faster
- ls can only show ten files
Answer it in the app
🏆 The Missing Homework
A real search
You wrote something about volcanoes. You cannot remember what you called it or where you put it.
You know two things about it: it is a .txt file, and the word volcano is in it. That is enough.
Try it yourself
You want every line mentioning dragon in every file under stories. What do you type?
- grep -r dragon stories
- find stories -name dragon
- ls -r dragon stories
- cat stories | dragon
Answer it in the app