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
Get-ChildItem
The Mode column carries the answer: an entry beginning with d is a directory. Get-Item returns the single item at one path, which is the narrower question:
Get-Item -LiteralPath ./data/README.txt
-LiteralPath means “use this path exactly as written.” It is the right choice whenever you know the precise name, and it matters most when a filename contains characters PowerShell would otherwise read as a pattern to match against several files. A later chapter teaches those patterns deliberately; here you always mean one exact path.
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
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
Get-Content -LiteralPath ./data/README.txt
Quotes hold a spaced path together, exactly as they did in the previous chapter:
Get-Content -LiteralPath './notes with spaces.txt'
cat data/README.txt
A path with spaces needs quoting, or the cat three separate arguments instead of one filename:
cat "notes with spaces.txt"
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.
Set-Content -LiteralPath ./reading.txt -Value 'north: 12'
Add-Content -LiteralPath ./reading.txt -Value 'south: 9'
Get-Content -LiteralPath ./reading.txt
Set-Content writes the file from the top, replacing everything already in it, with no warning and no undo. Add-Content appends to what is there. One Set-Content to start a file, Add-Content for every line after 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.
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.
Confirm you are at
/workspace.List the workspace and pick out which entries are directories.
List
dataand confirm the supplied tree holdsREADME.txtandmeasurements.txt.Read
data/README.txtand note the value aftermarker-name:.Read
notes with spaces.txt, with the path quoted.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.Read
destination/inspection.txtback 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.