The time
command in Linux is used to measure the execution time of a command or program. It provides the amount of time the program took to execute, broken down into real time, user CPU time, and system CPU time.
Syntax
time <command>
Output Breakdown
- real: Total elapsed time (wall clock time) it took to execute the command.
- user: The time spent in user-mode (the time the CPU was working on the command itself).
- sys: The time spent in kernel-mode (system calls and operating system tasks).
Examples
1. Basic Usage
time ls
This will show how long it took to run the ls
command.
Output Example:
real 0m0.002s
user 0m0.000s
sys 0m0.002s
real
is the wall-clock time it took to list the directory.user
andsys
times are very low sincels
doesn’t require a lot of processing.
2. Using time
with a Complex Command
time find / -name "*.log" > result.txt
This command will search for all .log
files on the system and redirect the output to result.txt
. It will also show how much time was spent to complete the task.
3. Using time
with a Program
time ./my_program
This will show how long it took to run a program named my_program
in the current directory.
4. Using time
with Pipe
time cat largefile.txt | grep "error" > error_logs.txt
This runs cat
and grep
in a pipeline, and time
measures the total time spent running the whole pipeline.
5. Using time
for a Command with Multiple Processes
time tar -czf archive.tar.gz folder/
This command creates a compressed tarball from a folder and measures how long it takes.
Additional Options
1. -p
(POSIX output format)
time -p ls
This gives a more concise, POSIX-compliant output format:
real 0.01
user 0.00
sys 0.00
2. -v
(Verbose output)
time -v ls
This provides a more detailed output:
Command being timed: "ls"
User time (seconds): 0.00
System time (seconds): 0.00
Percent of CPU this job got: 0%
Elapsed (wall clock) time (h:mm:ss or m:ss): 0m0.01s
Summary
The time
command is a useful tool for measuring the performance of commands and programs. It gives insights into how much CPU time was spent in user mode, system mode, and the overall real time (elapsed wall clock time).