Thursday, 5 February 2026

time command

 


[root@oel01db Shell-Scripting]# time tar -cvf archive.tar /var/log/

tar: Removing leading `/' from member names

/var/log/

/var/log/tallylog

.

.

/var/log/spooler-20251222

/var/log/spooler

real    0m0.166s

user    0m0.010s

sys     0m0.041s

[root@oel01db Shell-Scripting]#

Real (0m0.166s): "Wall Clock Time" This is the total time that passed from the moment you hit Enter to the moment the command finished.

[root@oel01db Shell-Scripting]# time  { echo starting
>
> sleep 5
>
> echo ending
>
>  }
starting
ending

real    0m5.007s
user    0m0.002s
sys     0m0.000s
[root@oel01db Shell-Scripting]#


[root@oel01db Shell-Scripting]# type -a time
time is a shell keyword
time is /usr/bin/time
[root@oel01db Shell-Scripting]#


This output is a perfect illustration of how Linux handles commands. It means you actually have two different versions of time on your system, and they behave differently.

Here is the breakdown of what that output tells you:

1. time is a shell keyword

This is the Internal Version.

  • What it is: A part of the Bash shell itself (like if, while, or for).

  • Priority: Because it is a "keyword," Bash will always use this one by default when you just type time.

  • Strength: It is very flexible. It can time built-in shell functions, command blocks { ... }, and entire pipelines (time a | b | c).

  • Weakness: It only gives you the basic "real, user, sys" output. It doesn't have many options for formatting.

Shell Keywords (The "Structure" of Bash)

Keywords are the fundamental building blocks of the shell's programming language. They are reserved words that define the logic and flow of a script.

  • Examples: ifthenelsewhilefortime[[ ]]{ }.

  • The Difference: Keywords are part of the shell's grammar. You cannot use them like a normal command (for example, you can't run man [[ because it’s not a standalone utility).

Because Keywords are "hard-coded" into the Bash source code itself, they do not exist as files on your disk. You can check their usage using help command 

For example : help [
help {
help time etc

2. time is /usr/bin/time

This is the External Version.

  • What it is: An actual standalone program file located on your hard drive (part of the gnu-time package).

  • Priority: Bash ignores this unless you tell it specifically to use the file by typing the full path: /usr/bin/time.

  • Strength: It is much more powerful. It can track maximum RAM usage, disk I/O, and exit statuses. It also supports the -o flag you were looking for earlier to save output to a file.

  • Weakness: It cannot time shell keywords or complex Bash-only blocks as easily as the keyword version.

For example if you want to check how much RAM a script used

/usr/bin/time -v ./myscript.sh


[root@oel01db Shell-Scripting]# /usr/bin/time -v ls
archive.tar
        Command being timed: "ls"
        User time (seconds): 0.00
        System time (seconds): 0.00
        Percent of CPU this job got: 66%
        Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.00
        Average shared text size (kbytes): 0
        Average unshared data size (kbytes): 0
        Average stack size (kbytes): 0
        Average total size (kbytes): 0
        Maximum resident set size (kbytes): 2412
        Average resident set size (kbytes): 0
        Major (requiring I/O) page faults: 0
        Minor (reclaiming a frame) page faults: 122
        Voluntary context switches: 1
        Involuntary context switches: 0
        Swaps: 0
        File system inputs: 0
        File system outputs: 0
        Socket messages sent: 0
        Socket messages received: 0
        Signals delivered: 0
        Page size (bytes): 4096
        Exit status: 0
[root@oel01db Shell-Scripting]#


No comments:

Post a Comment

JFrog Artifactory - How to install

JFrog Artifactory OSS Installation Guide CentOS 9 + PostgreSQL 17 This guide provides a structured workflow to install JFrog Artifactory OSS...