-1

When I tried exection grep function in particular directory its working on that directory but when I am trying to execute using absolute paths grep is not working for example

 grep `date +%Y-%m-%d` /var/wwww/file/file.log >> /home/filename/file.log

The above command output getting as empty file.
Is it possible to schedule grep in crontab to match text and generate valid output ?

HopelessN00b
  • 53,385
  • 32
  • 133
  • 208

1 Answers1

1

The % in your crontab file gets turned into a newline character unless escaped with a backslash. Try

grep `date +\%Y-\%m-\%d` /var/wwww/file/file.log >> /home/filename/file.log

see

man 5 crontab

for details.

Tobi Oetiker
  • 1,752
  • 12
  • 12
  • its working great from command line but how to set this as cronjob ? do you think grep works inside cron ? – user3585106 Feb 18 '15 at 07:17
  • its working as crob job too !!!! – user3585106 Feb 18 '15 at 07:24
  • 1
    the `%` in the crontab problem happens to me every few years as I tend to forget this special handling :) – Tobi Oetiker Feb 18 '15 at 08:06
  • It saved my day. I just want to write a bash script for automation for present day log. I am very noob in bash script so instead of going for bash script i thought about grep and cron. They just worked for me prefectly. – user3585106 Feb 18 '15 at 08:11