Orsius
Orsius

Reputation: 1029

bash --redirect output to log file (with day date in the name)

I just want to to redirect my script output to a file, to do so I try to redirect the output of a simple command and it works for a particular syntax and not for the other. Could you please have a look and give me any advice, Thanks in advance. Rgds, O.

This one is working fine:

du -h > "/var/log/mytst.$(date +%Y-%m-%d_%H:%M).log"

and this doesn't:

du -h > /var/log/mytst."$(date +"%D--%H:%M:%S")".log 2>&1

Any idea?

Upvotes: 15

Views: 31288

Answers (1)

Marcos Dione
Marcos Dione

Reputation: 566

Look at this output:

$ echo "/var/log/mytst.$(date +%Y-%m-%d_%H:%M).log"
/var/log/mytst.2014-08-11_13:54.log
$ echo /var/log/mytst."$(date +"%D--%H:%M:%S")".log
/var/log/mytst.08/11/14--13:54:00.log

The second one expects a tree hierarchy to exist, as the /'s in the output of date +%D are treated as directory separators. Your question does not state how the second version 'does not work', so I would bet the error is No such file or directory.

Upvotes: 27

Related Questions