0%

The Command Line · practice

Inspect Before You Change Anything

Navigation gets you to a path. It does not tell you what is sitting there, and a path can name a file, a directory, or nothing at all.

Before you copy, move, or remove anything, you want two facts: is this a file or a directory, and what is inside it? This lesson answers both, then closes the loop by writing a file of your own, which is what lets every later exercise leave evidence.

Tell a file from a directory

ls -1F

Two options are doing work there. -1 is the digit one, and it asks for one entry per line instead of columns. -F adds a marker to certain kinds of entry, and a trailing / means directory:

README.md
checkpoint/
data/
destination/
environment-check.py
notes with spaces.txt
report.py

The names have not changed. -F only adds the marks. You can also look inside a directory without moving into it, which is usually what you want when you are only checking:

ls -1 data

Read a short file

cat data/README.txt

A path with spaces needs quoting, or the shell hands cat three separate arguments instead of one filename:

cat "notes with spaces.txt"

Either way, reading is a way of looking rather than a way of editing, and it leaves the file exactly as it was. Detailed permission columns and pipelines are useful later, but neither is needed to answer today’s two questions: what does this path name, and what does that file say?

Write a file from the shell

Recording what you found is the other half of inspecting. Every graded exercise from here on asks for evidence, and this is how you leave it.

echo "north: 12" > reading.txt
echo "south: 9" >> reading.txt
cat reading.txt

echo prints what you give it, and > redirects that text into a file instead of the screen. Neither command printed anything, because the text went into the file rather than to you.

Be deliberate about >. It creates the file if it is missing, and it replaces the entire contents if the file already exists, with no warning and no undo. Use >> to add a line to what is already there. One > to start a file, >> for every line after it.

Practice: inspect, then record what you found

Graded from the workspace, starting at /workspace. Use Reset first if an earlier attempt left destination/inspection.txt behind.

  1. Confirm you are at /workspace.

  2. List the workspace and pick out which entries are directories.

  3. List data and confirm the supplied tree holds README.txt and measurements.txt.

  4. Read data/README.txt and note the value after marker-name:.

  5. Read notes with spaces.txt, with the path quoted.

  6. Write what you found into a new file at destination/inspection.txt. It needs two lines: the marker value from step 4 first, then the number of entries you counted in step 3.

  7. Read destination/inspection.txt back and check both lines before you submit.

Do not edit, move, or remove any supplied file. The check requires destination/inspection.txt and verifies the supplied evidence files remain unchanged; use Reset first so unrelated leftovers do not confuse your own inspection.

If the two lines come out in the wrong order, do not try to patch the file. Write it again from the top, replacing the whole thing. That is the one time overwriting is the easy answer rather than the dangerous one.

Why does reading notes with spaces.txt need the path in quotes?

A file log.txt already holds one line, and you write a new line to it using the command that starts a file from the top. What does it contain afterward?

You can tell a file from a directory, read one, and write one. That last skill is what makes the rest of the chapter possible: from here on, every exercise can leave evidence that says something rather than evidence that merely exists.