time command in Linux with examples
Last Updated :
05 Sep, 2024
‘time’ command in Linux is used to execute a command and prints a summary of real-time, user CPU time and system CPU time spent by executing a command when it terminates. ‘real‘ time is the time elapsed wall clock time taken by a command to get executed, while ‘user‘ and ‘sys‘ time are the number of CPU seconds that command uses in user and kernel mode respectively.
Understanding Time Command Basics
The ‘time’ command measures the execution time of a specified command or program and reports various metrics, including real, user, and system time. Here’s a breakdown of these metrics:
- Real Time: The actual elapsed time, from start to finish, including time spent waiting for I/O and other processes.
- User Time: The amount of CPU time spent executing user-mode instructions within the process.
- System Time: The amount of CPU time spent executing system-level instructions on behalf of the process.
Syntax:
The syntax for using the time command is:
time [options] command [arguments]
Key Options for the ‘time’ Command in Linux
time -p: This option is used to print time in POSIX format.

help time: it displays help information.

Examples of Time Command in Linux
Let us look at some of the examples of ‘time’ Command in Linux to better understand the concept.
1. To Create a Dummy Job with time Command
In this , ‘sleep 3’ is used to create a dummy job which lasts 3 seconds.
time sleep 3

In the above example, ‘sleep 3’ is used to create a dummy job which lasts 3 seconds.
2. Measure Execution Time of a Command
time wget http://example.com/file.zip
This example demonstrates how to use the time command to measure the execution time of a single command. In this case, the command we get 'http://example.com/file.zip' is timed, and the real, user, and system times are reported upon completion. This is useful for evaluating the performance of individual commands, such as downloading a file from a remote server.
3. Measure Execution Time of a Shell Script
time ./my_script.sh
Here, the time command is used to measure the execution time of a shell script named ‘my_script.sh'. When executed, time will run the shell script and provide timing statistics upon completion. This is helpful for analyzing the performance of complex operations or tasks encapsulated within shell scripts.
4. Compare Execution Time of Multiple Commands
time { command1 ; command2 ; command3 ; }
In this example, multiple commands (command1, command2, and command3) are enclosed within curly braces and executed sequentially. The time command is used to measure the combined execution time of all the commands enclosed within the braces. This allows for easy comparison of the performance of multiple commands executed in sequence.
5. Redirect Output to a File:
time -o timing.log ls -l
Here, the time command is used with the ‘-o' option to redirect the timing data to a file named ‘timing.log'. This is useful for capturing timing statistics for further analysis or documentation purposes. In this example, the ‘ls -l' command is timed, and the timing data is written to the specified file.
6. Custom Output Format:
time -f "User: %U seconds, System: %S seconds, Real: %e seconds" command
This example demonstrates how to specify a custom output format using the ‘-f' option with the time command. The format string "User: %U seconds, System: %S seconds, Real: %e seconds" defines the desired format for the timing data, including user, system, and real times. This allows for flexibility in formatting the output according to specific requirements or preferences.
Conclusion
In this article we discussed the time command in Linux which is super helpful for figuring out how long it takes for commands or programs to run. It tells you stuff like the actual time it took (real time), how much CPU time it used (user and system time), and helps you see if something’s running efficiently. Understanding the basics, like the syntax and options, is key. Through examples, we’ve shown how to use time to measure command and script execution, compare multiple commands, and even customize the output format. By using time, Linux users can better understand and improve their system’s performance.
time command in Linux with examples – FAQs
What is the time command used for in Linux?
The time command in Linux is used to measure the duration it takes for a program to execute. This tool provides crucial insights into how long a process runs, including user CPU time, system CPU time, and elapsed real-time. This is particularly useful for performance testing, optimizing program execution, and system monitoring.
How to measure the execution time of a script using time?
To measure the execution time of a script using the time command, prepend time to the command that starts your script. For example, if you have a script named script.sh:
time ./script.sh
This command will execute script.sh and display the time statistics after the script completes its execution.
Can time command measure system and user time separately?
Yes, the time command can measure system and user CPU time separately:
- User time refers to the amount of CPU time spent in user-mode (outside of the kernel) within the process. This is time spent on computations or executing the process code.
- System time refers to the amount of CPU time spent in the kernel within the process. This is usually for system calls made by the process.
These times are part of the output provided by time.
What are some common options for the time command?
While the time command itself is quite straightforward, there are a few options that can be useful:
- -p: When this option is used,
time outputs the timing information in a POSIX-compliant format, which might be easier to use in scripts or further processed by other tools.
- -f format, –format=format: Allows you to specify the output format of the time command. This can be used to display the information that is most relevant to your testing scenario.
- -o file, –output=file: Directs the output to a file instead of the terminal, useful for logging performance data over time.
- -v, –verbose: Provides a more detailed time report, including more information such as voluntary and involuntary context switches.
How to interpret the output of the time command?
The output of the time command typically looks like this (default format):
real 0m1.005s
user 0m0.004s
sys 0m0.001s
Hereâs how to interpret these values:
- Real: The elapsed actual time between invocation and termination of the command (often called wall-clock time). This includes all sorts of delays, including time delays due to resource contention or waiting for I/O operations to complete.
- User: The total amount of CPU time that the process used directly in user mode.
- Sys: The total amount of CPU time spent in the system (kernel) mode on behalf of the process (making system calls, executing kernel functions, etc.).
Master DevOps and also get 90% Course fee refund on completing 90% course in 90 days! Take the Three 90 Challenge today.
The most sought-after Three 90 challenge has started and this is your chance to upskill and get 90% refund. What more motivation do you need? Start the challenge right away!
Similar Reads
stty command in Linux with Examples
stty command in Linux is used to change and print terminal line settings. This command shows or changes terminal characteristics. Syntaxstty [-F DEVICE | --file=DEVICE] [SETTING]...stty [-F DEVICE | --file=DEVICE] [-a|--all]stty [-F DEVICE | --file=DEVICE] [-g|--save]where, -F DEVICE or --file=DEVIC
2 min read
sudo Command in Linux with Examples
sudo (Super User DO) command in Linux is generally used as a prefix for some commands that only superusers are allowed to run. If you prefix any command with "sudo", it will run that command with elevated privileges or in other words allow a user with proper permissions to execute a command as anoth
8 min read
sum command in Linux with Examples
The sum command in Linux is used to calculate the checksum and block count of files. This command helps in verifying file integrity by comparing the calculated checksum with an expected value. The sum command can operate on one or more files, and when no file is specified, it reads from standard inp
3 min read
sync command in Linux with Examples
sync command in Linux is used to synchronize cached writes to persistent storage. If one or more files are specified, sync only them, or their containing file systems. Syntax: sync [OPTION] [FILE]... Note: Nothing is being shown in the screenshots just because sync command makes the cache in the bac
1 min read
How to Manage System Services in Linux | systemctl Command
Linux operating systems are known for their robustness and versatility, and managing system services is a crucial aspect of maintaining a well-functioning system. With the advent of systemd, a system and service manager for Linux operating systems, the systemctl command has become an essential tool
8 min read
tac command in Linux with Examples
tac command in Linux is used to concatenate and print files in reverse. This command will write each FILE to standard output, the last line first. When no file is specified then this command will read the standard input. Here, we will look deeper into the tac command, exploring its syntax, various o
3 min read
Tail command in Linux with examples
It is the complementary of head command. The tail command, as the name implies, prints the last N number of data of the given input. By default, it prints the last 10 lines of the specified files. If more than one file name is provided then data from each file is preceded by its file name. Syntax of
7 min read
How to Compress Files in Linux | Tar Command
File compression is a fundamental task in managing and transferring data efficiently on a Linux system. The Tar command, short for Tape Archive, is a powerful tool that allows users to create compressed and archived files. In this comprehensive guide, we will explore the various options and examples
11 min read
tee command in Linux with examples
tee command reads the standard input and writes it to both the standard output and one or more files. The command is named after the T-splitter used in plumbing. It basically breaks the output of a program so that it can be both displayed and saved in a file. It does both the tasks simultaneously, c
2 min read
time command in Linux with examples
'time' command in Linux is used to execute a command and prints a summary of real-time, user CPU time and system CPU time spent by executing a command when it terminates. 'real' time is the time elapsed wall clock time taken by a command to get executed, while 'user' and 'sys' time are the number of
6 min read
How to Monitor System Activity in Linux | top Command
top command is used to show the Linux processes. It provides a dynamic real-time view of the running system. Usually, this command shows the summary information of the system and the list of processes or threads which are currently managed by the Linux Kernel. As soon as you will run this command it
10 min read
How to Create an Empty File in Linux | Touch Command
The touch command is a standard command used in the UNIX/Linux operating system which is used to create, change and modify the timestamps of a file. Basically, there are two different commands to create a file in the Linux system which are as follows: touch command: It is used to create a file witho
8 min read
tr command in Unix/Linux with examples
The tr command is a UNIX command-line utility for translating or deleting characters. It supports a range of transformations including uppercase to lowercase, squeezing repeating characters, deleting specific characters, and basic find and replace. It can be used with UNIX pipes to support more comp
4 min read
tracepath command in Linux with Examples
The 'tracepath' command in Linux is a network diagnostic tool used to trace the path packets take to reach a destination, discovering the Maximum Transmission Unit (MTU) along the way. It operates similarly to the traceroute command but does not require superuser privileges and offers a simpler set
2 min read
Traceroute Command in Linux with Examples
In networking, understanding the path that data packets take from one point to another is crucial for diagnosing and troubleshooting connectivity issues. One of the most valuable tools for this purpose is the traceroute command in Linux. Traceroute is a command-line tool used in Linux or other opera
8 min read
Tree command in Linux with examples
In UNIX/LINUX systems, as well as MS-DOS and Microsoft Windows, the tree is a recursive directory listing program that produces a depth-indented listing of files. With no arguments, the tree lists the files in the current directory. When directory arguments are given, the tree lists all the files or
6 min read
tty Command in Linux with Examples
In the Linux operating system, everything is represented within a file system, including hardware devices and terminal interfaces. Among these, the terminal is also treated as a file, allowing users to interact with the system by sending inputs and receiving outputs. One essential command related to
3 min read
type command in Linux with Examples
The type command in Linux is a useful utility for identifying how the shell will interpret a given command. It provides information on whether a command is a shell built-in, external binary, function, or alias, helping users understand the source of the command and its behavior. The command is parti
3 min read
uname command in Linux with Examples
Linux, renowned for its open-source flexibility and powerful performance, offers a range of commands that reveal the inner workings of your system. Among these, the 'uname' command stands out as a versatile tool that provides key details about your Linux machine. Here, we will learn the basics of th
4 min read
unexpand command in Linux with Examples
To convert the leading spaces and tabs into tabs, there exists a command line utility called unexpand command. The unexpand command by default converts each space into tabs writing the produced output to the standard output. Syntax $unexpand [OPTION]... [FILE]...where, OPTION: Represents the various
3 min read
uniq Command in Linux with Examples
The uniq command in Linux is a command-line utility that reports or filters out the repeated lines in a file. In simple words, uniq is the tool that helps to detect the adjacent duplicate lines and also deletes the duplicate lines. uniq filters out the adjacent matching lines from the input file(tha
7 min read
until command in Linux with Examples
until command in Linux is used to execute a set of commands as long as the final command in the 'until' Commands has an exit status that is not zero. It is mostly used where the user needs to execute a set of commands until a condition is true. Syntaxuntil COMMANDS; do COMMANDS; donewhere, CONDITION
2 min read
Linux | Uptime command with examples
It is used to find out how long the system is active (running). This command returns set of values that involve, the current time, and the amount of time system is in running state, number of users currently logged into, and the load time for the past 1, 5 and 15 minutes respectively. Linux uptime S
5 min read
How to add User in Linux | useradd Command
useradd is a command in Linux that is used to add user accounts to your system. It is just a symbolic link to adduser command in Linux and the difference between both of them is that useradd is a native binary compiled with the system whereas adduser is a Perl script that uses useradd binary in the
5 min read
usermod command in Linux with Examples
usermod command or modify user is a command in Linux that is used to change the properties of a user in Linux through the command line. After creating a user we have to sometimes change their attributes like password or login directory etc. so in order to do that we use the Usermod command. The info
4 min read