Repeat Command in Terminal
Solutions: Repeat Command in Ubuntu Terminal can significantly boost your productivity. Repetitive tasks can often feel boring, even for seasoned Linux users. Whether you’re re-running a failed command, monitoring real-time outputs, or testing scripts, the ability to efficiently repeat commands in the Ubuntu terminal can significantly boost your productivity.
In this guide, we’ll explore practical methods to repeat commands in the terminal effortlessly. From leveraging Bash shortcuts and history features to using advanced command-line tools, you’ll discover solutions tailored to various scenarios. Whether you’re a beginner or a pro, these tips will help you save time and streamline your workflow.
Let’s dive into the world of terminal magic!
About the watch
Command
watch
runs command repeatedly, displaying its output and errors (the first screenfull). This allows you to watch the program output change over time. By default, command is run every 2 seconds and watch will run until interrupted.
For more details about watch
command, you can check the link.
Example of watch
Command
Whether you’re monitoring system resources, tracking log file changes, or observing temperature readings from hardware sensors, watch
provides a dynamic way to automate and visualize recurring command executions.
Monitor temperature every 2 seconds on Pi4 device:
The command continuously monitors the temp1
sensor’s value and displays only the second field (likely the temperature). The watch
command refreshes this information at regular intervals, making it easy to track changes in real-time.
watch 'sensors | grep "temp1" | awk "{print \$2}"'
watch
: Repeats the provided command at regular intervals (default is every 2 seconds). Useful for monitoring output over time.sensors
: Displays hardware sensor readings such as CPU temperature, fan speed, and voltage. Requires thelm-sensors
package to be installed.|
(Pipe): Passes the output ofsensors
to the next command in the pipeline.grep "temp1"
: Searches for lines in thesensors
output that contain the text “temp1”. This is commonly associated with a specific temperature sensor.awk "{print \$2}"
: Processes the filtered output fromgrep
. It selects and prints the second field (usually the temperature value), assuming fields are separated by whitespace or similar delimiters.
Check the IP Address every 60 Seconds:
watch -n 60 ip addr
-n 60
: Sets the interval to 60 seconds.ip addr
: The command to execute.
Tracking Disk Usage with df
If you want to observe the disk usage of your system’s partitions at regular intervals, you can use:
watch -n 10 df -h
-n 10
: Runs thedf -h
command every 10 seconds.df -h
: Displays disk usage in a human-readable format.
If you are interested in how to run a command in the background, see this link.