jettisamba jettisamba - 4 months ago 7x
Linux Question

Grep time command output


time ls
, I have the following output:

$ time ls -l
total 2
-rwx------+ 1 FRIENDS None 97 Jun 23 08:59 location.txt
-rw-r--r--+ 1 FRIENDS None 10 Jun 23 09:06 welcome
real 0m0.040s
user 0m0.000s
sys 0m0.031s

Now, when I try to
only the real value line, the actual result is:

$ time ls -l | grep real
real 0m0.040s
user 0m0.000s
sys 0m0.031s

My question is, how to get only the real value as output? In this case,


time writes its output to stderr, so you need to pipe stderr instead of stdout. But it's also important to remember that time is part of the syntax of bash, and it times an entire pipeline. Consequently, you need to wrap the pipeline in braces, or run it in a subshell:

{ time ls -l >/dev/null; } |& grep real

(|& is an abbreviation for 2>&1 |)

Alternatively, you can use the time utility, which allows control of the output format. On my system, that utility is found in /usr/bin/time:

/usr/bin/time -f%e ls -l >/dev/null 

man time for more details on the time utility.