Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using tee command to create time specific log files

Tags:

bash

shell

tee

I am using 'tee' command to redirect the log of my shell program into a file.

My requirement is to append the current date and time with the file name.

sh sample.sh | tee logfile_$date.txt #sample command

Output Log File:

logfile_2013-08-22-14.txt  #yyyy-mm-dd-hh format

How can i achieve it ?

like image 302
Shivam Agrawal Avatar asked Feb 03 '26 18:02

Shivam Agrawal


1 Answers

Since date '+%Y-%m-%d-%H' returns a date of the type 2013-08-21-10 (year-month-day-hour), you can use the following:

sh sample.sh | tee logfile_$(date '+%Y-%m-%d-%H').txt

For example, let's print hello and also store it in a file:

$ echo "hello" | tee logfile_$(date '+%Y-%m-%d-%H').txt
hello
$ ls logfile_*
logfile_2013-08-21-10.txt

As you see, a file with the name logfile_2013-08-21-10.txt has been created at the same time that the string appeared in the screen.

like image 122
fedorqui 'SO stop harming' Avatar answered Feb 06 '26 11:02

fedorqui 'SO stop harming'



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!