How can I write a shell script to direct grep data into a date-based filename?
Question
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.
Solution
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`
OTHER TIPS
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`
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow