plamtrue
plamtrue

Reputation: 73

redirect a process to file AND redirect its time to a file

I'd like to redirect the time of a process to a file, and the process itself redirects to a file. Here's what I've tried:

time &> ltime.txt echo | ls -l > ls-l.txt

Both files (ltime.txt and ls-l.txt) are created, but ltime.txt is empty.

time still outputs to the screen, which is fine in my case, but I expected it to not do that because of the &>:

    real    0m0.034s
    user    0m0.000s
    sys     0m0.004s

(All that I know about this came from http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-3.html)

Any suggestions?

Upvotes: 2

Views: 68

Answers (2)

danfuzz
danfuzz

Reputation: 4353

Maybe you're getting bitten by the time that's built-in to the shell? Try this:

/usr/bin/time ls -l > ls-l.txt 2> ltime.txt

From man time:

   Users of the bash shell need to use an explicit path in order to run
   the external time command and not the shell builtin variant.

[Updated to add]

Apparently the location of time isn't totally standard. For a one-off use (and for general info) which time will tell you where it is. For a portable script you can say command time .... (command is a shell built-in that tells the shell to ignore built-ins.)

Upvotes: 1

damienfrancois
damienfrancois

Reputation: 59310

Try

{ time ls -l > ls-l.txt ; }  &> ltime.txt 

The curly braces capture the output even with the builtin.

Upvotes: 1

Related Questions