task 2.2 · chapter 2 of 8: working with files · 20 min
Tidy up
objective
Count files with find, then tidy a messy folder of config backups: make folders, rename the file with a space in its name, delete the empty ones, and move the rest with globs.
answers/2.2.ymlfiles/backups/open a file in VS Code: Ctrl+P (Cmd+P on a Mac), type its name, Enter
| Lesson | Working with files |
|---|---|
| The mess | files/backups/: config backups saved by hand, one per device per day |
| Your answers | answers/2.2.yml |
| Start again | git restore files/backups && git clean -fd files/backups puts the folder back |
new to this? step by stepbeginner
find files -name '*.cfg' -emptylists the empty configs;find files -name '*leaf2*'the leaf2 files. Count them.- Challenge: in files/backups,
mkdir r1 sw1,mvthe file with the space to a name with a dash, delete the empty configs, thenmv r1-*.cfg r1/and the same for sw1. ls -R files/backupsshould show r1/ and sw1/ with two configs each, and README.txt.- Fill in answers/2.2.yml, then
make check T=2.2.
more in meet the lab
Moving files without a mouse
Copy, move, delete
| Command | Does |
|---|---|
mkdir r1 | makes a folder (mkdir -p a/b/c makes the whole chain) |
cp a b | copies a to b |
mv a b | moves a to b. If b is in the same folder, that’s a rename |
mv a b c dir/ | moves several files into a folder |
rm a | deletes a file. There’s no recycle bin: it’s gone |
rmdir dir | deletes an empty folder |
in one sentence mv both moves and renames; rm deletes for good, with no bin to fish it out of.
Globs: one pattern, many files
A glob is a pattern the shell turns into a list of matching file names before the command runs:
| Pattern | Matches |
|---|---|
* | any characters, any number of them (also none) |
? | exactly one character |
r1-*.cfg | every name that starts r1- and ends .cfg |
So mv r1-*.cfg r1/ is really mv r1-2026-11-01.cfg r1-2026-11-02.cfg r1/: mv never sees the *. Try
echo r1-*.cfg first to see what a glob turns into; echo just prints it.
Names with spaces
The shell splits a command line on spaces, so mv sw1 2026-11-02.cfg new.cfg means three things to move.
Quote a name with spaces in it: 'sw1 2026-11-02.cfg'. Tab adds the quoting for you.
find: search the tree
find walks a folder and everything under it, and prints each path that matches all its tests:
find files -name '*.cfg' every .cfg file under files/
find files -name '*.cfg' -empty ... that's empty
find files -type d every folder
Quote the pattern after -name, so the shell hands the * to find instead of expanding it first.
in one sentence Globs are expanded by the shell; find searches a whole tree; quote anything with a space or a * you want to keep.
The mess
$ ls -l files/backups
total 20
-rw-r--r-- 1 vscode vscode 83 Nov 11 05:51 README.txt
-rw-r--r-- 1 vscode vscode 304 Nov 11 05:51 r1-2026-11-01.cfg
-rw-r--r-- 1 vscode vscode 398 Nov 11 05:51 r1-2026-11-02.cfg
-rw-r--r-- 1 vscode vscode 0 Nov 11 05:51 r1-2026-11-03.cfg
-rw-r--r-- 1 vscode vscode 277 Nov 11 05:51 sw1 2026-11-02.cfg
-rw-r--r-- 1 vscode vscode 304 Nov 11 05:51 sw1-2026-11-01.cfg
-rw-r--r-- 1 vscode vscode 0 Nov 11 05:51 sw1-2026-11-03.cfg
The fifth column is each file’s size in bytes. Two of them are 0: someone’s save failed. One name has a space
where the others have a dash.
When it’s tidy, it should look like this:
$ ls -R files/backups
files/backups:
README.txt r1 sw1
files/backups/r1:
r1-2026-11-01.cfg r1-2026-11-02.cfg
files/backups/sw1:
sw1-2026-11-01.cfg sw1-2026-11-02.cfg
Count with find
Every config under
files/:run in the terminalfind files -name '*.cfg'Just the empty ones. How many? That’s
empty_files:run in the terminalfind files -name '*.cfg' -emptyEvery file with
leaf2anywhere in its name. Count the lines: that’sleaf2_files. Look closely at the one infiles/configs.run in the terminalfind files -name '*leaf2*'See what a glob turns into before you trust it with
mv:run in the terminalcd files/backups echo r1-*.cfg echo sw1-*.cfg cd -Notice that
sw1-*.cfgdoesn’t match the file with the space: there’s no dash in its name.
in one sentence find PATH -name 'PATTERN' searches a whole tree; echo a glob to see what it will match.
Tidy the backups
Write your two counts in answers/2.2.yml first: once you delete the empty files, find can’t find them.
Then make files/backups look like the tidy listing above:
- a folder for each device,
r1andsw1, with that device’s configs in it; sw1 2026-11-02.cfgrenamed tosw1-2026-11-02.cfg, like the others;- the empty configs deleted;
README.txtleft where it is.
Don’t edit the configs themselves: the checker compares each one with the file it started as. Then run
make check T=2.2.
hint
cd files/backups first, so every name is short. mkdir r1 sw1 makes both folders at once.
hint
Rename the odd one before you move anything: mv 'sw1 2026-11-02.cfg' sw1-2026-11-02.cfg. Then sw1-*.cfg
matches it too.
hint
find . -maxdepth 1 -name '*.cfg' -empty -delete deletes the empty ones in this folder (-maxdepth 1: don’t
look in the subfolders). Or rm them by name: there are only two.
show the solution
empty_files: 2
leaf2_files: 4
cd files/backups
mkdir r1 sw1
mv 'sw1 2026-11-02.cfg' sw1-2026-11-02.cfg
rm r1-2026-11-03.cfg sw1-2026-11-03.cfg
mv r1-*.cfg r1/
mv sw1-*.cfg sw1/
ls -R
cd -
The order matters: delete the empty files before the globs move them, and rename the spaced one before
sw1-*.cfg runs, or it’s left behind. files/configs/chi leaf2.cfg is the fourth leaf2 file, the one ls
shows as two words.
5 check: run it, then prove it
$ make check T=2.2when it's right, you'll see
╭─ task 2.2 · tidy up
│ ✓ how many empty .cfg files find found (challenge): ...
│ ✓ how many files under files/ have leaf2 in their name (challenge): ...
│ ✓ files/backups holds r1/ and sw1/, two configs each, and the README (challenge)
│ ✓ each config is the one it started as, just moved or renamed (challenge)
╰─ 4/4 checks passed verified