Read

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

CommandJobKey optionsWatch out for
mkdirCreate directories-p parents, idempotentMissing parents without -p
touchCreate empty files, stamp times-d, -r, -cNothing: it never erases content
cpCopy-r, -i, -p, -aSilent overwrite; copy lands *inside* an existing dir
mvMove and rename-i, -n, -vSilent overwrite; typo = rename
rm / rmdirDelete-r, -i, -f / -pNo undo; stray spaces
cat less head tailRead-n; / search; -n N, +N, -fcat on binaries

Three habits that tie it together

  1. Look, then act. Before any mv or rm with a wildcard, run ls -d or echo with the same pattern. If the list is right, press the up arrow and swap the command.
  2. Prefer the tool that fails safely. rmdir over rm -r for directories that should be empty; a trailing slash on destination directories; -i when overwriting is possible.
  3. 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

CommandWhat it does
mkdir -p A/B A/CBuild a folder structure in one go.
mv DIR/*.jpg DEST/Move everything matching a pattern.
rm DIR/*.tmpDelete by pattern (preview first!).
rmdir DIRRemove a directory only if it is empty.
cp -a SRC DESTFaithful copy of a tree, timestamps included.
A && BRun B only if A succeeded.

Quiz

  1. 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`
  2. `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`
  3. Which pattern selects `a.tmp` and `b.tmp` but not `ab.tmp`?

    • `*.tmp`
    • `?.tmp`
    • `[ab]*.tmp`
  4. 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`
  5. Which command shows only the last line of `README`?

    • `head -n 1 README`
    • `tail -n 1 README`
    • `tail -n +1 README`
  6. 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
  7. In `mkdir -p dest && mv *.jpg dest/`, when does the `mv` run?

    • Always
    • Only if `mkdir` succeeded
    • Only if `mkdir` failed

Practice

  1. 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.

  2. 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.

  3. 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.