← The command line

Changing files

mkdir makes a folder, cp copies, mv moves or renames, rm deletes and echo … > saves text into a file. rm is for good: there is no recycle bin in a shell, so look before you delete.

On your laptop, deleted files go to the bin. In a shell, rm means gone. Ready?

So far you've only looked. These commands change things: they make, copy, move and delete files and folders.

PredictRead the problem and decide what you think before you try anything. A wrong guess is fine: it shows you what to look for.

After rm

You type rm notes.txt. Where is notes.txt now?

TryTry it on the board and compare the result with your prediction.

Try: back it up, then delete

Was your prediction right? Try ls: no notes.txt, and no bin to fetch it from.

Try it yourselfYour edits are saved in this browser.

Make a folder called old, copy notes.txt into it, then delete the notes.txt in your home folder.

Use the board. This ticks when you get there.

InvestigateWork out why it happens, one step at a time.

Worked example: cp or mv?

Worked exampleBefore each step, say what you think comes next.
  1. mkdir old\text{mkdir old}Make a new, empty folder called old.

Now compare cp and mv.

You type mv pets.txt photos. Then ls in your home folder. Is pets.txt there?

Sam deletes a file. Click the line where it first goes wrong.

How do you rename notes.txt to todo.txt?

ModifyChange the problem on purpose and see what happens.

Vary: one change each

Try it yourselfYour edits are saved in this browser.

Rename notes.txt to todo.txt.

Use the board. This ticks when you get there.

Make a file called hello.txt that says hello, using echo and >.

Use the board. This ticks when you get there.

Delete the photos folder and both photos in it, with one command.

Use the board. This ticks when you get there.

PractiseA few on your own, to check you have it.

Safely

You want to try editing report.txt but keep the original safe. What first?

echo hi > notes.txt. What happens to what was in notes.txt?

Review: how do you see hidden files?

MakeMake your own problem using what you have learned. Expect a few tries; that is normal.

Tidy up

Then: which of today's commands would you be most careful with, and why?

Try it yourselfYour edits are saved in this browser.
Need a hint?

The result: a folder called pictures inside projects, with both photos in it, and no photos folder.

Another hint

mkdir projects/pictures, then move the photos.

Another hint

mv photos/beach.jpg projects/pictures, the same for dog.jpg, then rm -r photos.

Tidy up: both photos should end up in a new folder called pictures inside projects, and the old photos folder should be gone.

Use the board. This ticks when you get there.

For teachers

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). About 40 minutes.

Open the teacher guide: answers, solutions, slides and a worksheet.

Students will be able to

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

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.

Where it leads

Next: Pipes.

What each step is for

Now you can
Next lesson4. PipesA pipe (|) sends one command's output into the next command instead of the screen, so small commands combine: grep finds lines, wc -l counts them, sort puts them in order, uniq drops repeats, head keeps the first few, and > saves the result.