How can I redirect the output of the «time» command?
In the file I can see the output of the ls command, not that of time . Please explain, why I couldn’t and how to do this.
The answer to this question not only of interest for programmers, but also for other power-users: superuser.com
8 Answers 8
no need to launch sub shell. Use a code block will do as well.
Agreed, Best answer. Gives you the option to ditch the ls results and just get the time. Thank you ghostdog74
you can redirect the time output using,
Because you need to take (time ls) as a single command so you can use braces.
time command will take the arguments as a command. But parenthesis will group that as a one command. Ex: time ls > file1.txt In arguments, 0 = time 1 = «ls > file1.txt»
The command time sends it’s output to STDERR (instead of STDOUT). That’s because the command executed with time normally (in this case ls) outputs to STDOUT.
If you want to capture the output of time, then type:
That captures only the output of time, but the output of ls goes normal to the console. If you want to capture both in one file, type:
2> redirects STDERR, &> redirects both.
This answer actually answers why the OP’s redirect wasn’t working, which was one of the questions the OP was asking. Nobody else addresses this.
time is shell builtin and I’m not sure if there is way to redirect it. However you can use /usr/bin/time instead, which definitely accept any output redirections.
The problem is that builtin commands are executed within the shell environment itself, not a subshell. Since the stderr of your shell is typically connected to the terminal, redirection won’t do anything. To redirect the builtin time, I believe you’d have to do something like bash time mycmd 2>file . Or just call /usr/bin/time as was mentioned.
You can also use the non-built-in without specifying the full path: stackoverflow.com/questions/29540540/…
The reason why redirection does not seem to work with time is that it’s a bash reserved word (not a builtin!) when used in front of a pipeline. bash(1):
If the time reserved word precedes a pipeline, the elapsed as well as user and system time consumed by its execution are reported when the pipeline terminates.
So, to redirect output of time , either use curly braces:
If you don’t want to mix output from time and the command. With GNU time, you can use -o file like:
/usr/bin/time -o tim grep -e k /tmp 1>out 2>err
while tim is output of time, out and err are stdout and stderr from grep .
I use the redirection of stdout and stderr method with braces for testing.
The &>>rpt represents this >>rpt 2>&1 but shorter.
The braces will execute a command(s) in the current shell. See: man bash
Not the canonical use case, but another way to go.
Longer running simple tasks can be launched in a detached «screen» terminal with logged output. You could even give the log a unique name.
Primarily this method is good for something that will take hours and is invoked over SSH with a need to «check up on» from time to time. In preference to backgrounding and disowning.
screen -dmL time -v ./crackpassword
You get the same output a terminal would get, with the caveat that this is asynchronous. Of course it could be a script. The output may need tweaking.
How to add timestamp while redirecting stdout to file in bash in Linux?
Linux: How to add timestamp while redirecting stdout to file in Bash?
There are several ways to add a timestamp while redirecting stdout (standard output) to a file in Bash. Here, I will explain some of the most common methods:
Method 1: Using the tee command
The tee command is used to read from standard input and write the output to both standard output and one or more files. We can use this command to redirect stdout to a file, while also adding a timestamp to the output.
For example, you can use the date command to print the current date and time:
- Step 2 — Use the tee command to redirect the output of the previous command to a file, while also displaying it on the screen
In this example, we will redirect the output to a file named «output.txt»:
The following command will add a timestamp in the format of «YYYY-MM-DD HH:MM:SS» to the beginning of each line of output:
date | awk '' | tee output.txt
- Step 4 — To add a timestamp to the file name, you can use command substitution $() to insert the timestamp into the file name
The following command will create a file named «output_YYYY-MM-DD_HH-MM-SS.txt» with timestamp.
date | awk '' | tee output_$(date +\%Y-\%m-\%d_\%H-\%M-\%S).txt
Method 2: Using the script command
The script command is used to record a terminal session. We can use this command to redirect stdout to a file, while also adding a timestamp to the output.
- Step 1 — Start by running the script command with a file name to specify where the output should be saved
For example, you can use the following command to start a new terminal session and redirect the output to a file named «output.txt»:
- Step 2 — Once the terminal session starts, you can run any commands you want to redirect the output
For example, you can use the date command to print the current date and time:
The following command will add a timestamp in the format of «YYYY-MM-DD HH:MM:SS» to the beginning of each line of output:
Method 3: Using the logger command
used to send messages to the syslogd daemon. We can use this command to redirect stdout to a file, while also adding a timestamp to the output.
- Step 1 — Start by using the logger command to send the output of a command to the syslogd daemon
For example, you can use the following command to send the output of the date command to the syslogd daemon:
- Step 2 — To redirect the output to a file, you can use the logger command with the -f option and specify the file name
For example, you can use the following command to redirect the output of the date command to a file named «output.txt»:
- Step 3 — To add a timestamp to the output, you can use the -t option to specify a tag for the message
The following command will add a timestamp in the format of «YYYY-MM-DD HH:MM:SS» to the beginning of each line of output:
date | logger -t $(date +\%Y-\%m-\%d_\%H-\%M-\%S) -f output.txt
- Step 4 — To add a timestamp to the file name, you can use command substitution $() to insert the timestamp into the file name
The following command will create a file named «output_YYYY-MM-DD_HH-MM-SS.txt» with timestamp.
date | logger -t $(date +\%Y-\%m-\%d_\%H-\%M-\%S) -f output_$(date +\%Y-\%m-\%d_\%H-\%M-\%S).txt
Conclusion
In conclusion, adding a timestamp to the output while redirecting stdout to a file in Bash can be achieved by using the tee , script or logger command. Each of these commands have their own set of options and can be used in a variety of ways to achieve the desired outcome. The examples provided in this tutorial should give you a good starting point for redirecting stdout and adding timestamps to your output. Remember to experiment with different combinations of commands and options to find the solution that best fits your needs.
How to add timestamp to STDERR redirection
In bash/ksh can we add timestamp to STDERR redirection? E.g. myscript.sh 2> error.log I want to get a timestamp written on the log too.
13 Answers 13
If you’re talking about an up-to-date timestamp on each line, that’s something you’d probably want to do in your actual script (but see below for a nifty solution if you have no power to change it). If you just want a marker date on its own line before your script starts writing, I’d use:
( date 1>&2 ; myscript.sh ) 2>error.log
What you need is a trick to pipe stderr through another program that can add timestamps to each line. You could do this with a C program but there’s a far more devious way using just bash .
First, create a script which will add the timestamp to each line (called predate.sh ):
#!/bin/bash while read line ; do echo "$(date): $" done
( echo a ; sleep 5 ; echo b ; sleep 2 ; echo c ) | ./predate.sh
Fri Oct 2 12:31:39 WAST 2009: a Fri Oct 2 12:31:44 WAST 2009: b Fri Oct 2 12:31:46 WAST 2009: c
Then you need another trick that can swap stdout and stderr, this little monstrosity here:
Then it’s simple to combine the two tricks by timestamping stdout and redirecting it to your file:
( myscript.sh 3>&1 1>&2- 2>&3- ) | ./predate.sh >error.log
The following transcript shows this in action:
pax> cat predate.sh #!/bin/bash while read line ; do echo "$(date): $" done pax> cat tstdate.sh #!/bin/bash echo a to stderr then wait five seconds 1>&2 sleep 5 echo b to stderr then wait two seconds 1>&2 sleep 2 echo c to stderr 1>&2 echo d to stdout pax> ( ( ./tstdate.sh ) 3>&1 1>&2- 2>&3- ) | ./predate.sh >error.log d to stdout pax> cat error.log Fri Oct 2 12:49:40 WAST 2009: a to stderr then wait five seconds Fri Oct 2 12:49:45 WAST 2009: b to stderr then wait two seconds Fri Oct 2 12:49:47 WAST 2009: c to stderr
As already mentioned, predate.sh will prefix each line with a timestamp and the tstdate.sh is simply a test program to write to stdout and stderr with specific time gaps.
When you run the command, you actually get «d to stdout» written to stderr (but that’s your TTY device or whatever else stdout may have been when you started). The timestamped stderr lines are written to your desired file.