online posts 16
linux and the command line 101

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.

you edit, in the labanswers/2.2.ymlfiles/backups/

open a file in VS Code: Ctrl+P (Cmd+P on a Mac), type its name, Enter

given
LessonWorking with files
The messfiles/backups/: config backups saved by hand, one per device per day
Your answersanswers/2.2.yml
Start againgit restore files/backups && git clean -fd files/backups puts the folder back
new to this? step by stepbeginner
  1. find files -name '*.cfg' -empty lists the empty configs; find files -name '*leaf2*' the leaf2 files. Count them.
  2. Challenge: in files/backups, mkdir r1 sw1, mv the file with the space to a name with a dash, delete the empty configs, then mv r1-*.cfg r1/ and the same for sw1.
  3. ls -R files/backups should show r1/ and sw1/ with two configs each, and README.txt.
  4. Fill in answers/2.2.yml, then make check T=2.2.

more in meet the lab

1 explain

Moving files without a mouse

Copy, move, delete

CommandDoes
mkdir r1makes a folder (mkdir -p a/b/c makes the whole chain)
cp a bcopies a to b
mv a bmoves 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 adeletes a file. There’s no recycle bin: it’s gone
rmdir dirdeletes 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:

PatternMatches
*any characters, any number of them (also none)
?exactly one character
r1-*.cfgevery 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.

2 show

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
3 follow along

Count with find

  1. Every config under files/:

    run in the terminal
    find files -name '*.cfg'
    
  2. Just the empty ones. How many? That’s empty_files:

    run in the terminal
    find files -name '*.cfg' -empty
    
  3. Every file with leaf2 anywhere in its name. Count the lines: that’s leaf2_files. Look closely at the one in files/configs.

    run in the terminal
    find files -name '*leaf2*'
    
  4. See what a glob turns into before you trust it with mv:

    run in the terminal
    cd files/backups
    echo r1-*.cfg
    echo sw1-*.cfg
    cd -
    

    Notice that sw1-*.cfg doesn’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.

4 challenge

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, r1 and sw1, with that device’s configs in it;
  • sw1 2026-11-02.cfg renamed to sw1-2026-11-02.cfg, like the others;
  • the empty configs deleted;
  • README.txt left 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.2

when 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