Module 2 · Files and directories
Module 2 recap: a small filing project
One messy inbox, three jobs: sort it into folders, clean out the junk and take a faithful backup, using every verb from this module.
What you will learn
- Combine `mkdir`, `mv`, `cp`, `rm` and wildcards to organise a real directory.
- Choose the safe tool for each deletion and preview before acting.
- Read a file to get an instruction, then act on it.
Module 2 gave you the everyday verbs of the command line. On their own each one is simple; the skill is in chaining them without surprises. This lesson is a short review followed by a project that works like an exam: the tasks do not tell you which command to use, only what the result must be. Read the whole lesson first, then open the VM.
The verbs at a glance
| Command | Job | Key options | Watch out for |
|---|---|---|---|
mkdir | Create directories | -p parents, idempotent | Missing parents without -p |
touch | Create empty files, stamp times | -d, -r, -c | Nothing: it never erases content |
cp | Copy | -r, -i, -p, -a | Silent overwrite; copy lands *inside* an existing dir |
mv | Move and rename | -i, -n, -v | Silent overwrite; typo = rename |
rm / rmdir | Delete | -r, -i, -f / -p | No undo; stray spaces |
cat less head tail | Read | -n; / search; -n N, +N, -f | cat on binaries |
Three habits that tie it together
- Look, then act. Before any
mvorrmwith a wildcard, runls -dorechowith the same pattern. If the list is right, press the up arrow and swap the command. - Prefer the tool that fails safely.
rmdiroverrm -rfor directories that should be empty; a trailing slash on destination directories;-iwhen overwriting is possible. - Chain with
&&.mkdir -p dest && mv *.jpg dest/runs the move only if the directory was created. A failed first step stops the line instead of scattering files. (Lesson 89 explains&&in detail.)
The project
In /root/lab/l20 you will find an inbox that collected everything: three photos, two text documents, some temporary files and a directory called old that is empty and useless. Next to it is a project directory whose README ends with an instruction. The three practical tasks below are independent; each one resets the lab, so do them in any order and retry as often as you like.
~% cd /root/lab/l20
l20% ls -F inbox project
inbox:
a.tmp cat.jpg minutes.txt sunset.jpg
b.tmp dog.jpg old/ todo.txt
project:
README src/
l20% tail -n 1 project/README
backup-name: snapshot-v1
Commands in this lesson
| Command | What it does |
|---|---|
mkdir -p A/B A/C | Build a folder structure in one go. |
mv DIR/*.jpg DEST/ | Move everything matching a pattern. |
rm DIR/*.tmp | Delete by pattern (preview first!). |
rmdir DIR | Remove a directory only if it is empty. |
cp -a SRC DEST | Faithful copy of a tree, timestamps included. |
A && B | Run B only if A succeeded. |
Quiz
Which command creates `files/images` and `files/docs` even if `files` does not exist yet?
- `mkdir files/images files/docs`
- `mkdir -p files/images files/docs`
- `touch files/images files/docs`
`backup/` exists. Where does `cp -r project backup/` put the copy?
- `backup/project`
- It replaces `backup`
- It merges the files of `project` directly into `backup`
Which pattern selects `a.tmp` and `b.tmp` but not `ab.tmp`?
- `*.tmp`
- `?.tmp`
- `[ab]*.tmp`
You think `inbox/old` is empty and want it gone. Which command is the safest?
- `rm -rf inbox/old`
- `rmdir inbox/old`
- `rm inbox/old`
Which command shows only the last line of `README`?
- `head -n 1 README`
- `tail -n 1 README`
- `tail -n +1 README`
Why use `cp -a` rather than `cp -r` for a backup?
- It is faster
- It keeps timestamps, permissions and symbolic links as they were
- It compresses the copy
In `mkdir -p dest && mv *.jpg dest/`, when does the `mv` run?
- Always
- Only if `mkdir` succeeded
- Only if `mkdir` failed
Practice
Sort the inbox: create /root/lab/l20/sorted/images and /root/lab/l20/sorted/docs, then move every `.jpg` from /root/lab/l20/inbox into `images` and every `.txt` into `docs`. The `.tmp` files and the `old` directory stay where they are.
Clean the inbox: delete every `.tmp` file in /root/lab/l20/inbox and remove the empty directory /root/lab/l20/inbox/old. Photos and documents must survive.
The last line of /root/lab/l20/project/README says what the backup must be called. Copy the whole /root/lab/l20/project directory to /root/lab/l20/<that name>, keeping the original timestamps of the files.
Open this lesson in the app to do the tasks in a real Linux machine and have them checked.