Chapter 5: Text, editors and the shell environment

Exercise: Capture and combine output

On servera, save command output to files, keep errors apart from results, discard what you don't need, and chain commands with pipes and tee.

Capture and combine output

  • Write and append output to files.
  • Separate errors from results, and discard them.
  • Build pipelines, and keep a copy with tee.

Start the exercise

On workstation, start the exercise. It removes the result files of an earlier run from student's home on servera.

[student@workstation ~]$ lab start sa-redirect

Later tasks assume that you are logged in to the server they name: ssh student@servera from workstation, then sudo -i for a root shell.

Save a list of configuration files

On servera, as student, list the .conf files directly in /etc into conf-files.txt, then count and peek at it.

[student@workstation ~]$ ssh student@servera
[student@servera ~]$ ls /etc/*.conf > conf-files.txt
[student@servera ~]$ wc -l conf-files.txt
22 conf-files.txt
[student@servera ~]$ head -n 3 conf-files.txt
/etc/chrony.conf
/etc/dracut.conf
/etc/host.conf

The pattern expanded to 22 names, and ls printed one per line because its output went to a file rather than a terminal.

Add a footer line

Add a line with today's date to the end of the same file, without losing the list.

[student@servera ~]$ echo "--- listed on $(date +%F)" >> conf-files.txt
[student@servera ~]$ tail -n 2 conf-files.txt
/etc/yum.conf
--- listed on 2026-10-03

Keep results and errors apart

Search all of /etc for .conf files, sending the results to found.txt and the permission errors to errors.txt. Count both.

[student@servera ~]$ find /etc -name "*.conf" > found.txt 2> errors.txt
[student@servera ~]$ wc -l found.txt errors.txt
  78 found.txt
  13 errors.txt
  91 total

Nothing appeared on the screen: both streams went to files.

Count results, discarding the errors

[student@servera ~]$ find /etc -name "*.conf" 2> /dev/null | wc -l
78

Only stdout flows through the pipe; stderr went to /dev/null.

Find the three biggest files in /etc

ls -lS sorts by size, biggest first. Keep the first four lines (the first is the total line).

[student@servera ~]$ ls -lS /etc | head -4
total 1320
-rw-r--r--.  1 root root   692252 Jun 23  2020 services
-rw-r--r--.  1 root root    13507 Oct  3 09:28 ld.so.cache
-rw-r--r--.  1 root root    10373 May  2  2025 nanorc

See a report and save it at the same time

[student@servera ~]$ df -h | tee disk-report.txt
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        455M     0  455M   0% /dev
...output omitted...
/dev/sda2        20G  1.7G   18G   9% /
...output omitted...
[student@servera ~]$ wc -l disk-report.txt
9 disk-report.txt

Grade and finish

Check your work with lab grade sa-redirect, then put the exercise away with lab finish sa-redirect.