← Teacher guide

Changing files

Students make folders, copy, move, rename and delete files (rm and rm -r) and save text with echo and >, learning that rm has no recycle bin (AC9TDI10K01 node 1).

The command line · Using a shell · about 40 minutes · Open the student lesson

Classroom resources

Load these into Waypoints Prepare: choose Load URL and paste the copied address, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.

Students will be able to

Timing

Predict and Try 7 min · Worked example 10 min · Vary 10 min · Practise and Make 13 min

Before class

Try the boards yourself.

The one idea: these commands change the filesystem, and rm is permanent. Real servers have no bin; backups are how people recover.

The pretend mv and cp take one file and one destination.

Running the lesson

  1. Predict: students who said the bin will meet the empty ls in the Try.
  2. Worked example: ask how many copies exist after cp, and after mv.
  3. Vary: ask why rm refuses a folder without -r.
  4. Make: compare routes: three commands or one?

The hard part (on purpose)

The Make has more than one route; students who try one big mv and get pictures/photos inside should look at the tree and fix it, not start again.

When students get stuck

What you will seeA question to askThe fix
A student expects to undo rm (Sam's mistake).Where would the file have gone? Is there a bin folder?rm is final; copy first.
A student's > emptied a file.What's the difference between > and >>?> replaces; >> adds to the end.

Answers to the checks

  1. after rm: You type rm notes.txt. Where is notes.txt now?
    Gone. rm deletes straight away; the bin on your laptop is something the desktop adds, not the operating system.
  2. Try: back it up, then delete: Make a folder called old, copy notes.txt into it, then delete the notes.txt in your home folder.

    mkdir old, cp notes.txt old, rm notes.txt. The copy in old is the only one left: rm really deletes.
  3. Five commands that change things: You type mv pets.txt photos. Then ls in your home folder. Is pets.txt there?
    No: mv moved it into photos
    mv moves (one file, new place); cp copies (two files).
  4. Find the mistake: Sam deletes a file. Click the line where it first goes wrong.
    line 3
    Line 3: rm has no recycle bin. The file is gone; copy anything you might need first.
  5. Renaming: How do you rename notes.txt to todo.txt?
    mv notes.txt todo.txt
    Moving a file to a new name in the same folder renames it.
  6. Rename: Rename notes.txt to todo.txt.

    mv notes.txt todo.txt: same file, new name.
  7. Make a file: Make a file called hello.txt that says hello, using echo and >.

    echo hello > hello.txt. Careful: > replaces whatever was in the file.
  8. Remove a folder: Delete the photos folder and both photos in it, with one command.

    rm -r photos. Plain rm refuses folders, because -r deletes everything inside.
  9. Back up first: You want to try editing report.txt but keep the original safe. What first?
    cp report.txt report-backup.txt
    Copy first; then you can always go back.
  10. Replace or add: echo hi > notes.txt. What happens to what was in notes.txt?
    It's replaced by hi
    > replaces the file's contents; >> adds to the end.
  11. Review: hidden: Review: how do you see hidden files?
    ls -a
    -a for all.
  12. tidy up: Tidy up: both photos should end up in a new folder called pictures inside projects, and the old photos folder should be gone.

    mkdir, mv, mv, rm -r. Or, before making pictures, one command: mv photos projects/pictures moves and renames the whole folder.

Solutions

One possible solution for each task. Other correct programs are fine.

Vary: one change each › Rename

Open task: any program that does what the step asks.

Vary: one change each › Make a file

Open task: any program that does what the step asks.

Vary: one change each › Remove a folder

Open task: any program that does what the step asks.

tidy up

Open task: any program that does what the step asks.

Stretch and support

Stretch: Make a folder called backups and copy every .txt file in your home folder into it, one cp at a time. How would a script do it?

Support: Add to the card: mkdir, cp a b, mv a b, rm a, rm -r folder, echo text > file.

Exit ticket

What is the difference between cp notes.txt old and mv notes.txt old? Which is safer before deleting?

cp leaves the original and makes a copy in old; mv moves it. cp is safer.

What each step is for

Where it leads

Next: Pipes.