Ian Cook
Ian Cook

Reputation:

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

I basically want to do this:

grep 'example.com' www_log > example.com.YYYY-MM-DD-H:i:S.log

...with of course the filename being example.com.2008-09-27-11:21:30.log

I'd then put this in crontab to run daily.

Upvotes: 5

Views: 1442

Answers (3)

Mez
Mez

Reputation: 24953

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

Upvotes: 5

Tao Zhyn
Tao Zhyn

Reputation: 1736

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`

Upvotes: 2

Michael Ridley
Michael Ridley

Reputation: 10498

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`

Upvotes: 9

Related Questions