Output to a File: >
So far, every command has printed its output to the screen. But what if you need to
save that output? The > operator sends (redirects) a command's output into a
file instead of displaying it.
The pattern is: COMMAND > FILE.
echo "Reactor online" > status.txt
Nothing appeared on screen: the text went into status.txt instead. You can verify
with cat:
cat status.txt
Reactor online
Warning: > overwrites!
If the file already exists, > replaces its contents entirely. Think of it as
"create or overwrite."
echo "Reactor offline" > status.txt
cat status.txt
Reactor offline
The old content is gone. When you need to keep existing content, you will use >>
(next lesson).
Any command that produces output can be redirected: ls, grep, cat, date,
anything.
Mission: Critical Alert Extraction
The sensor array has been logging readings to sensors.log, but the chief engineer wants the important lines split into their own files so the repair crew knows exactly what to fix.
- Find the lines containing
criticalinsensors.logand save that output into a new file calledalerts.txtinstead of printing it to the screen. - Do the same for the healthy readings: capture the lines containing
okinto a file calledhealthy.txt. - Create a folder called
reportand move both files into it. - Display
report/alerts.txtto confirm the repair crew's list is correct.
Expected result
The report folder holds alerts.txt (the two critical lines) and healthy.txt (the three healthy readings).