How can I write a shell script to direct grep data into a date-based filename? How can I write a shell script to direct grep data into a date-based filename? shell shell

How can I write a shell script to direct grep data into a date-based filename?


The verbose method:

grep 'example.com' www_log > `date +example.com.%Y-%m-%d-%H:%M:%S.log`

The terse method:

grep 'example.com' www_log > `date +example.com.%F-%T.log`


grep 'example.com' www_log > example.com.$(date +%F-%T).log


Here is another way, that I usually use:

grep 'example.com' www_log > example.com.`date +%F-%T`.log

Backticks are a form of command substitution. Another form is to use $():

$(command)

which is the same as:

`command`