The “time” Command in Linux [4 Practical Examples]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

The time command in Linux measures the amount of time it takes to complete a command or a script. This command is very useful for analyzing the resource utilization and performance of the system in Linux. In this section, I will demonstrate the ins and outs of the time command in Linux.

A. Description

The time command in Linux is a useful command that can be used to optimize the performance of the system. It displays the time taken by the command in three types and these are:

  • Real-Time: The real-time is the actual amount of time that has passed while the command was running.
  • User Time: The user time is the amount of CPU time spent by the CPU while executing the commands in user space.
  • System Time: The system time is the amount of CPU time that was spent by the CPU while executing the commands in system mode or kernel mode.

B. Syntax

The time command in Linux has a very simple syntax. And the syntax is indicated below:

time [OPTION]... [COMMAND]
Note: In the syntax above, OPTION is enclosed by a square bracket followed by 3 dots representing that more than one option can be used simultaneously. Moreover, COMMAND suggests that you can specify one command at a time.

C. Options

A few options are available for the time command. I have mentioned the most used options of the command here. However, you can explore the man page for the time command to know more about its options.

man time

Useful Options

  • -p, Displays time in POSIX format.
  • -o, Stores the output in the text file.
Note: The options in Linux CLI (Command Line Interface) are case-sensitive, so be careful while using them.

Practical Examples of the “time” Command in Linux

The time command measures the time of running a particular command or bash script and displays that on the terminal screen. Here you will learn some practical examples of the time command in Linux.

Example 1: Time Measurement of Running the “ls” Command Using the “time” Command in Linux

The time of running a specific command can be measured using the time command. Here I want to measure the time of running the ls command in Linux. Follow the process below to measure time:

Steps to Follow >

➊ Initially open the Terminal in Ubuntu.

➋ Execute the command below in the command prompt:

time ls

➌ Then, tap the ENTER button.

Output >

As you can see, the contents of the current directory are listed in the output and the time of running the ls command is shown as well.Time Measurement of Running the “ls” Command Using the “time” Command in Linux

Example 2: Time Measurement of Running the Bash Script

You can measure the time of running a bash script in Linux. Here I want to measure the time of running the script.sh in Linux. You can follow the steps below to measure time:

Steps to Follow >

➊ To start, launch the Ubuntu Terminal.

➋ Copy the command below in the command prompt:

time bash script.sh

➌ Now, hit the ENTER key.

Output >

As you can see in the image below, the output is showing the time of running the script.sh.Time Measurement of Running the Bash Script

Example 3: Display Time in POSIX Format Using the “time” Command in Linux

POSIX stands for Portable Operating System Interface. It stores time in seconds. You can use the time command with option -p in Linux to display the time in POSIX format. Here I want to determine the time of running the sleep command in Linux. To determine the time you can follow the instructions below:

Steps to Follow >

➊ Launch the Ubuntu Terminal.

➋ Type the following command in the command prompt:

time -p sleep 5

➌ After that, strike the ENTER button.

Output >

In the image below, the output is showing that it took 5 seconds(as determined) to run the sleep command in Linux.Display Time in POSIX Format Using the “time” Command in Linux

Example 4: Display the Time in the Text File Rather than the Terminal Screen

If you want to display the time in the text file rather than the terminal screen, you can also do this by using the time command with the option -o. Do follow the procedure below to display time in a text file:

Steps to Follow >

➊ To initiate open the Ubuntu Terminal.

➋ Run the command below in the command prompt:

/usr/bin/time -o time.txt sleep 5

➌ Then, press the ENTER button.

❹ Now execute the following command in the command prompt:

cat time.txt

❺ After that, hit the ENTER key.

Output >

In the following image, you can see the time in the text file.Display the Time in the Text File Rather than the Terminal Screen

Conclusion

As exemplified in this article, the time command has some practical uses in Linux. You’ve also got to know the syntax, some functional options, and the practical applications of this command. To master Linux, practice the command and its practical applications thoroughly.


Similar Readings

Rate this post
Jannatul Ferdousi Sylvie

Hi there! This is Jannatul Ferdousi Sylvie. I am one of the Linux Content Developer Executives at Softeko. I live in Dhaka, Bangladesh. I have completed my BSc. in Software Engineering from American International University-Bangladesh. You can see my projects in the GitHub account. I love traveling, shopping, cooking, and gardening. Read Full Bio

Leave a Comment