Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grep time command output

Tags:

linux

Using 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 grep 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, 0m0.040s.

like image 438
jettisamba Avatar asked Jun 23 '13 04:06

jettisamba


People also ask

What is time command output?

This is all elapsed time including time slices used by other processes and time the process spends blocked (for example if it is waiting for I/O to complete). User is the amount of CPU time spent in user-mode code (outside the kernel) within the process. This is only actual CPU time used in executing the process.

What does grep return?

The grep command searches a text file based on a series of options and search string and returns the lines of the text file which contain the matching search string.

How do I view a specific time log in Linux?

Linux logs will display with the command cd/var/log. Then, you can type ls to see the logs stored under this directory. One of the most important logs to view is the syslog, which logs everything but auth-related messages.


4 Answers

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; } 2>&1 | grep real
 real   0m0.005s

With Bash v4.0 (probably universal on Linux distributions but still not standard on Mac OS X), you can use |& to pipe both stdout and stderr:

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

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.

like image 184
rici Avatar answered Oct 12 '22 07:10

rici


(time ls -l)  2>&1 > /dev/null |grep real

This redirects stderr (which is where time sends its output) to the same stream as stdout, then redirects stdout to dev/null so the output of ls is not captured, then pipes what is now the output of time into the stdin of grep.

like image 40
Clyde Avatar answered Oct 12 '22 06:10

Clyde


If you just want to specify the output format of time builtin, you can modify the value of TIMEFORMAT environment variable instead of filtering it with grep.

In you case,

TIMEFORMAT=%R
time ls -l

would give you the "real" time only.

Here's the link to relevant information in Bash manual (under "TIMEFORMAT").

This is a similar question on SO about parsing the output of time.

like image 13
doubleDown Avatar answered Oct 12 '22 07:10

doubleDown


Look out.. bash has a built-in "time" command. Here are some of the differences..

# GNU time command (can also use $TIMEFORMAT variable instead of -f)
bash> /usr/bin/time -f%e ls >/dev/null
0.00


# BASH built-in time command (can also use $TIME variable instead of -f)
bash> time -f%e ls >/dev/null
-f%e: command not found

real    0m0.005s
user    0m0.004s
sys     0m0.000s
like image 3
Immotus Avatar answered Oct 12 '22 05:10

Immotus