Linux Command Timing

LinuxLinuxBeginner
Practice Now

Introduction

Welcome to the Enchanted Forest, a mystical place where fairy tales live and breathe. In this serene and magical forest, the creatures and foliage coexist in harmonious balance. However, a cunning Werewolf, named Timewolf, has been tinkering with the fabric of time itself, throwing this balance into chaos. As the appointed guardian of time, your goal is to master the art of timing your Linux commands to restore order and keep the forest's activities on schedule.

As you embark on this adventure, you will need to learn how to use the time command to measure how long it takes for other commands and programs to run. Efficiency is key when dealing with the quicksilver Timewolf! Your quest will not only help you save the Enchanted Forest but also arm you with valuable skills to navigate and optimize the world of Linux.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/SystemInformationandMonitoringGroup -.-> linux/time("`Command Timing`") subgraph Lab Skills linux/time -.-> lab-271405{{"`Linux Command Timing`"}} end

Track the Timewolf

In this step, you will learn how to use the time command to measure how long it takes to execute a simple command that could track the movements of the Timewolf. The time command is important for identifying which commands are efficient and which are dragging their feet through the mystical underbrush.

Begin by creating a script that echos a famous line from fairy tales. Let's call it fairy_tale_echo.sh.

echo -e "#\!/bin/bash \necho 'Mirror, Mirror on the wall...'" > ~/project/fairy_tale_echo.sh
chmod +x ~/project/fairy_tale_echo.sh

Then, use the time command to measure how long it takes to execute your script.

time ~/project/fairy_tale_echo.sh

You should see output similar to this:

Mirror, Mirror on the wall...
~/project/fairy_tale_echo.sh  0.00s user 0.00s system 90% cpu 0.001 total

~/project/fairy_tale_echo.sh is a command pointing to the fairy_tale_echo.sh script file in the project folder in the user's home directory.

0.00s user 0.00s system means that the user mode execution time and the system mode execution time are both 0 seconds.

90% cpu means that the CPU usage reached 90% when this command was executed.

0.001 total means that the total execution time of the command is 0.001 seconds.

To sum up, this sentence describes the execution of the command ~/project/fairy_tale_echo.sh: the command completed the execution in a very short time.

Hunt for Heavy Commands

After discovering how quickly 'fairy_tale_echo.sh' runs, it's time to understand how more complex commands fare against Timewolf's pace. For that, youโ€™ll create a heavier scriptโ€”let's call it the big_bad_wolf_search.sh.

First, create the script that searches for all the occurrences of the word 'wolf' in a natural language processing dataset.

echo "#\!/bin/bash \ngrep -i 'wolf' /usr/share/dict/words" > ~/project/big_bad_wolf_search.sh
chmod +x ~/project/big_bad_wolf_search.sh

Next, use the time command to find out how long it takes to run your grepping operation.

time ~/project/big_bad_wolf_search.sh

Your output might resemble:

wolfskin
wolfward
wolfwards
~/project/big_bad_wolf_search.sh  0.01s user 0.00s system 59% cpu 0.015 total

In this execution, you might notice an increase in the total time compared to the previous script owing to the added complexity of searching through a larger dataset.

Summary

In this lab, we ventured into the realms of the Enchanted Forest and pitted our timing skills against the elusive Timewolf. Through hands-on experience, we utilized the time command to measure the execution duration of Linux commands and discovered the importance of performance tracking. By learning to track command efficiencies, we not only restored balance to the forest but also gathered valuable insights into optimizing our workflows in Linux environments. Your newly acquired knowledge is now a trustworthy guide in the quest for mastery over the Linux command line.

Other Linux Tutorials you may like