Introduction
In the bustling medieval city of Tuxville, known for its vast markets and intricate craftsmanship, there lived a humble farmer named Gregor. One day, he overheard the royal cartographers saying they needed someone who could monitor the growth progress of the kingdom's crops and report it every hour. Considering this a golden opportunity to serve his kingdom and earn some silver coins, Gregor decided to venture into learning the craft of consistent observation with a bit of magic from the Linux realm. He needs your help to master the command that could repeat itself to watch over the crop fields tirelessly.
Understanding the watch Command
In this step, you're going to help Gregor learn how to use the watch command to monitor the status of his crops.
Tasks
- Task 1: Learn the syntax of the
watchcommand and its basic operation. - Task 2: Use the
watchcommand to repeatedly list the contents of the directory where crop reports are stored.
Requirements
- Familiarize yourself with the
manpages to understand how thewatchcommand works. - The default working path for terminal operations is
/home/labex/project, and the relative path is~/project. - Store the crop reports in a directory named
cropsinside the working path. - Use the
watchcommand to monitor thecropsdirectory. - Command instructions must be executed in the zsh terminal.
Example
Before using watch, Gregor's directory looks like this:
ll /home/labex/project/crops
total 0
After using watch, You can open a new terminal and create some files in the crops directory. The watch command will display the contents of the directory every 2 seconds.
Generating a Report with Timestamps
In this step, Gregor will generate a report file with timestamps using the watch command.
Tasks
- Task 1: Write a script that appends a new timestamp entry to a file called
report.txtinside thecropsdirectory every 1 second. - Task 2: Use the
watchcommand to execute this script repeatedly.
Requirements
Create a script named
timestamp.shinside the~/project/cropsdirectory. Use thedatecommand inside the script to get the current timestamp.#!/bin/bash echo "$(date)" >> /home/labex/project/crops/report.txtThe
report.txtfile should contain the history of executed timestamps.Utilize the
watchcommand to run the script every 1 second.
Example
After using the watch command to continuously execute the timestamp.sh script, the report.txt file will be updated with a new timestamp every second.
You can verify that the watch command is working correctly by executing the following script:
cat /home/labex/project/crops/report.txt
The output should look like this:
Sat Aug 12 09:00:01 UTC 2023
Sat Aug 12 09:02:01 UTC 2023
Sat Aug 12 09:04:01 UTC 2023
Summary
In this challenge, you were tasked to guide Gregor in mastering the watch command to keep a vigilant eye over his crops. By utilizing this powerful command, you successfully helped him generate and continuously update a crop report that satisfies the royal cartographers' demands. Not only did you enrich your Linux command line prowess, but you also contributed to the well-being of Tuxville's agricultural excellence.



