Skip to main content
70 votes

These two Bash lines are functionally equivalent. Can someone explain why one is faster?

time always times the directly following pipeline. A pipeline is a sequence of one or more commands (simple or compound) separated by one of the control operators | or |&. if ... fi is a single ...
pLumo's user avatar
  • 22.7k
50 votes
Accepted

How can I time a pipe?

It is working. The different parts of a pipeline are executed concurrently. The only thing that synchronises/serialises the processes in the pipeline is IO, i.e. one process writing to the next ...
Kusalananda's user avatar
  • 338k
46 votes
Accepted

get elapsed time in bash

Use the time since epoch to easily identify a span of time in a script man date %s seconds since 1970-01-01 00:00:00 UTC %N nanoseconds (000000000..999999999) . start_time="$(date -u +%s)" ...
Miati's user avatar
  • 3,150
45 votes

How does a Linux operating system stand going back in time (when applying winter time for example)? Going back a second, would it be the same problem?

In Linux, the operating system maintains a clock that runs fundamentally in UTC time, which does not have Daylight Saving time shifts. The (usually) one-hour Daylight Saving Time is handled by not ...
telcoM's user avatar
  • 99.3k
36 votes

get elapsed time in bash

bash has a builtin timer variable start=$SECONDS # do stuff end=$SECONDS duration=$(( end - start )) echo "stuff took $duration seconds to complete"
glenn jackman's user avatar
36 votes
Accepted

Why is filesystem time always some msecs behind system time in Linux?

The time used for file timestamps is the time at the last timer tick, which is always slightly in the past. The current_time function in inode.c calls ktime_get_coarse_real_ts64: /** * current_time - ...
Stephen Kitt's user avatar
33 votes
Accepted

Where is Unix Time / Official Time Measured?

Your headline question doesn't have a real answer; Unix time isn't a real timescale, and isn't "measured" anywhere. It's a representation of UTC, albeit a poor one because there are moments ...
hobbs's user avatar
  • 988
33 votes
Accepted

Manjaro OS shows wrong time

It seems as though Network time Protocol is either not installed or not working on your laptop. I suggest using the following commands to install it: Step 1: Install NTP sudo pacman -S ntp Step 2: ...
Prajwal's user avatar
  • 688
31 votes

What's the POSIX-compliant way to get the epoch timestamp in a shell?

For the epoch time as an integer number of seconds, that would be: awk 'BEGIN{srand(); print srand()}' or: awk 'BEGIN{print srand(srand())}' As in POSIX awk, srand() without argument uses the ...
Stéphane Chazelas's user avatar
28 votes
Accepted

How to measure time of program execution and store that inside a variable

If you are in bash (and not sh) and you DO NOT need sub-second accuracy, you can skip the call to date entirely and do it without spawning any extra processes, without having to separate the combined ...
sanpath's user avatar
  • 296
28 votes
Accepted

How to get execution millisecond time of a command in zsh?

zsh's time uses the TIMEFMT variable to control the format. By default, this is %J %U user %S system %P cpu %*E total, which produces the following. $ time sleep 2 sleep 2 0.00s user 0.00s system 0% ...
Sparhawk's user avatar
  • 20.1k
27 votes
Accepted

How can I make the output of `time` in zsh be like bash?

The time keyword in zsh produces output in the format specified by the variable TIMEFMT. The default value of this variable is %J %U user %S system %P cpu %*E total You can change this like this, ...
Kusalananda's user avatar
  • 338k
26 votes
Accepted

`time echo` got no output

In zsh, the time keyword has no effect on builtins (or other similar shell-internal constructs). From this mailing list post: Additional note: The time builtin applied to any construct that is ...
muru's user avatar
  • 73.3k
25 votes
Accepted

Why is `time` not in the GNU Bash manual?

It is described in the "Shell Grammar/Pipelines" subsection of the bash manpage. It is also described in the link that you provided in the Pipelines section, where it is indexed under "...
NickD's user avatar
  • 2,946
25 votes

Unix time, leap seconds, and converting Unix time to a date

I read many, many posts about Unix time and many say that a day is 86400 seconds. That's the definition, yes. But pages like this talk about leap seconds. And this made me confused. You're not the ...
ilkkachu's user avatar
  • 141k
22 votes

Why is filesystem time always some msecs behind system time in Linux?

Stephen Kitt's answer seems to be spot-on. We can reproduce this very nicely by actually getting the same "coarse" clock that the filesystem uses, at least on my kernel configuration; a C ...
Marcus Müller's user avatar
20 votes
Accepted

Command 'time' works on its own but not in a pipeline

The bash shell implements time as a keyword. The keyword is part of syntax of the pipeline. The syntax of a pipeline in bash is (from the section entitled "Pipelines" in the bash manual): [...
Kusalananda's user avatar
  • 338k
19 votes
Accepted

Find last shutdown time

According to your output: shutdown system down 3.14-1-amd64 Mon Jul 21 08:43 - 22:19 (13:36) You shutdown your system on July 21 at 08:43 and then after 13 hours and 36 minutes on July 21 at ...
Sajjad Hoviegar's user avatar
19 votes
Accepted

Why doesn't `strace` show this process is waiting for something?

The usual reason for hitting this issue, is that the process is blocking in page faults. These are reads or possibly writes to files performed through a memory mapping aka mmap(). You may have ...
sourcejedi's user avatar
18 votes
Accepted

Set ls -l time format

If you define an alias such as: alias ls='ls --time-style=long-iso' then all ls invocations which end up displaying dates will use that.
Stephen Kitt's user avatar
17 votes

Set time zone without root privilege

Yes, in a general way you can use: $ tzselect At the end of the selection it will tell you how to make the change permanent for the session, and for all future sessions. In your case this might be ...
Eduardo Trápani's user avatar
15 votes
Accepted

Time it takes to reboot a Linux server

I'm going to assume you're on CentOS 7+ or Ubuntu 15.04+ which both come with systemd. Systemd has some great tools for figuring out how long your system took to boot along with some visualizations to ...
Justin Garrison's user avatar
15 votes

Where is Unix Time / Official Time Measured?

UNIX time is measured on your computer, running UNIX. This answer is going to expect you to know what Coördinated Universal Time (UTC), International Atomic Time (TAI), and the SI second are. ...
JdeBP's user avatar
  • 69.3k
13 votes

Set ls -l time format

This question is similar to the one asked on AskUbuntu, so I'll repeat the answer I put there: export TIME_STYLE=<desired_time_style> So, if you want all invocation of ls to produce output with ...
pepoluan's user avatar
  • 1,353
13 votes
Accepted

Using time on bash functions (not commands)

Use the time keyword instead of the external command. Using the keyword allows you to run time on any shell command, including function calls, not just on running a program. You can control the output ...
Gilles 'SO- stop being evil''s user avatar
13 votes

Where is Unix Time / Official Time Measured?

The adjustments to the clock are co-ordinated by the IERS. They schedule the insertion of a leap second into the time stream as required. From The NTP Timescale and Leap Seconds The International ...
BillThor's user avatar
  • 8,975
13 votes

How can I time a pipe?

Would this be a better example? $ time perl -e 'alarm(3); 1 while 1;' | perl -e 'alarm(4); 1 while 1;' Alarm clock real 0m4.004s user 0m6.992s sys 0m0.004s The scripts busyloop for 3 and ...
ilkkachu's user avatar
  • 141k
13 votes

What's the POSIX-compliant way to get the epoch timestamp in a shell?

Write a C program that calls time() and prints the result. Borrowing from sample program in the specification of the time() function, let's call this e.g. seconds.c: #include <stdio.h> #include &...
ilkkachu's user avatar
  • 141k
13 votes

How can I set a time zone on a Linux machine that has only very minimal cmds available?

If you don’t have timezone files, you can still specify the timezone by describing it in the TZ variable (see man tzset for details). The minimal form is the timezone name followed by the offset from ...
Stephen Kitt's user avatar
13 votes

How does a Linux operating system stand going back in time (when applying winter time for example)? Going back a second, would it be the same problem?

Lots of good answers here already, but I didn't see the mention a far more prosaic fact: this kind of second-shifting happens all the time already on your computer. Sometimes the shift is even more ...
Vilx-'s user avatar
  • 507

Only top scored, non community-wiki answers of a minimum length are eligible